当前位置: 首页 > article >正文

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);
    }
}

http://www.kler.cn/a/378520.html

相关文章:

  • HarmonyOS 移动应用开发
  • 【docker】docker 环境配置及安装
  • 力扣排序350题 两个元组的交集2
  • Python酷库之旅-第三方库Pandas(193)
  • 【Java知识】Java基础-对象排序的实现
  • 03:选择语句的练习
  • vue-i18n国际化多国语言i18n国际语言代码对照表
  • Python | Leetcode Python题解之第525题连续数组
  • 项目总结(3)
  • Apache 配置出错常见问题及解决方法
  • CSS学习之Grid网格布局基本概念、容器属性
  • OpenCV自动滑块验证(Java版)
  • 数据库基础(1) . 关系型数据库
  • eclipse下载与安装(汉化教程)超详细
  • filebeat+elasticsearch+kibana日志分析
  • java项目之微服务在线教育系统设计与实现(springcloud)
  • Python爬虫的“京东大冒险”:揭秘商品类目信息
  • Golang gRPC
  • Pycharm,2024最新专业版下载安装配置详细教程!
  • uni-app使用movable-area 实现数据的拖拽排序功能
  • 链表逆置相关算法题|原地逆置|轮转链表|循环链表逆置(C)
  • vscode markdown-image 图片粘贴自动上传到本地目录设置
  • 11月3日笔记(根据凭据提权)
  • Manus Metagloves Pro虚拟现实手套
  • java项目之协力服装厂服装生产管理系统的设计与实现(springboot)
  • Spring Boot框架下的信息学科平台系统架构设计