1、创建微服务消费者cloud-consumer-feign-order80服务
编写pom.xml依赖Jar包
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </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-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <dependency> <groupId>com.atguigu.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
编写application.yml配置文件
server: port: 80 spring: application: name: cloud-feign-order-service eureka: client: register-with-eureka: true fetch-registry: true service-url: # defaultZone: http://localhost:7001/eureka defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka instance: instance-id: feignOrder80 prefer-ip-address: true #客户端向Eureka 发送心跳的时间间隔 lease-renewal-interval-in-seconds: 1 lease-expiration-duration-in-seconds: 2
编写主启动类
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class OrderFeignApplication80 { public static void main(String[] args) { SpringApplication.run(OrderFeignApplication80.class,args); } }
编写基于Feign的接口类
@Component @FeignClient(value = "cloud-payment-service") public interface PaymentFeignService { @GetMapping(value = "/payment/get/{id}") public CommonResult getById(@PathVariable("id")Long id); }
编写业务类Controller 进行测试
@RestController @Slf4j @RequestMapping(value = "/consumer") public class OrderController { @Resource private PaymentFeignService paymentFeignService; @GetMapping(value = "/payment/get/{id}") public CommonResult getById(@PathVariable("id")Long id){ return paymentFeignService.getById(id); } }
浏览器访问测试地址:http://localhost/consumer/payment/get/1409365580926001153