使用配置文件自定义Ribbon配置
1、application.yml——Ribbon配置文件
debug: false
spring:
application:
name: mcc-ribbon-properties
cloud:
consul:
discovery:
instanceId: ${spring.application.name}:${server.port}
host: localhost
port: 8500
config:
enabled: true #false禁用Consul配置,默认true
format: YAML # 表示consul上面文件的格式 有四种 YAML PROPERTIES KEY-VALUE FILES
#data-key: configuration #表示consul上面的KEY值(或者说文件的名字) 默认是data
data-key: data #表示consul上面的KEY值(或者说文件的名字) 默认是data
#prefix设置配置值的基本文件夹
#defaultContext设置所有应用程序使用的文件夹名称
#profileSeparator设置用于使用配置文件在属性源中分隔配置文件名称的分隔符的值
server:
port: 8804 #预加载配置,默认为懒加载
ribbon:
eager-load:
enabled: true
clients: mima-cloud-producer,mima-cloud-producer2
#这里使用服务提供者的instanceName
mima-cloud-producer:
ribbon:
# 代表Ribbon使用的负载均衡策略
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
# 每台服务器最多重试次数,但是首次调用不包括在内, Max number of retries on the same server (excluding the first try)
MaxAutoRetries: 1
# 最多重试多少台服务器,Max number of next servers to retry (excluding the first server)
MaxAutoRetriesNextServer: 1
# 无论是请求超时或者socket read timeout都进行重试,Whether all operations can be retried for this client
OkToRetryOnAllOperations: true
# Interval to refresh the server list from the source
ServerListRefreshInterval: 2000
# Connect timeout used by Apache HttpClient
ConnectTimeout: 3000
# Read timeout used by Apache HttpClient
ReadTimeout: 3000
mima-cloud-producer2:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.ZoneAvoidanceRule
2、RibbonConsumerApplication——Ribbon启动类
package com.mimaxueyuan.consumer.robbin; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableDiscoveryClient
public class RibbonConsumerApplication { @Bean
@LoadBalanced // 需要使用负载均衡,必须与Bean一同使用
public RestTemplate balanceRestTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
}
}
3、RibbonController——Ribbon测试类
package com.mimaxueyuan.consumer.robbin.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @RestController
public class RibbonController { @Autowired
private RestTemplate balanceRestTemplate; // 以下注入负载均衡客户端LoadBalancerClient是一个接口,下面只有一个RibbonLoadBalancerClient实现类
@Autowired
private LoadBalancerClient loadBalancerClient;
@Autowired
private RibbonLoadBalancerClient ribbonLoadBalancerClient; // 基于properties的ribbon使用展示
@GetMapping("/ribbon/get1")
public String eureka() {
ServiceInstance instance = loadBalancerClient.choose("mima-cloud-producer");
System.out.println("host:" + instance.getHost() + ",port:" + instance.getPort() + ",serviceId=" + instance.getServiceId() + ",uri=" + instance.getUri());
return "/ribbon/get1's demo, please to see console output";
} // 基于properties的ribbon使用展示
@GetMapping("/ribbon/get2")
public String get2() {
ServiceInstance instance = loadBalancerClient.choose("mima-cloud-producer2");
System.out.println("host:" + instance.getHost() + ",port:" + instance.getPort() + ",serviceId=" + instance.getServiceId() + ",uri=" + instance.getUri());
return "/ribbon/get2's demo, please to see console output";
} }
使用配置文件自定义Ribbon配置的更多相关文章
- SpringCloud系列五:Ribbon 负载均衡(Ribbon 基本使用、Ribbon 负载均衡、自定义 Ribbon 配置、禁用 Eureka 实现 Ribbon 调用)
1.概念:Ribbon 负载均衡 2.具体内容 现在所有的服务已经通过了 Eureka 进行了注册,那么使用 Eureka 注册的目的是希望所有的服务都统一归属到 Eureka 之中进 行处理,但是现 ...
- 使用Java代码自定义Ribbon配置
很多场景下,需要实现不同的微服务采用不同的策略,例如修改Ribbon的负载均衡规则等.Spring Cloud允许使用Java代码自定义Ribbon的配置. 在Spring Cloud中,Ribbon ...
- SpringCloud系列八:自定义Ribbon配置
1. 回顾 上文使用Ribbon实现了客户端侧的负载均衡.但是很多场景下,我们可能需要自定义Ribbon的配置,比如修改Ribbon的负载均衡规则. Spring Cloud允许使用Java代码或属性 ...
- 0404-服务注册与发现-客户端负载均衡-两种自定义方式-Ribbon通过代码自定义配置、使用配置文件自定义Ribbon Client
一.官方文档解读 官方地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_cust ...
- spring cloud中通过配置文件自定义Ribbon负载均衡策略
一.Ribbon中的负载均衡策略 1.Ribbon中支持的负载均衡策略 AvailabilityFilteringRule:过滤掉那些因为一直连接失败的被标记为circuit tripped的后端se ...
- cloud server ribbon 自定义策略配置
虽然ribbon默认为我们提供了多钟负载均衡策略,但有时候我们仍然需要自定义符合自身业务逻辑的规则 使用配置文件的方式:我们只需要在配置文件中添加配置 serviceId.ribbon.NFLoadB ...
- 【SpringCloud】Ribbon如何自定义客户端配置和全局配置
起因 事情的起因是这样的,公司内部要实现基于Zuul网关的灰度路由,在上线时进行灰度测试,故需要配置业务微服务向Eureka注册的metadata元数据,和自定义Ribbon的负载规则达到只访问灰度服 ...
- springcloud(六)-Ribbon配置自定义算法
前言 很多场景下,可能根据需要自定义Ribbon的配置,例如修改Ribbon的负载均衡规则等.Spring Cloud Edgware允许使用java代码或属性自定义Ribbon 的配置,两种方式等价 ...
- java的自定义配置文件统一读取配置类示例
前言:在我们的日常编程中难免会有些我们自定义的配置,虽然Java中提供了很多的读取配置文件的方法,但是当我们需要修改配置文件的key的时候,就会发现太过散乱了,工作量也会很大,涉及的文件还很多,一不小 ...
随机推荐
- Swift 从OC入门
前言:断断续续学了蛮多天的Swift,感触还是蛮多的~~ 1.OC是以C语言为基础的语言,学习之前有C语言的基础,所以对于 `*` 这些指针还是比较容易理解的.问过其他程序员,对OC里面的 `*` 指 ...
- Sonar+maven+jenkins集成,Java代码走查
Sonar服务在Sonar安装与使用篇已经介绍过,此文章不再说了 Jenkins的安装与配置方法参考http://www.cnblogs.com/chenchen-tester/p/6408815.h ...
- eclipse-查看继承层次图/继承实现层次图
阅读代码时,如果想要看某个类继承了哪些类.实现了哪些接口.哪些类继承了这个类,恰巧这个类的继承实现结构又比较复杂,那么如果对开发工具不是很熟练,这个需求是比较难以实现的.eclipse中的type h ...
- git 添加分支并与远程连接
今天由于项目需要,要改版,为了不影响当前网站,所以用分支来管理 首先,在本地添加分支dev git checkout -b dev 提交远程,让同事拉取这个分支,我是直接push了,推到远程. 同事在 ...
- Python中删除easy_install安装的包
网上查了一大圈,终于在官网上找到了.记一下,备忘...
- LAMP简介与部署
lamp简介 lamp,是由Linux+Apache+Mysql/MariaDB+Php/Perl/Python的一组动态网站或者服务器的开源软件,除Linux外其它各部件本身都是各自独立的程序,但是 ...
- 2019.03.01 bzoj3075: [Usaco2013]Necklace(kmp+dp)
传送门 题意简述:给出S,TS,TS,T两个字串,∣S∣≤10000,∣T∣≤1000|S|\le10000,|T|\le1000∣S∣≤10000,∣T∣≤1000,问至少从SSS中删去几个字符能够 ...
- spass按位置编码,进行排序题处理与分析
本范例即需建立Q4_1至Q4_4 等四个变项, 各变量的数值则是排序的内容,共有0.1.2.3.4 等五种可能,0代表该选项没有被受测者选取,1.2.3.4分别代表被受测者指为第一至第四顺位. htt ...
- Vuejs——(7)过渡(动画)
版权声明:出处http://blog.csdn.net/qq20004604 目录(?)[+] 本篇资料来于官方文档: http://cn.vuejs.org/guide/transition ...
- 关于Runtime.getRuntime().exec()产生阻塞的2个陷阱
本文来自网易云社区 背景 相信做java服务端开发的童鞋,经常会遇到Java应用调用外部命令启动一些新进程来执行一些操作的场景,这时候就会使用到Runtime.getRuntime().exec(), ...