這篇文章給大家介紹Spring Cloud中怎么使用Ribbon實(shí)現(xiàn)負(fù)載均衡,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
成都創(chuàng)新互聯(lián)作為成都網(wǎng)站建設(shè)公司,專(zhuān)注重慶網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì),有關(guān)成都企業(yè)網(wǎng)站建設(shè)方案、改版、費(fèi)用等問(wèn)題,行業(yè)涉及成都混凝土攪拌站等多個(gè)領(lǐng)域,已為上千家企業(yè)服務(wù),得到了客戶(hù)的尊重與認(rèn)可。
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.
Ribbon是負(fù)載均衡客戶(hù)端,可以很好的控制HTTP和TCP客戶(hù)端的行為。 Feign已經(jīng)集成了Ribbon。
Spring Cloud Netflix默認(rèn)為ribbon(BeanType beanName:ClassName)提供以下bean:
IClientConfig
ribbonClientConfig: DefaultClientConfigImpl
IRule
ribbonRule: ZoneAvoidanceRule
IPing
ribbonPing: NoOpPing
ServerList
ribbonServerList: ConfigurationBasedServerList
ServerListFilter
ribbonServerListFilter: ZonePreferenceServerListFilter
ILoadBalancer
ribbonLoadBalancer: ZoneAwareLoadBalancer
這篇文章基于上一篇文章項(xiàng)目,啟動(dòng)eureka-server
項(xiàng)目;啟動(dòng)eureka-client
項(xiàng)目,端口為8040
; 將eureka-client
的配置文件端口改為8041
,并啟動(dòng),同一個(gè)項(xiàng)目修改端口號(hào),啟動(dòng)多個(gè)實(shí)例,只需要在IDEA中勾選Allow parallel run
:
此時(shí)你會(huì)發(fā)現(xiàn)注冊(cè)服務(wù)中注冊(cè)了兩個(gè)eureka-client
實(shí)例,相當(dāng)于起了2個(gè)節(jié)點(diǎn)的集群。 訪問(wèn)http://localhost:9090:
使用Spring Initializr
新建一個(gè)項(xiàng)目,取名為ribbon-service
, 在Spring Cloud Discovery中勾選Eureka Discovery Client
,在Spring Cloud Routing中勾選Ribbon
,在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 ribbon-service 0.0.1-SNAPSHOT ribbon-service Ribbon Service 1.8 Greenwich.SR3 org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-netflix-ribbon org.springframework.boot spring-boot-starter-web 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
ribbon-service配置服務(wù)中心地址、應(yīng)用名、端口,配置文件內(nèi)容:
server: port: 8050 spring: application: name: ribbon-service eureka: client: service-url: defaultZone: http://localhost:9090/eureka/
在項(xiàng)目啟動(dòng)類(lèi)上注解@EnableDiscoveryClient
, 開(kāi)啟向服務(wù)中心注冊(cè);定義一個(gè) RestTemplate
Bean, 并注解@LoadBalanced
開(kāi)啟負(fù)載均衡功能:
@EnableEurekaClient @SpringBootApplication public class RibbonServiceApplication { public static void main(String[] args) { SpringApplication.run(RibbonServiceApplication.class, args); } @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
寫(xiě)一個(gè)controller, 通過(guò)之前定義的RestTemplate
來(lái)消費(fèi)eureka-client
服務(wù)的/hello
接口,url
中使用應(yīng)用名,ribbon
會(huì)根據(jù)應(yīng)用名來(lái)選擇具體的服務(wù)實(shí)例,根據(jù)服務(wù)實(shí)例在請(qǐng)求的時(shí)候會(huì)用具體的url替換掉服務(wù)名:
@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); } }
在瀏覽器上多次訪問(wèn) 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
關(guān)于Spring Cloud中怎么使用Ribbon實(shí)現(xiàn)負(fù)載均衡就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。