以两个模块(batch,business)为例子,期望实现batch调用business中的hello接口
在主程序batch中引入pom文件
<!--远程调用openfeign--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!--openfeign默认使用的是loadBalance的负载均衡器 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-loadbalancer</artifactId></dependency>
在business中的application.properties设置app名
spring.application.name=business
batch创建feign包
新建文件businessFeign.java
@FeignClient(name="business",url = ("http://127.0.0.1:8002/business"))
public interface businessFeign {@GetMapping("/hello")String hello();
}
batch启动类中增加注解
@EnableFeignClients("com.fugui.train.batch.feign")
新建test.java以测试是否成功
@RestController
public class Test {@Resourceprivate businessFeign feign;@GetMapping("/hello")public String hello(){return feign.hello();}
}
重启项目