美文网首页
03、SpringCloud 消费者(Consumer)

03、SpringCloud 消费者(Consumer)

作者: adced06edef5 | 来源:发表于2020-03-29 13:08 被阅读0次

一、代码示例

说明:此处使用的SpringBoot版本为2.1.13.RELEASE,SpringCloud版本为Greenwich.SR5
SpringCloud消费者以REST调用服务
1、maven依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2、application.yml

server:
  port: 9001
spring:
  application:
    name: consumer
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
    instance-id: consumer-9001
  client:
    service-url:
      defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
#info信息
info:
  app:
    name: consumer-9001
  company:
    name: www.xxx.com
  build:
    artifactId: ${project.artifactId}
    version: ${project.version}

3、启动类
注入RestTemplate进行调用,此处在启动类中进行创建。

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class Consumer9001Application {

    public static void main(String[] args) {
        SpringApplication.run(Consumer9001Application.class,args);
    }

    @Bean
    RestTemplate restTemplate(){
        return new RestTemplate();
    }

}

4、其他java类
在Controller类型注入RestTemplate,并通过RestTemplate进行REST调用。RestTemplate可以注入到其他Bean类型,此处为了方便演示放在了Controller中。

package org.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class HelloController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable  String name){
        return restTemplate.getForObject("http://localhost:8001/hello/"+name,String.class);
    }
}

2、测试验证

先后启动server、client和consumer服务,访问http://localhost:7001/可以看到client和consumer都已经注册到了eureka。

image.png
访问http://localhost:9001/hello/zs
image.png
表明consumer成功调用了client提供的服务。
github:
https://github.com/panli1988/cloud01
https://github.com/panli1988/cloud02
参考:
https://blog.csdn.net/forezp/article/details/70148833
http://www.itmuch.com/spring-cloud/spring-cloud-index/
还有尚硅谷周阳老师的视频

相关文章

网友评论

      本文标题:03、SpringCloud 消费者(Consumer)

      本文链接:https://www.haomeiwen.com/subject/surwshtx.html