-
Feign 采用的是基于接口的注解
-
Feign 整合了ribbon
创建一个名为eureka_feign的项目。
application.properties文件
server.port=10004 spring.application.name=eureka-feign #注册的eureka.Service信息 eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/ #注册成ip #spring cloud不同版本的配置可能不同,比较老的版本是eureka.instance.preferIpAddress=true #不行的话需要配置eureka.instance.instanceId eureka.instance.preferIpAddress=true spring.cloud.client.ipAddress=127.0.0.1 eureka.instance.instanceId=${spring.cloud.client.ipAddress}:${spring.application.name}:${server.port}
pom文件引用feign
<!-- cloud 版本问题 需要将feign改为openfeign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
启动class上添加@EnableFeignCliens
@EnableEurekaClient @EnableFeignClients @SpringBootApplication public class EurekaFeignApplication { public static void main(String[] args) { SpringApplication.run(EurekaFeignApplication.class, args); } }
新建controllet 文件夹和service 文件夹
创建service
//eureka-client 服务名称 @FeignClient(value = "eureka-client") public interface FeignServiceHi { //该服务的调用路径 @RequestMapping(value = "/hi") String home(@RequestParam String name); }
创建controllet
@RestController public class FeignController { @Autowired private FeignServiceHi feignServiceHi; @RequestMapping(value = "/fegin",method = RequestMethod.GET) public String fegin(@RequestParam String name){ return feignServiceHi.home(name); } }
启动原来的eureka项目和两个相关的client项目
访问http://127.0.0.1:10004/fegin?name=fegins
会有
hi fegins,i am from port:10003,
hi fegins,i am from port:10001
两个结果,负载到了eureka-client 的两个项目上。