Controller调用@FeignClient
shop-user模块
shop-user-client模块
提供远程调用的接口依赖spring-cloud-openfeign-core:2.2.2.RELEASE,以及调用失败的类
//shop-user模块下的/user
@FeignClient(value=ConstantsClient.USER_CLIENT,path=ConstantsClient.USER,fallbackFactory = AddressFallbackFactory.class)//
public interface AddressClient{
//查询用户默认地址
@RequestMapping(value="/address/default/{userId}",method=RequestMethod.POST)
Response<AddressResponse> address(@PathVariable("userId") Long userId);
//获取地址信息
@RequestMapping(value="/address/{addressId}/info",method=RequestMethod.POST)
Response<AddressResponse> info(@PathVariable("addressId") Long addressId);
}
AddressFallbackFactory
// io.github.openfeign:feign-hystrix:10.7.4
@Component
public class AddressFallbackFactory implements FallbackFactory<AddressClient>{
@Override
public AddressClient create(Throwable cause){
return null;
}
}
shop-user-api模块
业务上的Controller接口,进行调用具体的Service服务
@RestController
@RequestMapping(ConstantsClient.USER) //路径:/user
public class AddressController implements AddressClient{
@Autowired
private AddressService;
@Override
@PostMapping("/address/default/{userId}")
public Response<AddressResponse> address(@PathVariable("userId") Long userId){
return addressService.address(userId);
}
@Override
@PostMapping("/address/{addressId}/info")
public Response<AddressResponse> info(@PathVariable("addressId") Long addressId) {
return Response.toResponse(addressService.queryByPrimaryKey(addressId));
}
}
启动类开启注解@EnableFeignClients
@SpringCloudApplication
@EnableFeignClients
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class User{
public static void main(String[] args) {
SpringApplication.run(User.class, args);
}
}
=================================================
spring-cloud-openfeign-core强化
①、UserClient接口以及UserFallbackFactory
/**
shop-user /user
*/
@FeignClient(value=ConstantsClient.USER_CLIENT,path=ConstantsClient.USER,fallbackFactory = UserFallbackFactory.class)
public interface UserClient{
@RequestMapping(value="/profile/{userId}",method=RequestMethod.Post)
Response<UserProfileResponse> profile(@PathVariable("userId") Long userId);
@RequestMapping(value = "/profile/{phone}/info", method = RequestMethod.POST)
Response<UserProfileResponse> profileInfo(@PathVariable("phone") String phone);
//UserRequest参数已被封装为继承Serializable的实体类
@RequestMapping(value = "/batch/profile", method = RequestMethod.POST)
Response<List<UserProfileResponse>> batchProfile(@RequestBody UserRequest request);
}
@Slf4j
@Component
public class UserFallbackFactory implements FallbackFactoty<UserClient>{//feign-hystrix:10.7.4
@Override
public UserClient create(Throwable cause){
log.error("UserClient 进入熔断措施 msg = {}", cause.getMessage());
return new UserClienet(){
@Override
public Response<UserProfileResponse> profile(Long userId){
log.error("进入熔断措施 UserClient.profile");
return Response.toError(ResponseStatus.Code.EXCEPTION_CODE, ResponseStatus.PARAMS_EXCEPTION);//3000 网络出点小问题
}
@Override
public Response<UserProfileResponse> profileInfo(String phone){
log.error("进入熔断措施 UserClient.profileInfo");
return Response.toError(ResponseStatus.Code.EXCEPTION_CODE, ResponseStatus.PARAMS_EXCEPTION);
}
@Override
public Response<List<UserProfileResponse>> batchProfile(UserRequest request){
log.error("进入熔断措施 UserClient.batchProfile");
return Response.toError(ResponseStatus.Code.EXCEPTION_CODE, ResponseStatus.PARAMS_EXCEPTION);
}
}
}
}
②、业务Controller接口进行继承UserClient接口
@RestController
@RequestMapping(ConstantsClient.USER)//路径/user
public class UserController implements UserClient{
@Autowired
private UserService userService;
@Override
@PostMapping("/profile/{userId}")
public Response<UserProfileResponse> profile(@PathVariable("userId") Long userId) {
return userService.profile(userId);
}
@Override
@PostMapping("/profile/{phone}/info")
public Response<UserProfileResponse> profileInfo(@PathVariable("phone") String phone) {
return userService.profileInfo(phone);
}
@Override
@PostMapping("/batch/profile")
public Response<List<UserProfileResponse>> batchProfile(@RequestBody UserRequest request) {
return userService.batchProfile(request);
}
}