A load balancer provides at least the following functions:
To maintain the IP and other information of each server
According to specific Logical selection of servers
In order to achieve basic load balancing functions, Ribbon's load balancer has three major sub-modules:
Rule
Ping
ServerList
When Ribbon is used in SpringCloud, the configuration can be done in two ways, one One is Java configuration and the other is configuration file configuration.
Configuration through java:
package com.hurricane.learn.springcloud.ribbon; import org.springframework.cloud.netflix.ribbon.RibbonClient; import org.springframework.context.annotation.Bean; @RibbonClient(name="user-service-provider",configuration=MyConfig.class) public class MyConfig { @Bean public MyRule createMyRule() { return new MyRule(); } }
Configuration file configuration:
#自定义规则的使用 user-service-provider.ribbon.NFLoadBalancerRuleClassName=com.hurricane.learn.springcloud.ribbon.MyRule
A pitfall of service calls:
Test calls between services, keep calling Unsuccessful, prompt:
Request URI does not contain a valid hostname: http://user_service_provider/getUser
Finally found out that it was because the service instance name cannot have underscores. You can access it by changing the instance name to aaa.
The above is the detailed content of Ribbon usage in Spring Cloud applications. For more information, please follow other related articles on the PHP Chinese website!