Ribbonis a based onhttpand theTcpthe client Load Balancer tool
Hystrix with service degradation, service fuse, thread and signal isolation, request caching, request consolidation, and service monitoring
to facilitate consumer load balancing, copy demo-springcloud-client1 and rename demo-springcloud-client2, modify configuration files and startup classes, service provider 1 and 2 service names are spring.application.name=demo-springcloud-client
Modify Yhqcontoller implementation, in order to facilitate load balancing observation effect,Demo-springcloud-client2 project return string Client2
Create a consumer project Demo-springcloud-resttemplate-consumer, create a startup class Resttemplateapplication, @EnableDiscoveryClient Start service discovery and enroll for automatic configuration To be able to discover the previously registered service providers Demo-springcloud-client1 and Demo-springcloud-client2. @EnableCircuitBreaker turn on fuse auto-configuration
Create a load-balanced resttemplate, @Bean and use the @loadbalanced qualifier. If there is no spring-cloud-starter-ribbon dependency, the @LoadBalanced load-balancing polling algorithm will not take effect. Using Spring-cloud-starter-eureka dependency, the Ribbon service discovery will be delivered to Eureka. Add fuse dependent Spring-cloud-starter-hystrix
Create Yhqcontroller provide rest service Resttemplateconsumer to consume prior services 1 and 2,
The service calls our service provider Demo-springcloud-client1 and Demo-springcloud-client2 by injecting the Yhqservice resttemplate object Load Balancer
Start the registry, start service Providers 1 and 2, start consumer Demo-springcloud-resttemplate-consumer, browser access http://localhost:3331/ Resttemplateconsumer returns the string client1 and Client2 alternating, which is the result of load balancing polling.
Fuse Hystrix uses the fallback method used when the fallback command fails to implement the degraded processing logic for the service. This closes the service provider Demo-springcloud-client2, and the browser accesses Http://localhost:3331/restTemplateConsumer returns The string client1 and I was demoted logical alternate appear
When implementing a fuse through the annotation method, set the property value by using @hystrixcommand's commandproperties, such as setting fallback.enabled
Dry Goods sharing microservices spring-cloud (4. Load balancing ribbon and fuse hystrix)