1.创建模块
2.引入依赖
<dependencies>
<dependency>
<groupId>cn.aib.springcloud</groupId>
<artifactId>springclud-api-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- open feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
3.改配置
server: port: 80 eureka: client: register-with-eureka: false #是否将自己注册到注册中心,集群必须设置为true配合ribbon service-url: defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
由于Feign是webService客户端,不需要到注册中心上注册。但如果是直接的Ribbon+Eureka的话,调用方是需要到注册中心注册的。
4.主启动
@SpringBootApplication @EnableFeignClients public class OrderFeignApplication { public static void main(String[] args) { SpringApplication.run(OrderFeignApplication.class,args); } }
记得开启Feign
5.写业务
@Component @FeignClient("cloud-payment-service") public interface PaymentFeignService { @GetMapping("/payment/get/{id}") public CommonResult selectPayment(@PathVariable("id") Long id); }
@RestController public class FeignController { @Resource private PaymentFeignService paymentFeignService; @GetMapping(value = "/consumer/payment/get/{id}") public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){ return paymentFeignService.selectPayment(id); } }
6.测试。