上一篇文章,講述瞭如何通過RestTemplate+Ribbon去消費服務,這篇文章主要講述如何通過Feign去消費服務。
1.Feign概述
Feign是一個聲明式的僞Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要創建一個接口並註解。它具有可插拔的註解特性,可使用Feign 註解和JAX-RS註解。Feign支持可插拔的編碼器和解碼器。Feign默認集成了Ribbon,並和Eureka結合,默認實現了負載均衡的效果。
簡而言之:
Feign 採用的是基於接口的註解
Feign 整合了ribbon
2.準備工作
繼續用上一節的工程, 啓動netflix-eureka-server,端口爲8888; 啓動netflix-eureka-provider兩次,端口分別爲8889、9000.
3.創建一個feign的服務
新建一個spring-boot工程,取名爲serice-feign,在它的pom文件引入Feign的起步依賴spring-cloud-starter-feign、Eureka的起步依賴spring-cloud-starter-eureka、Web的起步依賴spring-boot-starter-web,代碼如下:
<?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.springcloud</groupId>
<artifactId>service-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-feign</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.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>
</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>
<!--引入feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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>Finchley.RELEASE</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>
在工程的配置文件application.yml文件,指定程序名爲service-feign-consumer,端口號爲9003,服務註冊地址爲http://localhost:8888/eureka/ ,代碼如下
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka/
server:
port: 9003
spring:
application:
name: service-feign-consumer
在程序的啓動類ServiceFeignApplication上加 ,@EnableDiscoveryClient //向服務中心註冊;並且向程序ioc注入一個bean和@EnableFeignClients //開啓feign的功能的註解
package com.springcloud.servicefeign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient //向服務中心註冊;並且向程序ioc注入一個bean
@EnableFeignClients //開啓feign的功能
public class ServiceFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceFeignApplication.class, args);
}
}
定義一個feign接口,通過@ FeignClient(“服務名”),來指定調用哪個服務。比如在代碼中調用了service-cart服務的“/hi”接口,代碼如下:
package com.springcloud.servicefeign.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @PACKAGE_NAME com.springcloud.servicefeign.feign
* @PROJECT_NAME service-feign
* @創建人 huang
* @創建時間 2018/11/27
*/
@FeignClient(value = "service-cart")
public interface SchedualServiceHi {
/**
* 定義一個feign接口,通過@ FeignClient(“服務名”),來指定調用哪個服務。
* 比如在代碼中調用了service-cart服務的“/hi”接口
*/
@GetMapping(value = "/hi")
String sayHiFromClientOne(@RequestParam(value = "name", defaultValue = "黃世民") String name);
}
在Web層的controller層,對外暴露一個”/hi”的API接口,通過上面定義的Feign客戶端SchedualServiceHi 來消費服務。代碼如下:
package com.springcloud.servicefeign.controller;
import com.springcloud.servicefeign.feign.SchedualServiceHi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @PACKAGE_NAME com.springcloud.servicefeign.controller
* @PROJECT_NAME service-feign
* @創建人 huang
* @創建時間 2018/11/27
*/
@RestController
public class HiController {
@Autowired
private SchedualServiceHi schedualServiceHi;
@GetMapping(value = "/hi")
public String sayHi(@RequestParam("name") String name) {
return schedualServiceHi.sayHiFromClientOne(name);
}
}
啓動程序,多次訪問http://localhost:9003/hi?name=hsm,瀏覽器交替顯示
hihsm,i am from port:9000
hihsm,i am from port:8889
4.參考資料
spring-cloud-feign