1 Description
Calling between microservices This article writes about realizing microservice communication through ribbon, feign, and http. This circuit breaker is based on the previous article.
2 Integrate Hystrix
pom dependencies:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
Add the @EnableHystrix annotation to the startup class
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
public class SalesApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(SalesApplication.class,args);
}
}
The interface is as follows. In order to demonstrate different fusing methods, there are two Feign method interfaces.
@RequestMapping("/sales")
public interface SalesRest {
@RequestMapping(value = "/queryGoodsListByRibbon", method = RequestMethod.GET)
Object queryGoodsListByRibbon();
@RequestMapping(value = "/queryGoodsListByFeign", method = RequestMethod.GET)
Object queryGoodsListByFeign();
@RequestMapping(value = "/queryGoodsListByFeign2", method = RequestMethod.GET)
Object queryGoodsListByFeign2();
@RequestMapping(value = "/queryGoodsListByHttp", method = RequestMethod.GET)
Object queryGoodsListByHttp();
}
Common method fuse: add @HystrixCommand(fallbackMethod = "CommonFallback") to the method, and execute the CommonFallback() fuse method after the specified target method cannot be called
@RestController
public class SalesRestImpl implements SalesRest {
@Autowired
private SalesService salesService;
@Override
@HystrixCommand(fallbackMethod = "CommonFallback")
public String queryGoodsListByHttp() {
return salesService.queryGoodsListByHttp();
}
@Override
@HystrixCommand(fallbackMethod = "CommonFallback")
public String queryGoodsListByRibbon() {
return salesService.queryGoodsListByRibbon();
}
@Override
@HystrixCommand(fallbackMethod = "CommonFallback")
public String queryGoodsListByFeign() {
return salesService.queryGoodsListByFeign();
}
@Override
public String queryGoodsListByFeign2() {
return salesService.queryGoodsListByFeign();
}
public String CommonFallback() {
return "CommonFallback";
}
}
Specific method calls:
@Service
public class SalesService {
@Autowired
RestTemplate restTemplate;
@Autowired
UserFeignClient userFeignClient;
private static final String RIBBON_URL = "http://user:8082/user/getUserInfo";
private static final String HTTP_URL = "http://127.0.0.1:8082/user/getUserInfo";
private static final String IP = IpUtil.getIp();
public String queryGoodsListByRibbon() {
String sales_result = "queryGoodsListByRibbon success : [sales_ip:" + IP + "] ";
String result = restTemplate.getForObject(RIBBON_URL, String.class);
return sales_result + result;
}
public String queryGoodsListByFeign() {
String sales_result = "queryGoodsListByFeign success : [sales_ip:" + IP + "] ";
String result = (String) userFeignClient.getUserInfo();
return sales_result + result;
}
public String queryGoodsListByHttp() {
String sales_result = "queryGoodsListByHttp success : [sales_ip:" + IP + "] ";
String result = HttpClientUtil.doGet(HTTP_URL);
return sales_result + result;
}
}
Feign fuse: Specify a fallback implementation class for the FeignClient interface, and then this class implements the FeignClient interface, so that when the interface cannot be adjusted, the corresponding method in the fallback implementation class will be called:
@FeignClient(name = "USER", fallback = UserFeignClientFallback.class)
public interface UserFeignClient {
@RequestMapping(value = "/user/getUserInfo", method = RequestMethod.GET)
String getUserInfo();
}
UserFeignClientFallback.class
@Component
public class UserFeignClientFallback implements UserFeignClient {
@Override
public String getUserInfo() {
return "feign client fallback";
}
}
3 Running results
Start eureka and sales, do not start user, observe the interface output
http://127.0.0.1:8081/sales/queryGoodsListByRibbon
--> CommonFallback
http://127.0.0.1:8081/sales/queryGoodsListByHttp
--> CommonFallback
http://127.0.0.1:8081/sales/queryGoodsListByFeign
--> CommonFallback
http://127.0.0.1:8081/sales/queryGoodsListByFeign2
--> queryGoodsListByFeign success : [sales_ip:192.168.0.102] feign client fallback
4 code
core-simple:https://code.aliyun.com/995586041/core-simple.git
eureka:https://code.aliyun.com/995586041/eureka.git
hystrix-sales:https://code.aliyun.com/995586041/hystrix-sales.git
hystrix-user:https://code.aliyun.com/995586041/hystrix-user.git