這篇文章給大家介紹Spring Cloud中怎么使用Feign實(shí)現(xiàn)負(fù)載均衡,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
目前創(chuàng)新互聯(lián)已為上千多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁(yè)空間、網(wǎng)站運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、阿克陶網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and for using the same HttpMessageConverters used by default in Spring Web. Spring Cloud integrates Ribbon and Eureka to provide a load balanced http client when using Feign.
Feign是一個(gè)聲明式的web服務(wù)客戶端,它使得寫(xiě)web客戶端變得更簡(jiǎn)單。想要使用Feign,只需要?jiǎng)?chuàng)建一個(gè)接口并注解它。Feign具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign還支持可插拔的編碼器和解碼器。Spring Cloud添加了對(duì)Spring MVC注釋的支持,并默認(rèn)使用和Spring Web相同的HttpMessageConverters。當(dāng)使用Feign時(shí),Spring Cloud集成了Ribbon和Eureka以提供負(fù)載平衡的http客戶端。
簡(jiǎn)而言之:
Feign 采用的是基于接口的注解
Feign 集成了ribbon,具有負(fù)載均衡的能力
集成了Hystrix,具有熔斷的能力
繼續(xù)在第一節(jié)項(xiàng)目的基礎(chǔ)上,啟動(dòng)eureka-server,端口為9090;啟動(dòng)兩個(gè)eureka-client, 端口為8040、8041。
使用Spring Initializr
新建一個(gè)項(xiàng)目,取名為feign-service
, 在Spring Cloud Discovery中勾選Eureka Discovery Client,在Spring Cloud Routing中勾選OpenFeign,在Web中勾選Spring Web:
創(chuàng)建成功后,項(xiàng)目pom.xml如下:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.9.RELEASE com.noodles.mars feign-service 0.0.1-SNAPSHOT feign-service Feign Service 1.8 Greenwich.SR3 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-openfeign org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
feign-service配置服務(wù)中心地址、應(yīng)用名、端口,配置文件內(nèi)容:
server: port: 8080 eureka: client: service-url: defaultZone: http://localhost:9090/eureka/ spring: application: name: feign-client
在項(xiàng)目啟動(dòng)類上注解@EnableDiscoveryClient, 開(kāi)啟向服務(wù)中心注冊(cè);注解@EnableFeignClients開(kāi)啟Feign功能:
@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class FeignServiceApplication { public static void main(String[] args) { SpringApplication.run(FeignServiceApplication.class, args); } }
定義一個(gè)feign接口,通過(guò)@FeignClient("服務(wù)應(yīng)用名")
,來(lái)指定調(diào)用哪個(gè)服務(wù)。比如在代碼中調(diào)用了hello-erueka-client
服務(wù)的/hello
接口,代碼如下:
@FeignClient(value = "hello-eureka-client") public interface FeignService { @GetMapping(value = "/hello") String hello(@RequestParam(value = "name") String name); }
定義一個(gè)Controller,對(duì)外提供一個(gè)"/hello"的Rest API接口, 通過(guò)上面定義的Feign來(lái)調(diào)用服務(wù)提供者:
@RestController public class FeignController { private final FeignService feignService; @Autowired public FeignController(FeignService feignService) { this.feignService = feignService; } @GetMapping(value = "/hello") public String hello(@RequestParam("name") String name) { return feignService.hello(name); } }
在瀏覽器上多次訪問(wèn) http://localhost:8080/hello?name=Mars :
Hello, My name is Mars, I'm from port: 8040 Hello, My name is Mars, I'm from port: 8041
關(guān)于Spring Cloud中怎么使用Feign實(shí)現(xiàn)負(fù)載均衡就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。