您好,登錄后才能下訂單哦!
這篇文章給大家介紹Spring Cloud中怎么使用Feign實現負載均衡,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
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是一個聲明式的web服務客戶端,它使得寫web客戶端變得更簡單。想要使用Feign,只需要創建一個接口并注解它。Feign具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign還支持可插拔的編碼器和解碼器。Spring Cloud添加了對Spring MVC注釋的支持,并默認使用和Spring Web相同的HttpMessageConverters。當使用Feign時,Spring Cloud集成了Ribbon和Eureka以提供負載平衡的http客戶端。
<!-- more -->
簡而言之:
Feign 采用的是基于接口的注解
Feign 集成了ribbon,具有負載均衡的能力
集成了Hystrix,具有熔斷的能力
繼續在第一節項目的基礎上,啟動eureka-server,端口為9090;啟動兩個eureka-client, 端口為8040、8041。
使用Spring Initializr
新建一個項目,取名為feign-service
, 在Spring Cloud Discovery中勾選Eureka Discovery Client,在Spring Cloud Routing中勾選OpenFeign,在Web中勾選Spring Web:
創建成功后,項目pom.xml如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.noodles.mars</groupId> <artifactId>feign-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>feign-service</name> <description>Feign Service</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR3</spring-cloud.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> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</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>${spring-cloud.version}</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>
feign-service配置服務中心地址、應用名、端口,配置文件內容:
server: port: 8080 eureka: client: service-url: defaultZone: http://localhost:9090/eureka/ spring: application: name: feign-client
在項目啟動類上注解@EnableDiscoveryClient, 開啟向服務中心注冊;注解@EnableFeignClients開啟Feign功能:
@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class FeignServiceApplication { public static void main(String[] args) { SpringApplication.run(FeignServiceApplication.class, args); } }
定義一個feign接口,通過@FeignClient("服務應用名")
,來指定調用哪個服務。比如在代碼中調用了hello-erueka-client
服務的/hello
接口,代碼如下:
@FeignClient(value = "hello-eureka-client") public interface FeignService { @GetMapping(value = "/hello") String hello(@RequestParam(value = "name") String name); }
定義一個Controller,對外提供一個"/hello"的Rest API接口, 通過上面定義的Feign來調用服務提供者:
@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); } }
在瀏覽器上多次訪問 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
關于Spring Cloud中怎么使用Feign實現負載均衡就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。