Spring Cloud Ribbon is based on HTTP and TCP client load balancing tool, Netflix Ribbon-based implementation, Ribbon, unlike registry, so need to deploy a separate gateway, which is integrated directly into Service in as a tool. Feign to be mentioned later which also integrates Ribbon.
1, manually set up a client load balancing
Ready to work:
- Preparation of a peer1, peer2 configuration center composed of
- Preparation of a service-1 (8091), service-1 (8092) of the server cluster configuration
- Prepare a Ribbon client
Add pom-dependent
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency>
Ribbon client, using @EnableDiscoveryClient registered with the Eureka:
@SpringBootApplication @EnableDiscoveryClient public class RibbonServiceApplication { public static void main (String [] args) { SpringApplication.run (RibbonServiceApplication. Class , args); } / ** * instantiated RestTemplate, opening balancing load by @LoadBalanced annotation * / @Bean @LoadBalanced public RestTemplate RestTemplate () { return new new RestTemplate (); } }
Call service Service-1 provides:
@Service public class UserService { @Autowired private RestTemplate restTemplate; public UserDto getUser(Long userId) { ResponseEntity<UserDto> responseEntity = restTemplate.getForEntity("http://service-1/getUser/{1}", UserDto.class, userId); return responseEntity.getBody(); } }
After the turn starts registry, Service-1 and the Ribbon two instances, all successful start, start service-1 can be seen two examples, as shown below:
Access Ribbon client interface: http: // localhost: 9000 / user / getByUserId
Refresh the page, the back-end Service-1 are two examples of output log:
service-1:8091
8091 provides service
service-1:8092
8092 provides service
8092 provides service
The system architecture as shown:
2, Ribbon Configuration
Automated configuration
Load balancing process above does not build any Ribbon-related configuration, the default configuration is done a lot since Spring Cloud integration Eureka and Ribbon time.
In the absence of the introduction of Spring Cloud Eureka, Spring Cloud Ribbon has achieved these default configuration bean:
-
IClientConfig: Ribbon client configuration defaults com.netflix.client.config.DefaultClientConfigImpl
-
IRule: Ribbon load balancing strategy, defaults com.netflix.loadbalancer.ZoneAvoidanceRule
-
IPing: Ribbon instance heartbeat checking policy, defaults com.netflix.loadbalancer.NoOpPing, it can be seen by looking to achieve: do not check the instance is available, always returns true
-
ServerList: Service maintains a list of list of examples, defaults com.netflix.loadbalancer.ConfigurationBasedServerList
-
ServerListFilter: Service Instance list filtering mechanisms, defaults org.springframework.cloud.netflix.ribbon.ZonePreferenceServerListFilter
-
ILoadBalancer: load balancer, defaults com.netflix.loadbalancer.ZoneAwareLoadBalancer
Custom configuration: Code + RibbonClient manner
@RibbonClient(value = "service-1", configuration = MyRibbonConfig.class) public class RibbonServiceApplication {
Custom random load when:
@Configuration public class MyRibbonConfig { @Bean public IRule ribbonRule() { new RandomRule(); } }
When the load is changed to customize the polling:
@Configuration public class MyRibbonConfig { @Bean public IRule ribbonRule() { new RoundRobinRule(); } }
Respectively, in the above two cases refresh the page http: // localhost: 9000 / user / getByUserId (to refresh eight times, for example, observation log two examples of service-1 console print):
- Found that when RandomRule service-1 is supplied in 8091 and 8092 two service instances.
- It found that when the rotation is RoundRobinRule service-1 in 8091 and 8092 to provide two service instances.
Custom configuration: Code + profile mode ( Components of Load Balancer )
In the properties in the configuration, created by the reflection mode, configuration is as follows:
以 `<clientName>.ribbon.` 为前缀,加上下面的属性名(属性名来自 org.springframework.cloud.netflix.ribbon.PropertiesFactory 类):
NFLoadBalancerClassName
NFLoadBalancerRuleClassName
NFLoadBalancerPingClassName
NIWSServerListClassName
NIWSServerListFilterClassName
Configuring ribbon load mode (for service-1 service settings):
service-1:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
The custom class assigned to the corresponding levels of service attributes can be done custom configuration.
3, Appendix
This article Demo Address: Ribbon Service