本文主要是介绍【SpringCloud】(七):自定义Ribbon,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
上篇文章,我们已经使用Ribbon解决了服务地址硬编码和负载均衡,负载均衡默认使用的是轮询方式。
本篇文章,我们来描述Ribbon的自定义配置,以配置它的负载均衡算法的方式为例。
自定义配置有2中方式,通过代码或配置文件
一.通过代码的方式
对WARNING内容的理解:
这个配置类必须加上@Configuration注解,但是要注意,它不是一个主要的应用组件在@ComponetScan 中。否则会被所有的@RibbonClient共享。
因此如果你使用@ComponetScan,你需要采取措施避免它被扫描。
使configuration只作用于指定的RibbonClient,解决方案:
1.把它放在一个独立的,非重叠的包,
2.或指定扫描明确的组件包,不扫描它
对Ribbon配置的类,要加上@Configuration注解,但是不能被@ComponetScan 扫描。
配置类TestConfiguration,配置负载均衡算法为随机
package com.dynamic.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.dynamic.cloud.ExcludeFromComponentScan;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;@Configuration
@ExcludeFromComponentScan
public class TestConfiguration {@Beanpublic IRule ribbonRule(){return new RandomRule();}
}
启动类
package com.dynamic.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.client.RestTemplate;import com.dynamic.config.TestConfiguration;@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "microservice-provider-user", configuration = TestConfiguration.class)
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type= FilterType.ANNOTATION,value = ExcludeFromComponentScan.class)})
public class ComsumerMovieRibbonApplication {@Bean@LoadBalanced//让restTemplate具备Ribbon负载均衡的能力。public RestTemplate restTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(ComsumerMovieRibbonApplication.class, args);}
}
Controller
package com.dynamic.cloud.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import com.dynamic.cloud.entity.User;@RestController
public class MovieController {@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate LoadBalancerClient loadBalancerClient;@GetMapping("/movie/{id}")public User findById(@PathVariable Long id) {// http://localhost:7900/simple/// VIP Virtual IP:虚拟IP,使用的是服务提供者的ServiceId,也就是application.name// HAProxy HeartBeat// microservice-provider-user:7900return this.restTemplate.getForObject("http://microservice-provider-user/simple/" + id, User.class);}@GetMapping("/test")public String test() {ServiceInstance serviceInstance = this.loadBalancerClient.choose("microservice-provider-user");System.out.println("111"+ ":" + serviceInstance.getServiceId() +":"+serviceInstance.getHost() + ":"+ serviceInstance.getPort() );ServiceInstance serviceInstance2 = this.loadBalancerClient.choose("microservice-provider-user2");System.out.println("222" + ":" + serviceInstance2.getServiceId()+":"+ serviceInstance2.getHost() + ":"+ serviceInstance2.getPort() );return "1";}}
解决@Configuration和@ComponetScan冲突
1.让配置类不在启动的包或者子包下。解决。
2.加入注解
(1)注解:
package com.dynamic.cloud;public @interface ExcludeFromComponentScan {}
(2)如上述代码,在配置类上加上该注解@ExcludeFromComponentScan
(3)在启动类加上,@ComponentScan(excludeFilters = {@ComponentScan.Filter(type= FilterType.ANNOTATION,value = ExcludeFromComponentScan.class)})
在启动类上的注解:
@RibbonClient(name = "microservice-provider-user", configuration = TestConfiguration.class)。如果解决了@Configuration和@ComponetScan的冲突。则只有服务ID为microservice-provider-user的负载均衡算法,才是自定义配置的随机。而其他的服务还是默认的轮询。
请求结果:
111:microservice-provider-user:192.168.21.60:7900
222:microservice-provider-user2:192.168.21.60:7903
111:microservice-provider-user:192.168.21.60:7900
222:microservice-provider-user2:192.168.21.60:7904
111:microservice-provider-user:192.168.21.60:7901
222:microservice-provider-user2:192.168.21.60:7903
111:microservice-provider-user:192.168.21.60:7901
222:microservice-provider-user2:192.168.21.60:7904
111:microservice-provider-user:192.168.21.60:7901
222:microservice-provider-user2:192.168.21.60:7903
111:microservice-provider-user:192.168.21.60:7901
222:microservice-provider-user2:192.168.21.60:7904
可以看出,microservice-provider-user的负载均衡策略是随机,microservice-provider-user2的策略是轮询
二.配置文件配置Ribbon
我们把所有代码配置的全部去掉,使用配置文件进行配置。
在电影微服务的配置文件中加上:
microservice-provider-user: #服务idribbon:NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
服务ID为microservice-provider-user的负载均衡为随机。而microservice-provider-user为轮询。
111:microservice-provider-user:192.168.21.60:7900
222:microservice-provider-user2:192.168.21.60:7903
111:microservice-provider-user:192.168.21.60:7901
222:microservice-provider-user2:192.168.21.60:7902
111:microservice-provider-user:192.168.21.60:7900
222:microservice-provider-user2:192.168.21.60:7903
111:microservice-provider-user:192.168.21.60:7900
222:microservice-provider-user2:192.168.21.60:7902
相比Java代码配置,使用配置文件
1.优先级高
2.简洁
这篇关于【SpringCloud】(七):自定义Ribbon的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!