SpringCloud系列研究---Eureka服務消費Feign
一、Feign簡介
Feign是一種聲明式、模板化的HTTP客戶端。這使得Web服務客戶端的寫入更加方便 要使用Feign創建一個界面並對其進行註釋。它具有可插入註釋支持,包括Feign註釋和JAX-RS註釋。Feign還支持可插拔編碼器和解碼器。Spring Cloud增加了對Spring MVC註釋的支持,並使用Spring Web中默認使用的HttpMessageConverters。Spring Cloud集成Ribbon和Eureka以在使用Feign時提供負載均衡的http客戶端。這段話來源於官方文檔,說白了就是通過Feign來調用Rest接口,而無需使用其他HTTP訪問組件,並且同時還提供了負載均衡、編解碼等功能,使用起來很方便。
二、環境介紹
首先在A服務器上啟動Eureka服務,然後在B、C兩臺服務器上分別啟動ms-demo-provider服務,這裏也可以部署在一臺服務器上采用不同的端口。訪問Eureka界面查看服務註冊狀態,之後在本地新建客戶端調用工程進行測試。此時A為註冊中心,B、C分別為服務提供者(提供相同的接口),本地工程為服務消費者。
三、項目代碼
在Idea中創建maven工程,ms-eurekaclient-demo工程代碼結構如下:
1: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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cloud.microservice</groupId> <artifactId>ms-eurekaclient-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>ms-eurekaclient-demo</name> <description>Demo project forSpring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.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>Edgware.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <!-- 加入斷路器依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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>
2:application.properties中的配置信息如下:
spring.application.name=ms-eurekaclient-demo server.port=9800 # 註冊中心地址 eureka.client.serviceUrl.defaultZone=http://xx.xx.xx.xx:9000/eureka/ # Indicates whether this client should fetch eureka registry information from eureka server # 客戶端是否要從eureka server獲取註冊信息,默認為true eureka.client.fetchRegistry=true # Indicates how often(in seconds) to fetch the registry information from the eureka server # 從eureka server獲取註冊信息的頻率,默認為30秒,縮短配置時間可以緩解服務上線時間過長的問題 eureka.client.registryFetchIntervalSeconds=10
3:在入口類Application中增加@EnableEurekaClient和@EnableFeignClients的註解
- @EnableEurekaClient:註解用來標識開啟服務發現功能,據說也可使用@EnableDiscoveryClient,網上有人說@EnableEurekaClient本身就是用@EnableDiscoveryClient來實現的,這點沒仔細研究過
- @EnableFeignClients:註解用來開啟Feign功能
package com.cloud.microservice.eurekaclientdemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableFeignClients public class FeignDemoApplication { public static void main(String[] args) { SpringApplication.run(FeignDemoApplication.class, args); } }
4:創建IUserFeignServiceClient接口類,代碼如下:
package com.cloud.microservice.eurekaclientdemo.FeignClient; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @FeignClient("ms-demo-provider") public interface IUserFeignServiceClient { //Feign定義服務提供者接口 @RequestMapping(value = "/demo/user/1.0/findAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"}) String findAll(); }
5:創建IUserService接口類和UserServiceImp實現類,代碼如下:
IUserService接口類如下:
package com.cloud.microservice.eurekaclientdemo.FeignClient; public interface IUserService { String findAll(); }
UserServiceImp實現類如下:
package com.cloud.microservice.eurekaclientdemo.FeignClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImp implements IUserService{ @Autowired private IUserFeignServiceClient userFeignServiceClient; public String findAll() { return "Feign: " + userFeignServiceClient.findAll(); } }
6:Rest接口定義,代碼如下:
package com.cloud.microservice.eurekaclientdemo.FeignClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class FeignDemoController { @Autowired private IUserService userService; @RequestMapping(value = "/feigndemo/findAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"}) public String feignDemo() { return userService.findAll(); } }
啟動工程,然後刷新Eureka界面,可以看到Feign已經註冊到服務中心
四、運行測試
打開瀏覽器,訪問ms-eurekaclient-demo中的接口,地址:http://localhost:9800/feigndemo/findAll,返回結果如下:
以上返回結果說明接口調用成功。同時我們也可以登陸到服務提供者的服務器上查看log。
通過多次訪問http://localhost:9800/feigndemo/findAll這個接口,可以看到B、C兩臺服務器上的provider中都有接口被調用的記錄。
SpringCloud系列研究---Eureka服務消費Feign