springcloud(四)服务治理之服务消费者

一、概述

前面通过 Eureka Client 组件创建了一个服务提供者 provider,并且在注册中心完成注册,接下来就来实现一个服务消费者 consumer,调用 provider 相关接口。

二、搭建服务消费者

先通过 Spring Boot 搭建一个微服务应用,再通过 Eureka Client 将其注册到 Eureka Server。此时的 provider 和 consumer 从代码的角度看并没有区别,都是 Eureka 客户端,我们人为地从业务角度对它们进行区分,provider 提供服务,consumer 调用服务,具体的实现需要结合 RestTemplate 来完成,即在服务消费者 consumer 中通过 RestTemplate 来调用服务提供者 provider 的相关接口。

1、创建maven 子模块consumer 。

2、pom添加 Eureka Client 依赖。

    <dependencies>
        <!--添加 Eureka Client 依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

3、在 resources 路径下创建配置文件 application.yml,添加 Eureka Client 相关配置,此时的 Eureka Client 为服务消费者 consumer。

server:
  port: 8020 #当前 Eureka Client 服务端口
spring:
  application:
    name: consumer #当前服务注册在 Eureka Server 上的名称
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka/ #注册中心的访问地址
  instance:
    prefer-ip-address: true #是否将当前服务的 IP 注册到 Eureka Server

4、创建启动springboot启动类consumerApplication 并添加RestTemplate 配置。

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

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

5、创建controller,注入RestTemplate 实例,业务方法通过RestTemplate 调用provider 服务。

@RequestMapping("/consumer")
@RestController
public class StudentHandler {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/findAll")
    public Collection<Student> findAll(){
        return restTemplate.getForObject("http://localhost:8010/student/findAll",Collection.class);
    }

// .....完整实现参考https://gitee.com/zhengj/myspringcloud.git......
}

RestTemplate 是 Spring 框架提供的基于 REST 的服务组件,底层对 HTTP 请求及响应进行了封装,提供了很多访问远程 REST 服务的方法,可简化代码开发。基于这个特性,我们可以实现不同微服务之间的调用。

RestTemplate 分别通过 GET、POST、PUT、DELETE 请求,访问服务资源:

具体API:

GET: 

1、ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables);

url 为请求的目标资源,responseType 为响应数据的封装模版,uriVariables 是一个动态参数,可以根据实际请求传入参数。

getForEntity 方法的返回值类型为 ResponseEntity,通过调用其 getBody 方法可获取结果对象。

2、T getForObject(String url, Class<T> responseType, Object... uriVariables)

getForObject 方法的使用与 getForEntity 很类似,唯一的区别在于 getForObject 的返回值就是目标对象,无需通过调用 getBody 方法来获取

POST

1、 ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables) ;

url 为请求的目标资源,request 为要保存的目标对象,responseType 为响应数据的封装模版,uriVariables 是一个动态参数

示例代码:
@PostMapping("/save")
public Collection<User> save(@RequestBody User user){
   return restTemplate.postForEntity("http://localhost:8080/user/save",user,Collection.class).getBody();
}

2、T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables)

postForObject 方法的使用与 postForEntity 类似,唯一的区别在于 postForObject 的返回值就是目标对象,无需通过调用 getBody 方法来获取,具体使用如下所示。

@PostMapping("/save2")
public Collection<User> save2(@RequestBody User user){
   return restTemplate.postForObject("http://localhost:8080/user/save",user,Collection.class);
}

PUT:

 

void put(String url, @Nullable Object request, Object... uriVariables);

url 为请求的目标资源,request 为要修改的目标对象,uriVariables 是一个动态参数,可以根据实际请求传入参数,具体使用如下所示。

@PutMapping("/update")
public void update(@RequestBody User user){
   restTemplate.put("http://localhost:8080/user/update",user);
}

DELETE:

void delete(String url, Object... uriVariables);

url 为请求的目标资源,uriVariables 是一个动态参数,可以根据实际请求传入参数,具体使用如下所示。

@DeleteMapping("/deleteById/{id}")
public void delete(@PathVariable("id") Long id){
   restTemplate.delete("http://localhost:8080/user/deleteById/{id}",id);
}

6、完整工程目录如下:

7、依次启动注册中心、服务提供者 provider,并运行 ConsumerApplication。

打开浏览访问注册中心界面  http://localhost:8761 可看见服务提供者和消费者已经注册到注册中心了。

8、接下来就可以访问 consumer 的相关服务了。

三、总结

使用 Eureka Client 组件在 Eureka Server 注册一个服务消费者 consumer 的具体实现,无论是服务消费者还是服务提供者,都通过 Eureka Client 组件来实现注册,实现服务消费者 consumer 之后,通过 RestTemplate 完成对服务提供者 provider 相关服务的调用。

 

完整demo下载地址:

https://gitee.com/zhengj/myspringcloud.git

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值