background
Let's first look at feign's separate article Feign declarative client interface
Many tools in spring cloud are used together. feign integrates ribbon and hystrix and provides a declarative consumer client. We use these tools together
-
ribbon -- load balancing, retry
-
hystrix - circuit breaker (degraded, fused)
Replace hystrix+ribbon with feign
feign + ribbon load balancing and retry
1 - load balancing
Without additional configuration, feign has enabled ribbon load balancing and retry mechanism, and parameters can be called through configuration
ConnectTimeout=1000 ReadTimeout=1000 MaxAutoRetries=0 MaxAutoRetriesNextServer=1
2 - configure ribbon timeout and retry
- ribbon - global configuration
- Item service -- configuration of a specific service instance
spring: application: name: feign server: port: 3001 eureka: client: service-url: defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka ribbon: ConnectTimeout: 1000 ReadTimeout: 1000 item-service: ribbon: MaxAutoRetries: 1 MaxAutoRetriesNextServer: 2 ConnectTimeout: 1000 ReadTimeout: 500
Start service, access test
http://localhost:3001/item-service/35
Tips:
Overloading is generally not used very much. It depends on the requirements of the project. It can only be used if necessary
Load balancing is available by default. Depending on the situation, it can only be used if the project needs it
feign + hystrix downgrade
Enable hystrix - hyfe1
feign does not enable hystrix by default. Add configuration and enable hystrix
1.1 - application.yml add configuration
feign: hystrix: enabled: true
1.2 access the service after enabling hystrix
http://localhost:3001/item-service/35
By default, 1 second will fail quickly. When there is no degradation method, the whiteboard page will be displayed
2 - feign implements hystrix degradation
Specify a degraded class in the remote interface
Target: if the remote call fails, the code in the degraded class will be executed
2 - 1 remote interface (Interface)
ItemClient
@FeignClient(name="item-service",fallback = ItemClientFB.class) public interface ItemClient { ... ...
OrderClient
... @FeignClient(name="order-service",fallback = OrderClientFB.class) public interface OrderClient{ ...
UserClient
... @FeignClient(name="user-service",fallback = UserClientFB.class) public interface UserClient{ ...
2 - 2 degraded
Degraded classes need to implement remote interfaces
ItemClientFB
@Component public class ItemClientFB implements ItemClient{ @Override public JsonResult<List<Item>> getItems(String orderId) { return JsonResult.err().msg("Failed to call product"); } @Override public JsonResult<?> decreaseNumber(List<Item> items) { return JsonResult.err().msg("Failed to call product"); } }
OrderClientFB
@Component public class OrderClientFB implements OrderClient{ @Override public JsonResult<Order> getOrder(String orderId) { return JsonResult.err().msg("Call order failed"); } @Override public JsonResult<?> saveOrder() { return JsonResult.err().msg("Call order failed"); } }
UserClientFB
@Component public class UserClientFB implements UserClient{ @Override public JsonResult<User> getUser(Integer userId) { return JsonResult.err().msg("Failed to call user"); } @Override public JsonResult<?> addScore(Integer userId, Integer score) { return JsonResult.err().msg("Failed to call user"); } }
2 - 3 start service, access test
http://localhost:3001/item-service/35
After the call fails, jump to the degraded object (degraded successfully)
feign + hystrix monitoring
1 - pom. Adding hystrix dependency to XML
feign does not contain a complete hystrix dependency
1 - 1 xml dependency
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
1 - 2 visual dependencies
Add @ enablercircuitbreaker to the main program
@EnableCircuitBreaker @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class Sp09FeignApplication { public static void main(String[] args) { SpringApplication.run(Sp09FeignApplication.class, args); } }
2 - configure actor to expose hystrix Stream monitoring endpoint
View POM XML and confirm that the actor dependency has been added
2 - 1 actor dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2 - 2 application.yml exposed hystrix Stream endpoint
management: endpoints: web: exposure: include: hystrix.stream
3 - start the service and view the monitoring endpoint
http://localhost:3001/actuator
4 - hystrix dashboard
If you don't have a hystrix dashboard instrument or have not learned it, please come in hystrix dashboard circuit breaker instrument panel
Start the hystrix dashboard service, fill in the feign monitoring path, and start monitoring
-
To access the instrument server: http://localhost:4001/hystrix
-
Fill in feign monitoring path: http://localhost:3001/actuator/hystrix.stream
-
Access microservices to generate monitoring data
To access the business server: http://localhost:3001/item-service/35