Introduction of Hystrix in Ribbon
- Start Eureka-server, Compute-service, Eureka-ribbon project in turn
- Access http://localhost:1111/to see the status of the registry
- Access Http://localhost:3333/add, call Eureka-ribbon's service, the service will go to invoke Compute-service service, calculate the value of 10+20, page display 30
- Close the Compute-service service, Access Http://localhost:3333/add, we get the following error message
for This 21:16:59 CST There is anunexpected error (type=internal Server error, status=500). I for "http://COMPUTE-SERVICE/add?a=10&b=20": Connection refused:connect; Nested exception is java.net.ConnectException:Connection refused:connect
pom.xml
Relies on Hystrix dependency in the import
<dependency><groupId>org.springframework.cloud</groupId><artifactId> Spring-cloud-starter-hystrix</artifactid></dependency>
RibbonApplication
use @EnableCircuitBreaker
Annotations to turn on circuit breaker functionality in the main class of Eureka-ribbon:
@SpringBootApplication @enablediscoveryclient@enablecircuitbreaker Public class ribbonapplication {@Bean @loadbalancedresttemplate resttemplate () {returnnew Public Static void Main (string[] args) {Springapplication.run (ribbonapplication. class , args);} }
- Transform the original service consumption method, add a
ComputeService
class, add annotations to the function that uses the Ribbon consumer service @HystrixCommand
to specify the callback method.
@Service Public class = "Addservicefallback") public String AddService () {return Resttemplate.getforentity ("Http://COMPUTE-SERVICE/add?a=10&b=20", String. Class public String addservicefallback () {return "error";}}
- The controller that provides the rest interface changes to call Computeservice's AddService
@RestController Public class Consumercontroller {@Autowiredprivate= "/add", method = requestmethod.get) Public String Add () {return computeservice.addservice ();}}
- Verify the callback for the circuit breaker
- Start Eureka-server, Compute-service, Eureka-ribbon project in turn
- Access http://localhost:1111/to see the status of the registry
- Visit Http://localhost:3333/add, page display: 30
- After you close the Compute-service service and then visit Http://localhost:3333/add, the page displays: Error
For more information on the use of Hystrix, refer to
Feign using HystrixNote that this is said to be "use", there is no mistake, we do not need to introduce hystix,feign in the Feigh project has relied on hystrix, we can do before any modification, try the following your operation:
- Start Eureka-server, Compute-service, Eureka-feign project in turn
- Access http://localhost:1111/to see the status of the registry
- Access Http://localhost:3333/add, call Eureka-feign's service, the service will go to invoke Compute-service service, calculate the value of 10+20, page display 30
- Close the Compute-service service, Access Http://localhost:3333/add, we get the following error message
for This 22:10:05 CST There is anunexpected error (type=internal Server error, status=500). Add timed -out and no fallback available.
If you are careful enough, you will find that the error in the Ribbon is different, see add timed-out and no fallback available
this sentence, perhaps you have guessed what, see our console, you can see the error message from hystrix-core-1.5.2.jar
, so in this project, we have to learn how to use feign integrated hystrix.
- Use
@FeignClient
the Fallback property in annotations to specify the callback class
@FeignClient (value = "Compute-service", fallback = Computeclienthystrix. Class)publicinterface= requestmethod.get, value = "/add"= "a") Integer A, @RequestParam (value = "B") integer b); }
- Create the callback class
ComputeClientHystrix
, implemented @FeignClient
by the interface, at this time the implementation of the method is the corresponding @FeignClient
interface map of the fallback function.
@Component Public class Implements computeclient {@Override Public integer Add (@RequestParam (value = "a") integer A, @RequestParam (value = "B") Integer b) {return -9999;}}
- Then verify with the previous method that the page returns 9999 if the Compute-service service is unavailable.
- For more information on how to use feign, refer to: Feign