SpringCloud feign, eureka, hysrix 的简单使用

 使用的Spring Cloud 版本是:Finchley.SR1

 project:

 eureka-service: 服务治理

 consume: 消费服务

 provider: 提供服务

 client-api: 公共调用service封装

 

client-api:

定义 service, model, 熔断返回的fallback。

SpringCloud feign, eureka, hysrix 的简单使用_第1张图片

 

 

定义service接口:

@FeignClient(name = "provider", fallback = UserServiceFallback.class)
public interface UserService {

    @GetMapping("/svc/user/getUser")
    public User getUser();

}

熔断回调如下:

@Component
public class UserServiceFallback implements UserService{


    public User getUser() {
        User defaultUser = new User();
        defaultUser.setId("no found");
        defaultUser.setName("no user");
        return defaultUser;
    }
}

 

consumer: 

引入client-api,调用接口

@RestController
public class ConsumeRestController {

    @Autowired
    private UserService userService;

    @GetMapping("/api/user/get")
    public User getUser(){
        User user = userService.getUser();
        return user;
    }

}

provider:

也引入client-api

@RestController
public class UserSvcController implements UserService{
    @Override
    public User getUser() {
        User user = new User();
        user.setId("id");
        user.setName("ant");
        return user;
    }


}

 启动eureka-service, consumer,provider 访问相应的链接可测试。

源码地址:https://github.com/lifj/springCloudDemo

你可能感兴趣的:(SpringCloud feign, eureka, hysrix 的简单使用)