SpringCloud(十八)自定义ribbon

本文介绍如何在Spring Cloud中配置Ribbon负载均衡器。包括通过代码配置特定微服务的负载均衡策略,以及如何通过配置文件来实现自定义负载均衡策略。通过实例演示了如何针对不同微服务应用不同的负载均衡算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       在上一篇文章 SpringCloud(四) ribbon负载均衡 中用的默认的负载均衡,这一篇文章讲解怎么配置负载均衡,怎么为某个微服务指定负载均衡。

1. 通过代码配置ribbon client

   写一个负载均衡规则的配置类。这个配置类如果在spring的管理下,就会变成全局的配置类,所以我把它放在了ComponentScan扫描范围外

package com.xhx.config;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * xuhaixing
 * 2018/9/3 20:09
 *
 *  不放在ComponentScan下,否则会变成全局的,如果对单个微服务进行控制,就放在扫描的外面
 *
 **/
@Configuration
public class FooConfiguration {

    @Bean
    public IRule ribbonRule(IClientConfig config){
        return new RandomRule();
    }
}

 写了另一个配置类,指定哪个服务应用上面设置的规则。这个要在spring管理下

package com.xhx.springcloud.config;

import com.xhx.config.FooConfiguration;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Configuration;

/**
 * xuhaixing
 * 2018/9/3 19:53
 **/
@Configuration
@RibbonClient(name = "eureka-service2",configuration = FooConfiguration.class)
public class TestConfiguration {
}

注入RestTemplate

package com.xhx.springcloud.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * xuhaixing
 * 2018/6/3 16:24
 */
@Configuration
public class RibbonConfig {


    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

两个测试的请求,一个请求service1,默认的策略,,另一个请求service2,随机的策略

package com.xhx.springcloud.controller;

import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * xuhaixing
 * 2018/6/3 16:27
 */
@RestController
public class RibbionController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "getName")
    public String getName(String name){
        return restTemplate.getForObject("http://eureka-service/hello/getName?name="+name,String.class);
    }

    @RequestMapping(value = "getName2")
    public String getName2(String name){
        return restTemplate.getForObject("http://eureka-service2/hello/getName?name="+name,String.class);
    }
}

 

起了如下工程,两个service是服务的,一个client是测试用的客户端

这样就可以了

 

2.  通过配置文件配置

Starting with version 1.2.0, Spring Cloud Netflix now supports customizing Ribbon clients by setting properties to be compatible with the Ribbon documentation.

This lets you change behavior at start up time in different environments.

The following list shows the supported properties>:

  • <clientName>.ribbon.NFLoadBalancerClassName: Should implement ILoadBalancer
  • <clientName>.ribbon.NFLoadBalancerRuleClassName: Should implement IRule
  • <clientName>.ribbon.NFLoadBalancerPingClassName: Should implement IPing
  • <clientName>.ribbon.NIWSServerListClassName: Should implement ServerList
  • <clientName>.ribbon.NIWSServerListFilterClassName: Should implement ServerListFilter
#通过配置文件方式配置自定义ribbon client
eureka-service:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

请求eureka-service这个微服务时,就会使用随机策略了

 

 

实时内容请关注微信公众号,公众号与博客同时更新:程序员星星

实时内容请关注微信公众号,公众号与博客同时更新

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值