บทความนี้แนะนำ Spring Cloud - ทางเลือกของวิธีเดียวโดยใช้ Hystrix และแบ่งปันกับคุณดังนี้:
1. เข้าร่วมการพึ่งพา Hystrix
<การพึ่งพา> <roupID> org.springframework.cloud </groupId> <ratifactid> Spring-Cloud-Starter-Hystrix </artifactId>
2. เขียนคอนโทรลเลอร์
แพ็คเกจ com.chhliu.springboot.restful.controller; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.web.bind.annotation.getMapping; นำเข้า org.springframework.web.bind.annotation.pathvariable; นำเข้า org.springframework.web.bind.annotation.restcontroller; นำเข้า com.chhliu.springboot.restful.feignlient.userfeignlient; นำเข้า com.chhliu.springboot.restful.vo.user; นำเข้า com.netflix.hystrix.contrib.javanica.annotation.hystrixCommand; @RestController คลาสสาธารณะ RESTTEMPLATECONTROLLERHYSTRIXCOMMAND {@AUTOWIRED Private UserFeignClient ไคลเอ็นต์; // ใช้ feign เพื่อใช้บริการพักผ่อน @getMapping ("/get/{id}") @HyStrixCommand (fallbackMethod = "findByidFallback") // ใช้คำอธิบายประกอบ HystrixCommand เพื่อระบุวิธีการโทรกลับ } // เขียนทับวิธีที่ระบุไว้ใน FlackMethod โปรดทราบว่าค่าการส่งคืนของวิธีนี้จะต้องสอดคล้องกับวิธีดั้งเดิมผู้ใช้สาธารณะ FindByIdFallback (Long ID) {ผู้ใช้ U = ผู้ใช้ใหม่ (); U.SetName ("จาง"); u.setusername ("chhliu"); U.SetId (9L); กลับคุณ; -3. เพิ่มการสนับสนุน Hystrix ในคลาสเริ่มต้น
@enablecircuitbreaker
4. เพิ่มไฟล์การกำหนดค่า
เซิร์ฟเวอร์พอร์ต: 7904 # ชื่อแอปพลิเคชันของบริการสปริงบูตลงทะเบียนไปยังเซิร์ฟเวอร์ยูเรก้าคือสปริงแอพพลิเคชั่น = Springboot-Rest-Template-Feign-Hystrix Eureka.instance.prefer-ip-address = True # ที่อยู่ของบริการลงทะเบียนเซิร์ฟเวอร์ Eureka Eureka.client.service-url.defaultzone = http: // chhliu: chhliu123456@localhost: 8764/eureka springboot-h2.ribbon.nfloadbalancerranclass Hystrix.Command.default.execution.isolation.thread.timeoutinmilliseconds: 1 #to Test Effect ทางเลือกของ Hystrix TEST เวลาหมดเวลาถูกตั้งไว้ที่ 1 มิลลิวินาทีที่นี่
V. ทดสอบ
ป้อนในเบราว์เซอร์: http: // localhost: 7904/get/2
ผลการทดสอบมีดังนี้:
{"id": 9, "ชื่อผู้ใช้": "chhliu", "name": "Zhangsan", "อายุ": null, "Balance": Null}
จากผลการทดสอบข้างต้นเราจะเห็นได้ว่าเนื่องจากการหมดเวลาการเชื่อมต่อเราป้อนวิธีทางเลือกโดยตรง
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น