diff --git a/borrow-service/pom.xml b/borrow-service/pom.xml
index 0c8ffd8..bfdf0f7 100644
--- a/borrow-service/pom.xml
+++ b/borrow-service/pom.xml
@@ -18,6 +18,10 @@
UTF-8
+
+ org.springframework.boot
+ spring-boot-starter-actuator
+
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
diff --git a/borrow-service/src/main/java/com/test/controller/BorrowController.java b/borrow-service/src/main/java/com/test/controller/BorrowController.java
index 7a59bc4..7ee9ae1 100644
--- a/borrow-service/src/main/java/com/test/controller/BorrowController.java
+++ b/borrow-service/src/main/java/com/test/controller/BorrowController.java
@@ -25,16 +25,9 @@ public class BorrowController {
@Resource
BorrowService service;
- @HystrixCommand(fallbackMethod = "onError") //使用@HystrixCommand来指定备选方案
@RequestMapping("/borrow/{uid}")
UserBorrowDetail findUserBorrows(@PathVariable("uid") int uid){
return service.getUserBorrowDetailByUid(uid);
}
- //备选方案,这里直接返回空列表了
- //注意参数和返回值要和上面的一致
- UserBorrowDetail onError(int uid){
- System.out.println("执行补救措施");
- return new UserBorrowDetail(null, Collections.emptyList());
- }
}
diff --git a/borrow-service/src/main/java/com/test/service/client/BookClient.java b/borrow-service/src/main/java/com/test/service/client/BookClient.java
index e6ac7d7..08690dc 100644
--- a/borrow-service/src/main/java/com/test/service/client/BookClient.java
+++ b/borrow-service/src/main/java/com/test/service/client/BookClient.java
@@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
* @author yovinchen
* @Create 2023/8/15 16:56
*/
-@FeignClient("bookservice")
+@FeignClient(value = "bookservice", fallback = BookFallbackClient.class)
public interface BookClient {
@RequestMapping("/book/{bid}")
Book findBookById(@PathVariable("bid") int bid);
diff --git a/borrow-service/src/main/java/com/test/service/client/BookFallbackClient.java b/borrow-service/src/main/java/com/test/service/client/BookFallbackClient.java
new file mode 100644
index 0000000..8babc66
--- /dev/null
+++ b/borrow-service/src/main/java/com/test/service/client/BookFallbackClient.java
@@ -0,0 +1,21 @@
+package com.test.service.client;
+
+import com.test.entity.Book;
+import org.springframework.stereotype.Component;
+
+/**
+ * ClassName: BookFallbackClient
+ * Package: com.test.service.client
+ *
+ * @author yovinchen
+ * @Create 2023/8/15 20:37
+ */
+@Component //注意,需要将其注册为Bean,Feign才能自动注入
+public class BookFallbackClient implements BookClient {
+ @Override
+ public Book findBookById(int bid) { //这里我们自行对其进行实现,并返回我们的替代方案
+ Book book = new Book();
+ book.setTitle("我是替代方案");
+ return book;
+ }
+}
diff --git a/borrow-service/src/main/java/com/test/service/client/UserClient.java b/borrow-service/src/main/java/com/test/service/client/UserClient.java
index a6a488a..2e352ee 100644
--- a/borrow-service/src/main/java/com/test/service/client/UserClient.java
+++ b/borrow-service/src/main/java/com/test/service/client/UserClient.java
@@ -12,8 +12,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
* @author yovinchen
* @Create 2023/8/15 16:54
*/
-@FeignClient("userservice")
-//声明为userservice服务的HTTP请求客户端
+//fallback参数指定为我们刚刚编写的实现类
+@FeignClient(value = "userservice", fallback = UserFallbackClient.class)
public interface UserClient {
//路径保证和其他微服务提供的一致即可
diff --git a/borrow-service/src/main/java/com/test/service/client/UserFallbackClient.java b/borrow-service/src/main/java/com/test/service/client/UserFallbackClient.java
new file mode 100644
index 0000000..9e6323a
--- /dev/null
+++ b/borrow-service/src/main/java/com/test/service/client/UserFallbackClient.java
@@ -0,0 +1,21 @@
+package com.test.service.client;
+
+import com.test.entity.User;
+import org.springframework.stereotype.Component;
+
+/**
+ * ClassName: UserFallbackClient
+ * Package: com.test.service.client
+ *
+ * @author yovinchen
+ * @Create 2023/8/15 20:24
+ */
+@Component //注意,需要将其注册为Bean,Feign才能自动注入
+public class UserFallbackClient implements UserClient{
+ @Override
+ public User getUserById(int uid) { //这里我们自行对其进行实现,并返回我们的替代方案
+ User user = new User();
+ user.setName("我是替代方案");
+ return user;
+ }
+}
diff --git a/borrow-service/src/main/resources/application.yml b/borrow-service/src/main/resources/application.yml
index 4066dd6..4374193 100644
--- a/borrow-service/src/main/resources/application.yml
+++ b/borrow-service/src/main/resources/application.yml
@@ -15,3 +15,11 @@ eureka:
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
+feign:
+ circuitbreaker:
+ enabled: true
+management:
+ endpoints:
+ web:
+ exposure:
+ include: '*'
diff --git a/hystrix-dashboard/pom.xml b/hystrix-dashboard/pom.xml
new file mode 100644
index 0000000..1a4c08d
--- /dev/null
+++ b/hystrix-dashboard/pom.xml
@@ -0,0 +1,29 @@
+
+
+ 4.0.0
+
+ com.example
+ SpringCloudStudy
+ 0.0.1-SNAPSHOT
+
+
+ org.example
+ hystrix-dashboard
+
+
+ 8
+ 8
+ UTF-8
+
+
+
+
+ org.springframework.cloud
+ spring-cloud-starter-netflix-hystrix-dashboard
+ 2.2.10.RELEASE
+
+
+
+
diff --git a/hystrix-dashboard/src/main/java/com/test/HystrixDashBoardApplication.java b/hystrix-dashboard/src/main/java/com/test/HystrixDashBoardApplication.java
new file mode 100644
index 0000000..a722f1a
--- /dev/null
+++ b/hystrix-dashboard/src/main/java/com/test/HystrixDashBoardApplication.java
@@ -0,0 +1,20 @@
+package com.test;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
+
+/**
+ * ClassName: HystrixApplication
+ * Package: com.test
+ *
+ * @author yovinchen
+ * @Create 2023/8/15 21:19
+ */
+@SpringBootApplication
+@EnableHystrixDashboard
+public class HystrixDashBoardApplication {
+ public static void main(String[] args) {
+ SpringApplication.run(HystrixDashBoardApplication.class, args);
+ }
+}
diff --git a/hystrix-dashboard/src/main/resources/application.yml b/hystrix-dashboard/src/main/resources/application.yml
new file mode 100644
index 0000000..e7cfa1c
--- /dev/null
+++ b/hystrix-dashboard/src/main/resources/application.yml
@@ -0,0 +1,6 @@
+server:
+ port: 8900
+hystrix:
+ dashboard:
+ # 将localhost添加到白名单,默认是不允许的
+ proxy-stream-allow-list: "localhost"
diff --git a/pom.xml b/pom.xml
index ff46ad2..1a9bae5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -20,6 +20,7 @@
book-service
commons
eureka-service
+ hystrix-dashboard
1.8