1、接口+注解
微服务调用接口+@FeignClient
微服务调用接口:指的是提供者所提供的接口和我消费者想要调用的接口吻合才能匹配
2、新建cloud-consumer-feign-order80
3、POM
这里其实就是加入了openfeign
<dependencies><!--openfeign--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!--eureka client--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>com.tfjy.springcloud</groupId><artifactId>cloud-api-commons</artifactId><version>1.0-SNAPSHOT</version></dependency><!--web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!--一般基础通用配置--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>
4、YML
feign就是一个客户端所以就不注册进eureka
server:port: 80eureka:client:register-with-eureka: falseservice-url:defaultZone: http://localhost:7001/eureka/
5、主启动
因为加了openfeign注解,所以需要在主启动上面加上EnableFeignClients去启动。
6、业务
现在8001提供了两个
消费者
现在消费者调用服务者其实是和服务者的controller保持一致的,

所以说总结一下业务的话,第一步在消费端的service接口中加入FeignClient然后调用接口即可。
7、测试
先启动2个eureka集群7001/7002
再启动2个微服务8001/8002
启动OpenFeign启动
这里我只启动了3个服务,一个是注册中心Eureka一个是服务的提供者,另一个就是消费者OrderFeign
总结:在调用者生成service接口的代理对象,因为service接口是不能直接调用的。
http://t.zoukankan.com/lay2017-p-11946324.html
