通過上一篇《三:服務(wù)的注冊與發(fā)現(xiàn)(Eureka》,我們已經(jīng)成功地將服務(wù)提供者:provider-test注冊到了Eureka服務(wù)注冊中心上了,那么接下來我們要學(xué)習(xí)的就是:如何去消費服務(wù)提供者的接口猾普?
4.1使用LoadBalancerClient
在Spring Cloud Commons中提供了大量的與服務(wù)治理相關(guān)的抽象接口,包括DiscoveryClient本谜、LoadBalancerClient等初家。從LoadBalancerClient接口的命名中,可以看出這是一個負(fù)載均衡客戶端的抽象定義乌助,下面筆者將使用Spring Cloud提供的負(fù)載均衡器客戶端接口來實現(xiàn)服務(wù)的消費溜在。
首先,將利用上一篇中構(gòu)建的eureka-server作為服務(wù)注冊中心他托、provider-test作為服務(wù)提供者為基礎(chǔ)掖肋。
- 創(chuàng)建一個叫voyer-consumer-test的Spring Boot項目,引入相關(guān)maven包赏参。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.voyer</groupId>
<artifactId>voyer-consumer-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>voyer-consumer-test</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.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>
<spring-cloud.version>Finchley.M9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</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>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
- 然后配置
application.yml
志笼,指定服務(wù)注冊中心地址、端口號以及名稱
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: consumer-test
- 在默認(rèn)的啟動程序中注入
RestTemplate
@SpringBootApplication
public class VoyerConsumerTestApplication {
public static void main(String[] args) {
SpringApplication.run(VoyerConsumerTestApplication.class, args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
- 創(chuàng)建
com.voyer.web
包路徑把篓,創(chuàng)建ConsumerController
纫溃,并注入LoadBalancerClient
和RestTemplate
,并在/hi接口的實現(xiàn)中,先通過loadBalancerClient
的choose
函數(shù)來負(fù)載均衡的選出一個provider-test
的服務(wù)實例韧掩,這個服務(wù)實例的基本信息存儲在ServiceInstance中紊浩,然后通過這些對象中的信息拼接出訪問/hi接口的詳細(xì)地址,最后再利用RestTemplate對象實現(xiàn)對服務(wù)提供者接口的調(diào)用:
@RestController
public class ConsumerController {
@Autowired
LoadBalancerClient loadBalancerClient;
@Autowired
RestTemplate restTemplate;
@RequestMapping("/hi")
public String hello(){
ServiceInstance serviceInstance = loadBalancerClient.choose("provider-test");
String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/hi";
System.out.println(url);
return restTemplate.getForObject(url, String.class);
}
}
訪問http://localhost::8764/hi ,會發(fā)現(xiàn)每次訪問的返回的信息會循環(huán)輸出hello world! I am from 8763
和 hello world! I am from 8762
郎楼。
4.2使用Ribbon
Spring Cloud Ribbon是基于Netflix Ribbon實現(xiàn)的一套客戶端負(fù)載均衡的工具万伤。它是一個基于HTTP和TCP的客戶端負(fù)載均衡器。它可以通過在客戶端中配置ribbonServerList來設(shè)置服務(wù)端列表去輪詢訪問以達(dá)到均衡負(fù)載的作用呜袁。
每個load balancer都是組件的一部分敌买,這些組件協(xié)同工作,Spring Cloud通過使用RibbonClientConfiguration為每個指定的客戶端創(chuàng)建一個新的套裝阶界,這包括ILoadBalancer虹钮、RestClient和ServerListFilter。
- 創(chuàng)建一個叫voyer-consumer-ribbon的Spring Boot項目膘融,(操作順序同上)引入相關(guān)maven包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
- 修改默認(rèn)啟動類芙粱。增加為
@EnableEurekaClient
注解(此處為什么不用@EnableDiscoveryClient
,讀者可以百度一下這兩者的區(qū)別)氧映,RestTemplate
增加@LoadBalanced
注解:
@SpringBootApplication
@EnableEurekaClient
public class VoyerConsumerRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(VoyerConsumerRibbonApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
- 修改配置文件
application.yml
eureka:
client:
registerWithEureka: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8765
spring:
application:
name: consumer-ribbon
- 修改
ConsumerController
:
@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/hi")
public String hello(){
return restTemplate.getForObject("http://PROVIDER-TEST/hi", String.class);
}
}
啟動程序春畔,然后訪問http://localhost:8765,會發(fā)現(xiàn)
hello world! I am from 8762
hello world! I am from 8763
這兩個循環(huán)出現(xiàn)。到此ribbon消費者成功岛都。
4.3使用Feign
Feign是一個聲明性的web服務(wù)客戶端律姨,它使編寫web服務(wù)客戶機(jī)變得更容易。使用Feign創(chuàng)建接口并對其進(jìn)行注釋臼疫。它有可插入的注釋支持择份,包括Feign注釋和JAX-RS注釋。Feign還支持可插入式的編碼器和解碼器烫堤。Spring Cloud增加了對Spring MVC注釋的支持荣赶,并支持在Spring Web中使用默認(rèn)的HttpMessageConverters。Spring Cloud集成了Ribbon和Eureka鸽斟,在使用Feign時提供負(fù)載平衡的http客戶端拔创。(來自有道翻譯)
總結(jié)兩點:1、Feign采用的是接口加注解;2湾盗、Feign 整合了ribbon
- 創(chuàng)建一個叫voyer-consumer-feign的Spring Boot項目伏蚊,(操作順序同上)引入相關(guān)maven包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
- 配置文件:
registerWithEureka: false
自身是消費者,不注冊為服務(wù)提供者格粪。
eureka:
client:
registerWithEureka: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8766
spring:
application:
name: consumer-feign
- 默認(rèn)啟動類增加
@EnableDiscoveryClient
躏吊、@EnableFeignClients
注解:
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class VoyerConsumerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(VoyerConsumerFeignApplication.class, args);
}
}
- 創(chuàng)建service包路徑,然后新建一個ConsumerService的接口帐萎,通過@ FeignClient(“服務(wù)名”)比伏,來指定調(diào)用哪個服務(wù):
@FeignClient(value = "provider-test")
public interface ConsumerService {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String hiFromProvider();
}
- 創(chuàng)建
com.voyer.web
包路徑,創(chuàng)建ConsumerController
:
@RestController
public class ConsumerController {
@Autowired
ConsumerService consumerService;
@RequestMapping(value = "/hi",method = RequestMethod.GET)
public String hi(){
return consumerService.hiFromProvider();
}
}
啟動程序疆导,然后訪問http://localhost:8766,會發(fā)現(xiàn)
hello world! I am from 8762
hello world! I am from 8763
這兩個循環(huán)出現(xiàn)赁项。到此feign消費者成功。