springcloud微服務實戰 學習筆記三 服務消費者

zhumeilu發表於2017-12-14

###第一種方式LoadBalancerClient

  • 依賴

          <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>1.5.4.RELEASE</version>
              <relativePath/> <!-- lookup parent from repository -->
          </parent>
          <properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
              <java.version>1.8</java.version>
          </properties>
    
          <dependencies>
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-eureka-server</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
          </dependencies>
    
          <dependencyManagement>
              <dependencies>
                  <dependency>
                      <groupId>org.springframework.cloud</groupId>
                      <artifactId>spring-cloud-dependencies</artifactId>
                      <version>Dalston.SR1</version>
                      <type>pom</type>
                      <scope>import</scope>
                  </dependency>
              </dependencies>
          </dependencyManagement>
    
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                  </plugin>
              </plugins>
          </build>
    複製程式碼
  • 配置檔案

      spring.application.name=eureka-consumer
      server.port=3333
      #服務註冊中心
      eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
    複製程式碼
  • application

      @EnableDiscoveryClient
      @SpringBootApplication
      public class Application {
      
          @Bean
          public RestTemplate restTemplate() {
              return new RestTemplate();
          }
      
          public static void main(String[] args) {
              new SpringApplicationBuilder(Application.class).web(true).run(args);
          }
      }
    複製程式碼
  • controller

      @RestController
      public class DemoController {
          Logger logger = LoggerFactory.getLogger(this.getClass());
          @Autowired
          LoadBalancerClient loadBalancerClient;
          @Autowired
          RestTemplate restTemplate;
          @GetMapping("/hello")
          public String hello(){
              ServiceInstance serviceInstance = loadBalancerClient.choose("eureka-client");
              logger.info("host:"+serviceInstance.getHost()+"---port:"+serviceInstance.getPort()+"---uri"+serviceInstance.getUri());
              String url = "http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/hello";
              String forObject = restTemplate.getForObject(url, String.class);
      
              return forObject;
          }
      
      }
    複製程式碼

###第二種方式Ribbon

  • 依賴 在一種方式下新增依賴

              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-ribbon</artifactId>
              </dependency>
    複製程式碼

配置檔案修改埠號即可 修改Application.java,在RestTemplate上新增註解

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
複製程式碼

controller修改為

    @GetMapping("/hello2")
    public String hello2(){
        return restTemplate.getForObject("http://eureka-client/hello",String.class);
    }
複製程式碼

###第三種方式Feign

  • 依賴 在第一種方式下新增依賴

      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-feign</artifactId>
      </dependency>
    複製程式碼

配置檔案修改埠號即可

Application.java新增註解@EnableFeignClients支援Feign客戶端

    @EnableFeignClients
    @EnableDiscoveryClient
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            new SpringApplicationBuilder(Application.class).web(true).run(args);
        }
    }
複製程式碼

建立一個介面 @FeignClient("eureka-client")設定使用的服務名稱

    @FeignClient("eureka-client")
    public interface DemoService {
    
        @GetMapping("/hello")
        String hello();
    }
複製程式碼

controller

    @RestController
    public class DemoController {
    
        Logger logger = LoggerFactory.getLogger(this.getClass());
        @Autowired
        DemoService demoService;
        @GetMapping("/hello")
        public String hello(){
    
            return demoService.hello();
        }
    }
複製程式碼

三種方式可以混合使用,第二種方式因為將RestTemplate物件新增了註解,所以第一種方式就不能直接使用了,只能利用其它方式傳送http請求獲取資料。

相關文章