简单记录一下feign实现的远程调用。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
开启hystrix配
#开启hystrix配置
feign.hystrix.enabled=true
#根据你的需求添加以下配置
#feign.client.config.default.connect-timeout=10000
#feign.client.config.default.read-timeout=10000
#feign.client.config.default.logger-level=basic
#请求连接的超时时间
ribbon.ReadTimeout=30000
#禁用hystrix:
hystrix.command.default.execution.timeout.enabled=false
3.启动类上添加注解
@SpringBootApplication
//?? 这两个注解很重要
@EnableFeignClients(basePackages = {"com.xxx.xxfeign"})
@EnableDiscoveryClient
//??
@MapperScan("com.xx.xx.dao")
@EnableDistributedTransaction
public class DocsApplication {
public static void main(String[] args) {
SpringApplication.run(DocsApplication.class, args);
}
}
4.写好方法, ??????@RequestParam() 注意添加这个注解,这个注解可以让你传递调用方法参数,不至于调用时候传Null
@RequestMapping(value = "/getInfo", method = RequestMethod.POST)
//??这个注解很重要!!!
public CrmUserInfoEntity GetUserInfo(@RequestParam("id") Long id){
CrmUserInfoEntity all = crmUserInfoIService.findAll(id);
return all;
}
?
// crmUserInfoIService 的实现方法就不给出了(简单的查询)。
声明调用
@Component
@FeignClient(value = "你的server-name",configuration = FeignConfiguration.class)
public interface CmsFeign {
?
@RequestMapping(value = "/crmUserInfo/getInfo", method = RequestMethod.POST)
CrmUserInfoEntity GetUserInfo(@RequestParam("id")Long id);
}
//configuration = FeignConfiguration.class 这个是自定义的配置,根据你的需求添加,不添加也不会报错;
//不添加去除@Component
远程调用实现
@Resource
private CmsFeign cmsFeign;
?
public String xxx(){
long i = 9527;
CrmUserInfoEntity info = cmsFeign.GetUserInfo(i);
system.out.println("远程调用返回了啥:"+info);
return null;
}
至此feign的基本调用结束 ??????
原文:https://www.cnblogs.com/TheShieldOfValoran/p/14657173.html