Introduction to Ribbon
In a distributed system, each microservice will deploy multiple instances. How to evenly distribute service consumers to multiple service provider instances requires the use of load balancers.
Ribbon is a load balancer, which provides many load balancing algorithms, such as polling, and then, after configuring the service provider address, the service consumer requests can be distributed evenly.
Integrate Ribbon to serve consumers
Add Ribbon dependency library
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-ribbobn</artifactId></dependency>
Add @LoadBalaced annotation to RestTemplate to integrate RestTemplate and Ribbon
@Bean@LoadBalancedpublic RestTemplate restTemplate(){ return new RestTemplate();}Modify the Controller and modify the request address to http://flim-user/user/ . When Ribbon and Eureka are used together, the virtual host name will be automatically mapped to the network address of the microservice, and the LoadBalancerClient is injected to output the currently selected microservice node.
@RestControllerpublic class MovieController { private final Logger log = LoggerFactory.getLogger(MovieController.class); @Autowired private RestTemplate restTemplate; @Autowired private LoadBalancerClient loadBalancerClient; @GetMapping("/user/{id}") public User findById(@PathVariable int id){ return this.restTemplate.getForObject("http://flim-user/"+id,User.class); } @GetMapping("/log-instance") public void logInstance(){ ServiceInstance serviceInstance = this.loadBalancerClient.choose("flim-user"); log.info("{}:{}:{}",serviceInstance.getServiceId(),serviceInstance.getHost(),serviceInstance.getPort()); }}Run the test program
{"id":1,"username":"account1","name":"Zhang San","age":20,"balance":100.00}
The following information will be output when accessing the http://localhost:8010/log-instance multiple times.
You can see that the requests will be evenly distributed to the two user microservices
2017-12-17 20:47:53.975 INFO 12313 --- [nio-8010-exec-2] com.linyuan.controller.MovieController : flim-user:linyuandembp:8764
2017-12-17 20:47:54.215 INFO 12313 --- [nio-8010-exec-1] com.linyuan.controller.MovieController : flim-user:linyuandembp:8763
2017-12-17 20:47:54.445 INFO 12313 --- [nio-8010-exec-3] com.linyuan.controller.MovieController : flim-user:linyuandembp:8764
2017-12-17 20:47:54.690 INFO 12313 --- [nio-8010-exec-4] com.linyuan.controller.MovieController : flim-user:linyuandembp:8763
2017-12-17 20:47:54.935 INFO 12313 --- [nio-8010-exec-5] com.linyuan.controller.MovieController : flim-user:linyuandembp:8764
Note: restTemplate.getForObject(...) and loadBalancerClient.choose(...) cannot be written in the same method, because rest-Template is actually a Ribbon client and itself already contains the "choose" behavior.
Configuring Ribbon by code
You can use Java code or attributes to customize the Ribbon configuration. The default configuration class of Ribbon is RibbonClientConfiguration, or you can use a POJO to customize the Ribbon configuration. This configuration is fine-grained. Different Ribbon clients can use different configurations.
Create a Ribbon configuration class
/** * This class is a configuration class* should not be scanned by ComponentScan*/@Configurationpublic class RibbonConfiguration { @Bean public IRule ribbonRule(){ //Configuration load balancing rules, change to random return new RandomRule(); }}Specify configuration classes for service providers using the @RibbonClient or @RibbonClients annotation
@SpringBootApplication@EnableDiscoveryClient@RibbonClient(name = "flim-user",configuration = RibbonConfiguration.class)public class FlimConsumerApplication { @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(FlimConsumerApplication.class, args); }}Visit the test address http://localhost:8010/log-instance and you can see that the request will be randomly distributed to two microservices.
2017-12-17 21:08:52.769 INFO 12524 --- [nio-8010-exec-7] com.linyuan.controller.MovieController : flim-user:linyuandembp:8763
2017-12-17 21:08:52.946 INFO 12524 --- [nio-8010-exec-8] com.linyuan.controller.MovieController : flim-user:linyuandembp:8763
2017-12-17 21:08:53.138 INFO 12524 --- [nio-8010-exec-9] com.linyuan.controller.MovieController : flim-user:linyuandembp:8763
2017-12-17 21:08:53.319 INFO 12524 --- [io-8010-exec-10] com.linyuan.controller.MovieController : flim-user:linyuandembp:8764
2017-12-17 21:08:53.511 INFO 12524 --- [nio-8010-exec-1] com.linyuan.controller.MovieController : flim-user:linyuandembp:8763
Note: The RibbonConfiguration class cannot be scanned by @ComponentScan, otherwise the configuration information will be shared by all @RibbonClient. Therefore, if you only want to customize the configuration of a certain Ribbon client, you must prevent it from being scanned by @ComponentScan.
Configure Ribbon in configuration file
Customizing Ribbon properties through configuration files is more convenient, and the configuration prefix is <clientName>.ribbon.
Define Ribbon configuration through configuration files
flim-user: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
Commonly used Ribbon global configuration
ribbon: ConnectionTimeout: #Connection timeout ReadTimeout: #Read timeout OkToRetryOnAllOperatotions: #Retry all operation requests MaxAutoRetriesNextServer: #Switch server instance retries MaxAutoRetries: #Retry on the current instance ServerListRefreshInterval: #Reset time between refreshing the service list source
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.