在上一篇文章,講解了經過RestTemplate+Ribbon消費服務,今天主要講述怎樣經過Feign去消費服務。java
Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and for using the same HttpMessageConverters used by default in Spring Web. Spring Cloud integrates Ribbon and Eureka to provide a load balanced http client when using Feign.git
Feign是一個聲明式的web服務客戶端,它使得寫web客戶端變得更簡單。想要使用Feign,只須要建立一個接口並註解它。Feign具備可插拔的註解特性,可以使用Feign 註解和JAX-RS註解。Feign還支持可插拔的編碼器和解碼器。Spring Cloud添加了對Spring MVC註釋的支持,並默認使用和Spring Web相同的HttpMessageConverters。當使用Feign時,Spring Cloud集成了Ribbon和Eureka以提供負載平衡的http客戶端。web
<!-- more -->spring
簡而言之:apache
繼續在第一節項目的基礎上,啓動eureka-server,端口爲9090;啓動兩個eureka-client, 端口爲8040、8041。瀏覽器
使用Spring Initializr
新建一個項目,取名爲feign-service
, 在Spring Cloud Discovery中勾選Eureka Discovery Client,在Spring Cloud Routing中勾選OpenFeign,在Web中勾選Spring Web:app
建立成功後,項目pom.xml以下:負載均衡
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.noodles.mars</groupId> <artifactId>feign-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>feign-service</name> <description>Feign Service</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR3</spring-cloud.version> </properties> <dependencies> <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.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</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> </project>
feign-service配置服務中心地址、應用名、端口,配置文件內容:maven
server: port: 8080 eureka: client: service-url: defaultZone: http://localhost:9090/eureka/ spring: application: name: feign-client
在項目啓動類上註解@EnableDiscoveryClient, 開啓向服務中心註冊;註解@EnableFeignClients開啓Feign功能:ide
@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class FeignServiceApplication { public static void main(String[] args) { SpringApplication.run(FeignServiceApplication.class, args); } }
定義一個feign接口,經過@FeignClient("服務應用名")
,來指定調用哪一個服務。好比在代碼中調用了hello-erueka-client
服務的/hello
接口,代碼以下:
@FeignClient(value = "hello-eureka-client") public interface FeignService { @GetMapping(value = "/hello") String hello(@RequestParam(value = "name") String name); }
定義一個Controller,對外提供一個"/hello"的Rest API接口, 經過上面定義的Feign來調用服務提供者:
@RestController public class FeignController { private final FeignService feignService; @Autowired public FeignController(FeignService feignService) { this.feignService = feignService; } @GetMapping(value = "/hello") public String hello(@RequestParam("name") String name) { return feignService.hello(name); } }
在瀏覽器上屢次訪問 http://localhost:8080/hello?name=Mars :
Hello, My name is Mars, I'm from port: 8040 Hello, My name is Mars, I'm from port: 8041
這說明負載均衡器已經工做了。 下一節咱們講解熔斷的能力,敬請期待!
關注公衆號:JAVA九點半課堂,回覆【資料】獲取1T最新技術資料!