當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot中使用Ribbon软负载
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot中使用Ribbon软负载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為什么80%的碼農都做不了架構師?>>> ??
問題
后臺服務依賴第三方服務,第三方服務是服務器集群的形式對外提供服務。導致Http客戶端需要配置多個ip地址來訪問第三方服務的問題。這個問題,這里是使用大Spring中內置的Ribbon客戶端。
build.gradle
ext {springCloudVersion = "Finchley.SR2" }dependencyManagement {imports {mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"} }dependencies {implementation "org.springframework.cloud:spring-cloud-starter-netflix-ribbon" }application.yml
src/main/resources/application.yml
say-hello:ribbon:eureka:enabled: falselistOfServers: 10.158.17.60:80,10.158.17.61:80ServerListRefreshInterval: 15000這里主要做了三件事情:
- 禁用ribbon中的eureka注冊
- listOfServers配置第三方服務器集群地址
- ServerListRefreshInterval第三方服務器集群數據刷新時間
Application.java
@SpringBootApplication @RibbonClient(name = "say-hello") public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}@LoadBalanced@BeanRestTemplate restTemplate(){return new RestTemplate();} }這里主要注意@RibbonClien和@LoadBalanced的配置:
- @RibbonClien主要配置第三方的調用的服務使用ribbon來請求
- @LoadBalanced告訴RestTemplate需要啟動負載均衡
使用
@RequestMapping("/hi")public String hi(@RequestParam(value="name", defaultValue="Artaban") String name) {String greeting = this.restTemplate.getForObject("http://say-hello/greeting", String.class);return String.format("%s, %s!", greeting, name);}參考
- Client Side Load Balancing with Ribbon and Spring Cloud
- 【web】Spring RestTemplate提交時設置http header請求頭
轉載于:https://my.oschina.net/fxtxz2/blog/3028647
總結
以上是生活随笔為你收集整理的Spring Boot中使用Ribbon软负载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第10章 流
- 下一篇: 刚刚,ACM宣布三位深度学习之父共同获得