公衆號:java樂園java
spring cloud的Netflix中提供了兩個組件實現軟負載均衡調用,分別是Ribbon和Feign。上一篇和你們一塊兒學習了Ribbon。
Ribbon :Spring Cloud Ribbon是基於HTTP和TCP的客戶端負載工具,它是基於Netflix Ribbon實現的,它能夠在客戶端配置 ribbonServerList(服務端列表),而後輪詢請求以實現均衡負載。
Feign :spring cloud feign 是一個使用起來更加方便的 HTTP 客戶端。 在使用ribbon時,一般會使用RestTemplate實現對http請求的封裝,造成了模板化的調用方法。spring cloud feign在此基礎上作了進一步的封裝,Feign是一種聲明式、模板化的HTTP客戶端。在Spring Cloud中使用Feign, 能夠作到使用HTTP請求遠程服務時能與調用本地方法同樣的編碼體驗,徹底感知不到這是遠程方法,更感知不到這是個HTTP請求。git
一、 新建項目sc-eureka-client-consumer-feign,對應的pom.xml文件以下web
<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>spring-cloud</groupId> <artifactId>sc-eureka-client-consumer-feign</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>sc-eureka-client-consumer-feign</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!-- 說明是一個 eureka client --> <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-web</artifactId> </dependency> <!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>1.4.5.RELEASE</version> </dependency> --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> </project>
備註:spring cloud 2.x後spring-cloud-starter-feign已經被標識爲過時,推薦使用spring-cloud-starter-openfeignspring
二、 新建spring boot啓動類ConsumerFeignApplication.javaapache
package sc.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableFeignClients public class ConsumerFeignApplication { public static void main(String[] args) { SpringApplication.run(ConsumerFeignApplication.class, args); } }
三、 建立配置文件bootstrap.yml和application.yml,對應的內容以下bootstrap
bootstrap.ymlapp
server: port: 5800
application.yml負載均衡
spring: application: name: sc-eureka-client-consumer-feign eureka: client: registerWithEureka: true #是否將本身註冊到Eureka服務中,默認爲true fetchRegistry: true #是否從Eureka中獲取註冊信息,默認爲true serviceUrl: defaultZone: http://localhost:5001/eureka/
四、 編寫feign客戶端maven
package sc.consumer.service; import java.util.Map; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import sc.consumer.model.User; @FeignClient(value="sc-eureka-client-provider") public interface UserService { @GetMapping("/user/getUser/{id}") Map<String, Object> getUser(@PathVariable(value ="id") Long id); @RequestMapping("/user/listUser") Map<String, Object> listUser(); @PostMapping("/user/addUser") Map<String, Object> addUser(@RequestBody User user); @PutMapping("/user/updateUser") Map<String, Object> updateUser(@RequestBody User user); @DeleteMapping("/user/deleteUser/{id}") Map<String, Object> deleteUser(@PathVariable(value ="id") Long id); }
五、 分別啓動註冊中心項目sc-eureka-server和服務提供者sc-eureka-client-provideride
六、 啓動項目sc-eureka-client-consumer-feign,並驗證是否啓動成功
方法一
方法二
七、 使用postman驗證
查詢:
http://127.0.0.1:5800/feign/user/getUser/4
列表:
http://127.0.0.1:5800/feign/user/listUser
添加:
http://127.0.0.1:5800/feign/user/addUser
更新:
http://127.0.0.1:5800/feign/user/updateUser
刪除:
http://127.0.0.1:5800/feign/user/deleteUser/6
備註:
sc-eureka-client-provider項目的UserController.java 須要修正
源碼
https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-eureka-client-consumer-feign