이 기사는 Hystrix를 사용한 단일 방법의 폴백 인 Spring Cloud를 소개하고 다음과 같이 공유합니다.
1. Hystrix 종속성에 가입하십시오
<pectionency> <groupid> org.springframework.cloud </groupid> <artifactid> Spring-Cloud-Starter-Hystrix </artifactid> </fectionency>
2. 컨트롤러를 쓰십시오
패키지 com.chhliu.springboot.restful.controller; org.springframework.beans.factory.annotation.autowired; org.springframework.web.bind.annotation.getMapping; import org.springframework.web.bind.annotation.pathvariable; org.springframework.web.bind.annotation.RestController; com.chhliu.springboot.restful.feignclient.userfeignclient; import com.chhliu.springboot.restful.vo.user; import com.netflix.hystrix.contrib.javanica.annotation.hystrixcommand; @RestController 공개 클래스 RestTemPlateControllerHyStrixCommand {@autowired private userfeignclient client; // feign을 사용하여 편안한 서비스를 소비하기 위해 @getmapping ( "/get/{id}") @hystrixcommand (hystrixmethod = "findByidfallback") // hystrixcommand 주석을 사용하여 폴백 메드 속성 공개 사용자 findbyid (@PhathVariable Long ID) {returnbariable long id) {id); } // FallbackMethod에 지정된 메소드를 덮어 씁니다. 이 메소드의 반환 값은 원래 메소드 공개 사용자 FindByIdfallback (long id) {user u = new user ()와 일치해야합니다. U.setName ( "Zhangsan"); u.setusername ( "chhliu"); U.SetID (9L); u; }}3. 스타트 업 클래스에 Hystrix 지원을 추가하십시오
@EnableCircuitBreaker
4. 구성 파일을 추가하십시오
Server.port : 7904 # Eureka Server에 등록 된 Spring Boot 서비스의 응용 프로그램 이름은 Spring.application.name = Springboot-Rest-Template-Feign-Hystrix Eureka.instance.prefer-IP-Address = True # Eureka Server 등록 서비스의 주소입니다. eureka.client.service-url.defaultzone = http : // chhliu : chhliu123456@localhost : 8764/eureka springboot-h2.ribbon.nfloadbalancerRuleclassname = com.netflix.loadflix.loadbalancer.retryrule hystrix.command.default.execution.isolation.thread.timeoutinmilliseconds : 1 #to test hystrix의 폴백 효과, 시간 초과 시간은 1 밀리 초로 설정됩니다.
V. 테스트
브라우저를 입력하십시오 : http : // localhost : 7904/get/2
테스트 결과는 다음과 같습니다.
{ "id": 9, "username": "chhliu", "name": "zhangsan", "age": null, "balance": null}
위의 테스트 결과에서 연결 시간 초과이므로 폴백 메소드를 직접 입력 할 수 있습니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.