Hystrix监控服务整合完成
This commit is contained in:
		@@ -18,6 +18,10 @@
 | 
			
		||||
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 | 
			
		||||
    </properties>
 | 
			
		||||
    <dependencies>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>org.springframework.boot</groupId>
 | 
			
		||||
            <artifactId>spring-boot-starter-actuator</artifactId>
 | 
			
		||||
        </dependency>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>org.springframework.cloud</groupId>
 | 
			
		||||
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
 | 
			
		||||
 
 | 
			
		||||
@@ -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());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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 {
 | 
			
		||||
 | 
			
		||||
    //路径保证和其他微服务提供的一致即可
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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: '*'
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user