随笔 - 1357  文章 - 0  评论 - 1104  阅读 - 1941万

使用配置文件自定义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";
    }

}
复制代码

 

posted on   Ruthless  阅读(8690)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示