Spring Cloud 自定义负载均衡的一些问题

Spring Cloud 自定义负载均衡的一些问题

由于最近毕设要求,需要自定义负载均衡,中途发生OOM错误,一开始莫名其妙,最后才发现问题,特意记录一下。首先需要有多个微服务提供者,并且均注册到nacos中心,注册名称一致,然后通过openfeign调用则会默认开启轮询负载均衡策略。这里先简单记录下openfeign配置。
在这里插入图片描述
服务提供端

 	@GetMapping("/demo")
    public CommonResult demo() {
        return new CommonResult(200,"success","this is server:"+port);
    }

然后在消费端customer services接口上添加@FeignClient注解,name为提供者的微服务名称,启动类上添加@EnableFeignClients注解和@EnableDiscoveryClient。

	@FeignClient(name = "ApiManager",configuration = MyFeignConfiguration.class)
	public interface openFeign
	{
	  @GetMapping("/demo")
	  public CommonResult demo();
	}

接着在第三个微服务openfeign里面导入customer模块maven坐标,导入过后就可以在Controller里面注入openfeign对象,从而调用APIManager的demo函数

	@Resource
    OpenFeigenTest openFeigen;

    @GetMapping("/demo")
    public CommonResult demo()
    {
        return openFeigenTest.demo();
    }

customer maven坐标

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>

openfeign maven坐标

引入openfeign模块
  <dependency>
            <groupId>com.wl</groupId>
            <artifactId>com.wl.openFeign</artifactId>
            <version>1.0-SNAPSHOT</version>
</dependency>
 <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
        <version>2.2.0.RELEASE</version>
  </dependency>

由于spring cloud自带了ribbon,调用openfeign模块的demo函数,会默认按照轮询的方式依次调用三个后端节点。
接下来开始自定义负载均衡。
在openfeign模块中,新建一个类MyRule,继承AbstractLoadBalancerRule,重写choose方法

public class MyRule extends AbstractLoadBalancerRule {
   //@Resource
   // ServerService serverService;
    @Override
    public Server choose(Object o) {

        List<Server> allServers = getLoadBalancer().getAllServers();
        .....
        .....
        .....//可以在此自定义负载均衡策略
        return allServers.get(1);//该返回值指的是将请求接入第1个服务节点
      }
}

然后新建一个Controller,添加@Configuration注解,注入bean,代码如下所示

@Configuration
public class RuleConfig {
    @Bean
    public IRule ribbonRule() {
        return new MyRule();
    }
}

最后在openfeign启动类上添加@RibbonClient(value = “service-hi”, configuration = RuleConfig.class)注解,在yaml文件中配置负载均衡策略,其中,ApiManager是微服务名称,最后一个就是自定义的负载均衡策略

ApiManager:
  ribbon:
    #轮询
#    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule
    #随机算法
#    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
    #重试算法,该算法先按照轮询的策略获取服务,如果获取服务失败则在指定的时间内会进行重试,获取可用的服务
    #NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RetryRule
    #加权法,会根据平均响应时间计算所有服务的权重,响应时间越快服务权重越大被选中的概率越大。刚启动时如果同统计信息不足,则使用轮询的策略,等统计信息足够会切换到自身规则。
#    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.ZoneAvoidanceRule
    NFLoadBalancerRuleClassName: com.wl.controller.MyRule

关键点在于,choose里面不能使用openfeign调用远程API,只能调用本地API。
openfeign本意调用的均是远程API,是不需要引入一堆maven坐标,编写函数,因此当我需要操作数据库时,想当然的在ApiManager模块编写函数,然后在本模块远程调用,然后发生OOM.
原因在于,每次调用http请求时,都会由于触发负载均衡从而自动进入choose函数里面,而又在choose函数调用http请求则会发生递归从而停止运行,因此choose模块的函数均是本地函数,serverService不能远程引入。

  • 14
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值