# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #RestTemplate calls, Feign calls use fallback and FallbackFactory use Hystrix instances ---------------------------restTemplate-----------begin--------------------------------- package com.example.user.controller; import com.example.user.rest.RestTemplateWrapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; /** * User services */ @restController Public class UserRestController {@autoWired private RestTemplateWrapper restTemplateWrapper; /** * Normal request * @return
*/
@PostMapping("/rest/login")
public String restLogin() {return "rest "+ restTemplateWrapper.loginActivity(); } /** * timeout request * @return
*/
@PostMapping("/rest/timeout")
public String restTimeOut() {return "rest "+ restTemplateWrapper.timeout(); } /** * exception request * @return
*/
@PostMapping("/rest/exception")
public String restException() {return "rest "+ restTemplateWrapper.exception(); } } ----------------------------------------------------------------------------------------- package com.example.user.rest; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; @Component public class RestTemplateWrapper { @Autowired private RestTemplate restTemplate; /** * Normal request active interface * @return
*/
public String loginActivity() {return restTemplate.postForObject("http://ACTIVITY/activity/loginActivity",null, String.class); } /** * timeout request * @return
*/
public String timeout() {return restTemplate.postForObject("http://ACTIVITY/activity/timeout",null, String.class); } /** * exception request * @return
*/
@HystrixCommand(
fallbackMethod = "fallback",
threadPoolKey = "exceptionPool",
threadPoolProperties = {
@HystrixProperty(name = "coreSize", value = "3"),
@HystrixProperty(name = "maxQueueSize", value = "20")},commandProperties = {
@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "5000"),
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2"),
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"),
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "3000")
}
)
public String exception() {return restTemplate.postForObject("http://ACTIVITY/activity/exception",null, String.class);
}
public String fallback() {return "Starting circuit breaker"; } } ---------------------------restTemplate------end------------------------------------- ---------------------------feign--fallback----begin---------------------------------- package com.example.user.controller; import com.example.user.feign.UserFeign; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @restController Public Class UserFeignController {@autoWired private UserFeign UserFeign; /** * Feign normal * @return
*/
@PostMapping("/feign/login")
public String feignLogin() {return "feign "+ userFeign.loginActivity(); } /** * feign times out * @return
*/
@PostMapping("/feign/timeout")
public String feignTimeOut() {return "feign "+ userFeign.timeout(); } /** * Feign exception * @return
*/
@PostMapping("/feign/exception")
public String feignException() {return "feign " + userFeign.exception();
}
}
-----------------------------------------------------------------------------
package com.example.user.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
//@FeignClient(value = "ACTIVITY", fallback = UserFeignFallBack.class)
public interface UserFeign {
@PostMapping("/activity/loginActivity")
String loginActivity();
@PostMapping("/activity/timeout")
String timeout();
@PostMapping("/activity/exception")
String exception();
}
-----------------------------------------------------------------------------
package com.example.user.feign;
import org.springframework.stereotype.Component;
@Component
public class UserFeignFallBack implements UserFeign {
@Override
public String loginActivity() {
return "Hystrix to return to the login";
}
@Override
public String timeout() {
return "Hystrix returns a timeout";
}
@Override
public String exception() {
return "Return to exception hystrix"; } } ---------------------------feign--fallback----end------------------------------------- ---------------------------feign--fallbackFactory----begin---------------------------- package com.example.user.controller; import com.example.user.feignfactory.UserFeign; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; / * * * * / user service @ RestController public class UserFeignFactoryController {@autowired private UserFeign UserFeign; /** * Feign normal * @return
*/
@PostMapping("/feign/factory/login")
public String feignLogin() {return "feign "+ userFeign.loginActivity(); } /** * feign times out * @return
*/
@PostMapping("/feign/factory/timeout")
public String feignTimeOut() {return "feign "+ userFeign.timeout(); } /** * Feign exception * @return
*/
@PostMapping("/feign/factory/exception")
public String feignException() {return "feign "+ userFeign.exception(); } } ----------------------------------------------------------------------------- package com.example.user.feignfactory; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PostMapping; @FeignClient(value ="ACTIVITY", fallbackFactory = UserFeignFallBackFactory.class)
public interface UserFeign {
@PostMapping("/activity/loginActivity")
String loginActivity();
@PostMapping("/activity/timeout")
String timeout();
@PostMapping("/activity/exception")
String exception();
}
-----------------------------------------------------------------------------
package com.example.user.feignfactory;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
@Component
public class UserFeignFallBackFactory implements FallbackFactory<UserFeign> {
@Override
public UserFeign create(Throwable throwable) {
return new UserFeign() {
@Override
public String loginActivity() {
return "fallbackFactory loginActivity";
}
@Override
public String timeout() {
return "fallbackFactory timeout";
}
@Override
public String exception() {
return "fallbackFactory exception"; }}; } } ---------------------------feign--fallbackFactory----end----------------------------------Copy the code
# configuration
server:
port: 18080
servlet:
context-path: /user
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:18761/eureka
enabled: true
spring:
application:
name: user
management:
endpoints:
web:
base-path: /monitor
exposure:
include: "*"
endpoint:
health:
show-details: always
feign:
hystrix:
enabled: trueCopy the code