Feign是一個聲明式的web service客戶端,它使得編寫web service客戶端更爲容易。創建接口,爲接口添加註解,即可使用Feign。Feign可以使用Feign註解或者JAX-RS註解,還支持熱插拔的編碼器和解碼器。Spring Cloud爲Feign添加了Spring MVC的註解支持,並整合了Ribbon和Eureka來爲使用Feign時提供負載均衡。
啓動註冊中心和服務提供者
https://blog.csdn.net/miaodichiyou/article/details/104160284
1、啓動SpringCloud高可用服務註冊中心(Eureka)搭建的註冊中心
2、啓動三個springcloud-eureka-provider實例,端口分別爲8001,8002和8003
創建服務消費者
添加依賴包文件POM.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.springcloud</groupId>
<artifactId>springcloud-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>springcloud-feign</artifactId>
<name>springcloud-feign</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置application.yml文件
spring:
application:
name: springcloud-feign
freemarker:
prefer-file-system-access: false
security:
user:
name: admin
password: 123456
server:
port: 8101
eureka:
instance:
hostname: eureka-feign.com
instance-id: eureka-feign
client:
service-url:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer1.com:8897/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer2.com:8898/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer3.com:8899/eureka/
修改C:\Windows\System32\drivers\etc\hosts
127.0.0.1 eureka-feign.com
添加服務消費者啓動類
-
FeignApplication.java
在程序的啓動類加上註解@EnableFeignClients開啓Feign Client功能
package org.springcloud.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
新建接口
-
FeignConsumer.java
新建一個FeignConsumer的接口:
- 通過@FeignClient(“服務名”),來指定調用哪個服務。value爲遠程調用其他服務的服務名,FeignConfig.class爲配置類。
- 在FeignConsumer內部有一個sayHiFromEurekaProvider()的方法,該方法通過Feign來調用springcloud-eureka-provider服務的“/hi”的api接口。
package org.springcloud.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Component
/*指定這個接口所要調用的提供者服務名稱 */
@FeignClient(value = "springcloud-eureka-provider",configuration = FeignConfig.class)
public interface FeignConsumer {
@GetMapping(value = "/hi")
String sayHiFromEurekaProvider(@RequestParam(value = "name")String name);
}
添加配置
-
FeignConfig .java
在FeignConfig類上加上@Configuration註解,表明這是一個配置類,並注入一個BeanName爲feignRetryer的Retryer的Bean。可使feign在遠程調用失敗後會進行重試。
package org.springcloud.feign;
import java.util.concurrent.TimeUnit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.Retryer;
@Configuration
public class FeignConfig {
@Bean
public Retryer feignRetryer(){
return new Retryer.Default(100,TimeUnit.SECONDS.toMillis(1),5);
}
}
添加service消費服務提供者實例提供的服務
-
FeignService .java
Service層FeignService類注入FeignConsumer的Bean
package org.springcloud.feign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class FeignService {
@Autowired
FeignConsumer feignConsumer;
public String hi(String name){
return feignConsumer.sayHiFromEurekaProvider(name);
}
}
創建controller,調用service中的方法
-
FeignController.java
FeignController類調用FeignService的hi()方法,FeignService通過FeignConsumer遠程調用springcloud-eureka-provider服務的API接口“/hi”。
package org.springcloud.feign;
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;
/*調用提供者的home方法*/
@RestController
public class FeignController {
@Autowired
FeignService feignService;
@GetMapping("/hi")
public String hi(@RequestParam(defaultValue = "zhaojq",required = false)String name){
return feignService.hi(name);
}
}
總結
Feign源碼實現過程如下:
- 首先通過@EnableFeignClients註解開啓FeignClient功能。只有這個註解的存在,程序啓動時纔會開啓對@FeignConfig註解的包的掃描,
- 根據Feign的規則實現接口,並在接口上面加上@FeignClient註解。
- 程序啓動後會進行包掃描,掃描所有@FeignClient註解的類,並將這些信息注入IOC容器中。
- 當接口的方法被調用時,通過JDK的代理來生成具體的RequestTemplate模板對象。
- 根據RequestTemplate再生成Http請求的Request對象。
- Request對象交給Client去處理,其中Client的網絡請求框架可以是HttpURLConnection、HttpClient和OkHttp。
- 最後Client被封裝到LoadBalanceClient類,這個類結合類Ribbon做到了負載均衡。
啓動springcloud-feign工程
查看Eureka註冊中心
運行啓動類,訪問http://eureka-peer1.com:8897/
http://eureka-peer2.com:8898/,http://eureka-peer3.com:8899/ 結果同上。
服務消費者實例已成功註冊。
瀏覽器多次訪問http://eureka-feign.com:8101/hi
在命令窗口curl http://eureka-feign.com:8101/hi,發現Feign已經實現負載均衡