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"; } }