SpringCloud微服務雲架構構建B2B2C電子商務平臺之-Eureka服務消費Feign
阿新 • • 發佈:2018-12-05
一、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已經註冊到服務中心
Honghu程式碼結構圖:
意瞭解框架技術或者原始碼的朋友直接求求交流分享技術:2147775633