使用配置文件自定义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的时候,就会发现太过散乱了,工作量也会很大,涉及的文件还很多,一不小 ...
随机推荐
- Head First Servlets & JSP 学习笔记 第六章 —— 会话状态
MVC中的M(模型),通常就是一个普通的类,这个类里面的信息就是业务逻辑. 会话(Session) 我们可以使用一个HttpSession对象,来保存横跨多个请求的会话状态. HTTP协议使用的是无状 ...
- 新建一个项目,如何使用abp用户登录系统
1.首先参考Framework.Web里的packages.config,把相关的包都安装好. 2.App_Start文件夹下的xxxModule.cs和Startup.cs拷过来,修改下命名空间. ...
- 31.Mysql复制
31.Mysql复制复制是指将主数据库的DDL和DML操作通过二进制日志传到从数据库上,然后在从数据库上对重做日志,从而使从库与主库保持同步.Mysql支持一台主库同时向多台从库复制,从库也可以作为其 ...
- vnc 搭建 转
这里要注意,关闭selinux setenforce 0 原文地址: http://www.linuxidc.com/Linux/2015-04/116725.htm 这是一个关于怎样在你的 Cent ...
- mysql开启调试日志general_log开启跟踪日志
general_log = 1 general_log_file = /tmp/umail_mysql.log 有时候,不清楚程序执行了什么sql语句,但是又要排除错误,找不到原因的情况下, 可以在m ...
- 20170805_Elasticsearch_简介和安装
官网地址: https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started.html Elk 是一个可高 ...
- CentOS上部署.net core
1.阿里云更换系统安装CentOS7.4 64位版本 2.试用XShell 5 登录服务器 参考https://www.microsoft.com/net/learn/get-started/linu ...
- python2.x 到 python3.x 中“url”部分变化
这部分是笔者在亲身项目中遇到的一些变化,并不全,后面将会更新. (1) urllib.urlopen 改为: urllib.request.urlopen (2) urllib2 删除 ...
- jxl操作excel写入数据不覆盖原有数据示例
public void readTO() { Workbook wb = null; WritableWorkbook wwb = null; try { ...
- swiper,一个页面使用多个轮播
代码示例: <html> <head> <link href="https://cdn.bootcss.com/Swiper/4.3.0/css/swiper. ...