为什么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软负载相关推荐

  1. Spring Cloud:使用Ribbon实现负载均衡详解(下)

    在上一篇文章(Spring Cloud:使用Ribbon实现负载均衡详解(上))中,我对 Ribbon 做了一个介绍,Ribbon 可以实现直接通过服务名称对服务进行访问.这一篇文章我详细分析一下如何 ...

  2. Spring Boot 中使用 MongoDB 增删改查

    本文快速入门,MongoDB 结合SpringBoot starter-data-mongodb 进行增删改查 1.什么是MongoDB ? MongoDB 是由C++语言编写的,是一个基于分布式文件 ...

  3. Spring Cloud Alibaba - 11 Ribbon 自定义负载均衡策略(同集群优先权重负载均衡算法)

    文章目录 Pre 需求 工程 Code 继承AbstractLoadBalancerRule实现自定义Rule 随机权重策略 配置 验证 源码 Pre Spring Cloud Alibaba - 0 ...

  4. flyway spring_关于使用Flyway在Spring Boot中管理数据更改的建议

    flyway spring 介绍 (Introduction) Database migrations are essential for the development of an applicat ...

  5. java调用r实例,Spring Boot中使用RSocket的示例代码

    1. 概述 RSocket 应用层协议支持Reactive Streams 语义, 例如:用RSocket作为HTTP的一种替代方案.在本教程中, 我们将看到RSocket 用在spring boot ...

  6. 再谈Spring Boot中的乱码和编码问题

    编码算不上一个大问题,即使你什么都不管,也有很大的可能你不会遇到任何问题,因为大部分框架都有默认的编码配置,有很多是UTF-8,那么遇到中文乱码的机会很低,所以很多人也忽视了. Spring系列产品大 ...

  7. 【spring boot2】第8篇:spring boot 中的 servlet 容器及如何使用war包部署

    嵌入式 servlet 容器 在 spring boot 之前的web开发,我们都是把我们的应用部署到 Tomcat 等servelt容器,这些容器一般都会在我们的应用服务器上安装好环境,但是 spr ...

  8. Spring Boot 中使用@Async实现异步调用,加速任务执行!

    欢迎关注方志朋的博客,回复"666"获面试宝典 什么是"异步调用"?"异步调用"对应的是"同步调用",同步调用指程序按照 ...

  9. 徒手解密 Spring Boot 中的 Starter自动化配置黑魔法

    我们使用 Spring Boot,基本上都是沉醉在它 Stater 的方便之中.Starter 为我们带来了众多的自动化配置,有了这些自动化配置,我们可以不费吹灰之力就能搭建一个生产级开发环境,有的小 ...

最新文章

  1. static成员函数不能调用non-static成员函数
  2. jvm 什么是对象头,里面有什么
  3. Spring源码解析-核心类之XmlBeanDefinitionReader
  4. FlexiBO:基于成本感知的深度神经网络多目标优化
  5. Delphi2009下编译提示“无法找到“Excel_TLB”
  6. css img重复_20 个 CSS 快速提升技巧
  7. iTOP-4412开发板实现3路ADC数模转换驱动例程
  8. android获取图片格式,Android得到图片的真实格式——从本地文件或者网络文件流...
  9. mac mail 删除邮件服务器,如何从Mac OS X中的邮件中删除所有电子邮件 | MOS86
  10. 零基础入门专利代理考试需要了解的,持续更新ing
  11. matlab void函数,MATLAB基本函数
  12. php有空语句吗?,php是空还是空?
  13. 《三国志·战略版》爆火,友盟+助力手游实现高效促活
  14. 从事嵌入式软件开发的好处是什么
  15. 陈怡暖:非农克星LMCI数据今晚出炉
  16. 使用超级用户登录系统linux,登录和退出Linux系统
  17. 插入U盘弹出不了的问题解决
  18. 设计到生产:流程名词解释
  19. 【STemWin】STM32F429IG单片机用LTDC驱动正点原子7寸RGB彩色触摸屏,并裸机移植STemWin图形库
  20. 基于Qt的多人实时桌面共享

热门文章

  1. 解决ImportError: cannot import name ‘imread‘ from ‘scipy.misc‘
  2. ValueError: slice index xxxx of dimension 0 out of bounds,详细分析。
  3. pygame中Rect(left, top, width, height)的参数详解
  4. 8086CPU常见汇编指令、debug命令
  5. [数据结构] 二叉树基础
  6. [BUUCTF-pwn]——picoctf_2018_got_shell
  7. Linux下程序的保护机制(checksec)
  8. STL-String源码分析
  9. Vitis学习记录(一)
  10. OpenSessionInViewFilter作用及配置