background
Look at the feign
individual 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
用 feign 代替 hystrix+ribbon
feign + ribbon load balancing and retry
1-load balancing
No additional configuration is required, feign has enabled the ribbon
负载均衡
and重试
mechanism, and the parameters can be called through configurationConnectTimeout=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 the service, access the test
http://localhost:3001/item-service/35
prompt:
重载 一般不怎么使用,看项目的需求,如果需要才用
负载均衡默认都是有的,看情况,如果项目需要才用上
feign + hystrix downgrade
1-feign enables hystrix
feign
默认没有启用 hystrix
, add configuration, enable hystrix
1.1-application.yml add configuration
feign:
hystrix:
enabled: true
1.2 After enabling hystrix, access the service
http://localhost:3001/item-service/35
默认1秒会快速失败,没有降级方法时,会显示白板页
2-feign implements hystrix downgrade
Degrade class specified in the remote interface
aims:远程调用失败, 会执行降级类中的代码
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 Downgrade class
The downgrade class needs to implement the remote interface
ItemClientFB
@Component
public class ItemClientFB implements ItemClient{
@Override
public JsonResult<List<Item>> getItems(String orderId) {
return JsonResult.err().msg("调用商品失败");
}
@Override
public JsonResult<?> decreaseNumber(List<Item> items) {
return JsonResult.err().msg("调用商品失败");
}
}
OrderClientFB
@Component
public class OrderClientFB implements OrderClient{
@Override
public JsonResult<Order> getOrder(String orderId) {
return JsonResult.err().msg("调用订单失败");
}
@Override
public JsonResult<?> saveOrder() {
return JsonResult.err().msg("调用订单失败");
}
}
UserClientFB
@Component
public class UserClientFB implements UserClient{
@Override
public JsonResult<User> getUser(Integer userId) {
return JsonResult.err().msg("调用用户失败");
}
@Override
public JsonResult<?> addScore(Integer userId, Integer score) {
return JsonResult.err().msg("调用用户失败");
}
}
2-3 Start service, access test
http://localhost:3001/item-service/35
Jump to the degraded object after the call fails
(降级成功)
feign + hystrix monitoring
1-pom.xml add hystrix start-up dependency
feign 没有包含完整的 hystrix 依赖
1-1 xml dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
1-2 visual dependencies
Main program added @EnableCircuitBreaker
@EnableCircuitBreaker
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class Sp09FeignApplication {
public static void main(String[] args) {
SpringApplication.run(Sp09FeignApplication.class, args);
}
}
2-Configure actuator to expose hystrix.stream monitoring endpoint
View pom.xml, confirmation has been added
actuator
dependent
2-1 actuator dependent
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2-2 application.yml exposes 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 (monitoring instrument)
If you don’t have a hystrix dashboard or have never learned it, then please enter the hystrix dashboard circuit breaker dashboard
Start the hystrix dashboard service, fill in the feign monitoring path, and start monitoring
-
Access the instrument server: http://localhost:4001/hystrix
-
Fill in the feign monitoring path: http://localhost:3001/actuator/hystrix.stream
Access business server: http://localhost:3001/item-service/35