Fork me on GitHub

【Spring Cloud】服务消费者

创建Spring Boot项目并添加依赖

创建项目过程略

添加依赖

<!-- 用于进行WEB开发,向外提供服务接口-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加spring cloud eureka-client-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

服务配置

  • application.yml

    spring:
      application:
        name: service-consumer
    server:
      port: 8082
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8000/eureka/ //对应服务注册中心的配置,指定服务注册中心地址
    
  • 启动类添加注解@EnableEurekaClient

    @EnableEurekaClient
    @SpringBootApplication
    public class ServiceProductApplication {
        @Bean
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
        
        public static void main(String[] args) {
            SpringApplication.run(ServiceProductApplication.class, args);
        }
    }
    

添加服务接口

Get /department/consumer (不推荐)

  • 创建DepartmentController

    package com.example.serviceconsumer.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.cloud.client.ServiceInstance;
    import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    @RequestMapping("/department")
    public class DepartmentController {
    
        @Autowired
        private RestTemplate restTemplate;
    
        @Autowired
        private LoadBalancerClient loadBalancerClient;
    
        @GetMapping("/consumer")
        public String getUser() {
            ServiceInstance serviceInstance = loadBalancerClient.choose("service-producter");
            // 客户端负载时请的URL 通过服务实例拼接的并对服务依赖太大,所以感觉不够友好
            String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/user/123";
            return restTemplate.getForObject(url, String.class);
        }
    }
    
  • 测试接口

    访问http://localhost:8082/consumer,返回结果如下

    name: gary, id: 123
    

Get /department/consumer /{id}(推荐)

  • 修改主程序入口类
    添加注解@LoadBalanced
@EnableEurekaClient
@SpringBootApplication
public class ServiceConsumerApplication {
  
    @Bean("restTemplateWithLB")
    @LoadBalanced
    public RestTemplate restTemplateWithLB() {
        return new RestTemplate();
  }
  
    public static void main(String[] args) {
        SpringApplication.run(ServiceConsumerApplication.class, args);
    }
  }
  • 修改DepartmentController
  package com.example.serviceconsumer.controller;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Qualifier;
  import org.springframework.cloud.client.ServiceInstance;
  import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RestController;
  import org.springframework.web.client.RestTemplate;
  
  @RestController
  @RequestMapping("/department")
  public class DepartmentController {
  
    @Autowired
    @Qualifier("restTemplateWithLB")
    private RestTemplate restTemplateWithLB;
  
      @GetMapping("/consumer/{id}")
    public String getUserInfo(@PathVariable("id") String id) {
        // 此处只依赖于服务名称
        String url = "http://service-producter/user/" + id;
        return restTemplateWithLB.getForObject(url, String.class);
    }
  }
  • 测试接口
    访问http://localhost:8082/consumer/666,返回结果如下
name: gary, id: 666
posted @ 2020-10-23 18:28  逍遥メ风  阅读(65)  评论(0编辑  收藏  举报