设置feign接口
@FeignClient(value = "leadnews-wemedia",fallback = IWemediaClientFallback.class)
@Qualifier("IWemediaClient")
public interface IWemediaClient {
@PostMapping("/api/v1/wemedia/list")
ResponseResult list(@RequestBody ChannelDto dto);
}
1. 设置FeignClient
value 指定需要调用的微服务
fallback 指定熔断机制
2. 设置PostMapping
PostMapping 指定调取feign的路径
熔断设置
@Component
public class IWemediaClientFallback implements IWemediaClient {
@Override
public ResponseResult list(ChannelDto dto) {
return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"获取数据失败");
}
}
@Configuration
@ComponentScan("com.heima.apis.admin.fallback")
public class InitConfig {
}
被调取者feign的微服务的实现
@RestController
public class WemediaClient implements IWemediaClient {
@Autowired
private WmChannelService wmChannelService;
@PostMapping("/api/v1/channel/list")
@Override
public ResponseResult list(ChannelDto dto) {
LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
if(StringUtils.isNotBlank(dto.getName())){
wrapper.eq(WmChannel::getName,dto.getName());
}
Page<WmChannel> wmChannelPage = new Page<>(dto.getPage(),dto.getSize());
wmChannelService.page(wmChannelPage,wrapper);
return ResponseResult.okResult(wmChannelPage);
}
}
将feign封装为controller
其他微服务调用/api/v1/article/save 就可以直接到list方法
调取者feign的微服务的实现
如何调取其他微服务呢
@Controller
@Slf4j
@Transactional
public class WemediaController {
@Autowired
@Qualifier("IWemediaClient")
private IWemediaClient iWemediaClient;
@PostMapping("/wemedia/api/v1channel/list")
public ResponseResult ChannelList(ChannelDto dto){
return iWemediaClient.list(dto);
}
}
只需要自动注入Client接口就行了,Autowired 会自动向上转型调用被调取者实现的feign的fang'fa