Spring cloud微服務架構-feign(負載均衡)

Spring cloud微服務架構-feign(負載均衡)

前言

Feign現在是Spring Cloud OpenFeign的獨立模塊,Feign和Ribbon一樣,也是一個客戶端負載均衡器,客戶端負載均衡和服務器負載均衡有著本質的區別,後面文章詳說這個。Feign在Ribbon的基礎上做了封裝,所以Feign負載均衡c策略也是使用Ribbon配置的。

Feign 是一個聲明式的web服務客戶端,讓我們更容易的編寫開發負載均衡客戶端。我們通過創建一個接口並用註解標註它就可以使用Feign。同時它有可插撥式的註解支持,包括Feign註解和 JAX-RS 註解。Feign也支持可插撥式編解碼,Feign同時支持SpringMVC 的所有特性以及數據轉換等。當我們使用Feign是,它集成了Ribbon和Eureka來支持Http的負載均衡客戶端。

Feign的使用

以之前文章的工程為基礎,我們創建一個 Feign-service 模塊,以 eureka-client 作為服務提供者。

pom文件引入相關jar

<dependency>
<groupid>org.springframework.cloud/<groupid>
<artifactid>spring-cloud-starter-openfeign/<artifactid>
/<dependency>
<dependency>
<groupid>org.springframework.cloud/<groupid>
<artifactid>spring-cloud-starter-netflix-ribbon/<artifactid>
/<dependency>
<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>

配置application.yml

server:
port: 8769
spring:
application:
name: feign-service
eureka:
client:
serviceUrl:
defaultZone: http://eureka-server1:8761/eureka/

啟動類

@SpringBootApplication
@EnableFeignClients
public class FeignServiceApplication {
public static void main(String[] args) {
SpringApplication.run(FeignServiceApplication.class, args);
}
}

@EnableFeignClients 啟動Feign註解,如果我們要指定掃描特定包下的feign客戶端,可以這樣

@EnableFeignClients(basePackages = {xx,xx})

接口Feign客戶端

@FeignClient( "eureka-client")
public interface EurekaCleintApi {
@GetMapping("/feignhello")
String feignHello(@RequestParam("name")String name);
}

eureka-client是服務提供者在Eureka的註冊名,值得注意的是,傳遞參數必須加上參數註解,不然會報錯。

調用的controller

@RestController
public class FeignController {
@Autowired
private EurekaCleintApi eurekaCleintApi;
@GetMapping("/testFeign")
public String testFeign(String name){
return eurekaCleintApi.feignHello(name);
}
}

經過以上幾步,Feign負載均衡客戶端整合完成

Feign定製配置文件覆蓋默認配置

Feign有一份默認的配置寫在 FeignClientsConfiguration,假如像上面我們什麼Feign配置都沒做的話,Feign就會用這份默認配置,但是我們應用往往會加入很多個Feign虛擬客戶端,但不一定都使用默認的 FeignClientsConfiguration

Spring Cloud的Feign支持中的中心概念是指定客戶端的概念。每個虛擬客戶端都是組件集合的一部分,這些組件可以一起工作以按需聯繫遠程服務器,並且該集合的名稱是使用@FeignClient註解作為應用程序開發人員提供的。Spring Cloud ApplicationContext使用來為每個命名客戶端按需創建一個新集合 FeignClientsConfiguration。FeignClientsConfiguration包含了 a feign.Decoder,a feign.Encoder和a feign.Contract。通過使用批註的contextId 屬性,可以覆蓋該集合的名稱@FeignClient。

上面的接口客戶端我們可以改成

@FeignClient(contextId = "eurekaCleintApi",name = "eureka-client",configuration = FeignConfig.class)
public interface EurekaCleintApi {

@GetMapping("/feignhello")
String feignHello(@RequestParam("name")String name);
}

然後我們的FeignConfig

@Configuration

public class FeignConfig {

}

可以在FeignConfig 定義我們自己的 Decoder,Encoder,Contract 等等

注意:按照官方的說法,FeignConfig應該排除在@ComponentScan的掃描範圍之外

第二種方式配置Feign

假如我們不想再接口加註解的方式定義客戶端,官方提供的第二種辦法

EurekaCleintApi的註解去掉,FeignController改成

@RestController
public class FeignController {
private EurekaCleintApi eurekaCleintApi;
@Autowired
public FeignController(Decoder decoder, Encoder encoder, Client client, Contract contract) {
this.eurekaCleintApi = Feign.builder().client(client)
.encoder(encoder)
.decoder(decoder)
.contract(contract)
.target(EurekaCleintApi.class, "http://eureka-client");
}
@GetMapping("/testFeign")
public String testFeign(String name){
return eurekaCleintApi.feignHello(name);
}
}
Decoder decoder, Encoder encoder, Client client, Contract contract 這幾個參數都是默認配置好的,不用擔心找不到

Feign請求響應壓縮

有時候為了傳輸的效率,在傳輸之前我們需要將我們請求和響應的數據進行壓縮,我們通過如下的配置來啟用

feign.compression.request.enabled=true
feign.compression.response.enabled=true
feign.compression.request.enabled=true
feign.compression.request.mime-types=text/xml,application/xml,application/json
feign.compression.request.min-request-size=2048

代碼沒有完全貼出來,具體完成代碼如下

代碼github地址:https://github.com/Chandgaochengdong/spring-cloud-sample


分享到:


相關文章: