一、什么是Ribbon:
Ribbon是Netflix發布的開源專案,主要功能是提供客戶端的軟體負載均衡演算法,默認是使用輪詢,
方法1:可以在springboot啟動類之外定義一個配置類(@RibbonClient)配置對應的演算法,
方法2:在組態檔yml中配置
相關鏈接:https://www.cnblogs.com/fengyuduke/p/10712569.html
怎么使用Ribbon實作負載均衡?
相關鏈接: https://www.cnblogs.com/xing-12/p/9889153.html
1.你要有兩個服務,一個是服務消費方,一個是服務提供方,并且服務提供方要有兩個實體,也就是xing-user有兩個實體,分別運行在8070和8071埠,
2.所有服務注冊到eureka server中,
3.通過注入bean: 用RestTemplate呼叫restTemplate.getForObject("http://xing-user/user/findByName/"+name, User.class)方法,
二、Eureka服務提供者集群
先啟動上篇文章中的注冊中心eureka-server:8001, 以及hello-service:8011,接著修改hello-service專案組態檔中的埠,改成8012,再次運行用戶服務專案,
執行成功,查看Eureka注冊中心,可以看到有用戶服務應用有兩個埠對應,
(如果是IDEA用戶,需要修改Application.java的執行方式,默認是單實體運行,所以你在運行8011的專案,修改埠無法再次運行,)

三、RestTemplate+Ribbon消費者: 新建一個maven服務

pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
application.properties:
spring.application.name=hello-consumer-ribbon server.port=8021 eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/spring.application.name=hello-consumer-ribbon server.port=8021 eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/
啟動類:
package cn.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.Bean; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableDiscoveryClient @RestController public class HelloConsumerRibbonApplication { public static void main(String[] args) { SpringApplication.run(HelloConsumerRibbonApplication.class, args); } @Bean @LoadBalanced public RestTemplate restTemplate () { return new RestTemplate(); } @Autowired private RestTemplate restTemplate; //獲取服務實體,作用為之后console顯示效果;"http://USER-SERVICE/hello?name= 標識注冊的服務,USER-SERVICE在eureka注冊的服務名 @RequestMapping("hello") public ResponseEntity<String> hello (String name) { return restTemplate.getForEntity("http://USER-SERVICE/hello?name=" + name, String.class); } }
四、測驗
測驗服務消費
http://localhost:8021/hello?name=ribbon

測驗負載均衡:
我們在hello-service hello方法中加上日志列印,然后再分別啟動 hello-service:8012,8002,然后多次訪問 http://localhost:8021/hello?name=ribbon,可以看到兩個hello-service專案的控制臺都有日志輸出,表示實作了負載均衡,
使用RestTemplate+Ribbon必須指定呼叫服務名稱,如上面的HELLO-SERVICE,為了方便使用,SpringCloud還集成了Feign消費方式,
————————————————
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/42070.html
標籤:架構設計
上一篇:網站架構優化性能
下一篇:資料結構導論(第二章線性表)
