使⽤配置⽂件⾃定义Ribbon配置
1、l——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: comflix.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: comflix.loadbalancer.ZoneAvoidanceRule
2、RibbonConsumerApplication——Ribbon启动类
package bbin;
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 t.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 ller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloudflix.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
spring怎么读取yamlprivate 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:" + Host() + ",port:" + Port() + ",serviceId=" + ServiceId() + ",uri=" + Uri());
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:" + Host() + ",port:" + Port() + ",serviceId=" + ServiceId() + ",uri=" + Uri());
return "/ribbon/get2's demo, please to see console output";
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论