您好,登錄后才能下訂單哦!
這篇文章給大家介紹Spring Cloud中怎么使用Ribbon實現負載均衡,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.
<!-- more -->
Ribbon是負載均衡客戶端,可以很好的控制HTTP和TCP客戶端的行為。 Feign已經集成了Ribbon。
Spring Cloud Netflix默認為ribbon(BeanType beanName:ClassName)提供以下bean:
IClientConfig
ribbonClientConfig: DefaultClientConfigImpl
IRule
ribbonRule: ZoneAvoidanceRule
IPing
ribbonPing: NoOpPing
ServerList<Server>
ribbonServerList: ConfigurationBasedServerList
ServerListFilter<Server>
ribbonServerListFilter: ZonePreferenceServerListFilter
ILoadBalancer
ribbonLoadBalancer: ZoneAwareLoadBalancer
這篇文章基于上一篇文章項目,啟動eureka-server
項目;啟動eureka-client
項目,端口為8040
; 將eureka-client
的配置文件端口改為8041
,并啟動,同一個項目修改端口號,啟動多個實例,只需要在IDEA中勾選Allow parallel run
:
此時你會發現注冊服務中注冊了兩個eureka-client
實例,相當于起了2個節點的集群。 訪問http://localhost:9090:
使用Spring Initializr
新建一個項目,取名為ribbon-service
, 在Spring Cloud Discovery中勾選Eureka Discovery Client
,在Spring Cloud Routing中勾選Ribbon
,在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>ribbon-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ribbon-service</name> <description>Ribbon Service</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR3</spring-cloud.version> </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-netflix-ribbon</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> </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>
ribbon-service配置服務中心地址、應用名、端口,配置文件內容:
server: port: 8050 spring: application: name: ribbon-service eureka: client: service-url: defaultZone: http://localhost:9090/eureka/
在項目啟動類上注解@EnableDiscoveryClient
, 開啟向服務中心注冊;定義一個 RestTemplate
Bean, 并注解@LoadBalanced
開啟負載均衡功能:
@EnableEurekaClient @SpringBootApplication public class RibbonServiceApplication { public static void main(String[] args) { SpringApplication.run(RibbonServiceApplication.class, args); } @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
寫一個controller, 通過之前定義的RestTemplate
來消費eureka-client
服務的/hello
接口,url
中使用應用名,ribbon
會根據應用名來選擇具體的服務實例,根據服務實例在請求的時候會用具體的url替換掉服務名:
@RestController public class HelloController { private final RestTemplate restTemplate; @Autowired public HelloController(RestTemplate restTemplate) { this.restTemplate = restTemplate; } @GetMapping("/hello") public String hello(@RequestParam("name") String name) { return restTemplate.getForObject("http://HELLO-ERUEKA-CLIENT/hello?name=" + name, String.class); } }
在瀏覽器上多次訪問 http://localhost:8050/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中怎么使用Ribbon實現負載均衡就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。