文
章
目
录
章
目
录
通过之前的学习,我们知道可以使用OpenFeign
代替Ribbon+RestTemplate
实现服务的负载均衡调用,下面我们来准备好演示环境。
第1步:创建Module
我们在mscloud
下创建名为cloud-consumer-feign-order80
模块
第2步:改pom
pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>mscloud</artifactId> <groupId>com.panziye.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-consumer-feign-order80</artifactId> <dependencies> <!-- OpenFign --> <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> <!-- 引入自定义的api通用包,可以使用支付Payment等Entity --> <dependency> <groupId>com.panziye.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</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.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> </project>
我们除了Eureka依赖还添加了OpenFeign依赖(如果你的依赖报错,请加入版本号),我们打开依赖查看,发现OpenFeign依赖以及集成了Ribbon
第3步:写yml
application.yml
如下:
server: port: 80 spring: application: name: cloud-order-feign-service eureka: client: #表示是否将自己注册进eureka服务中心,默认true register-with-eureka: false #表示是否从EurekaServer抓取已有注册信息,默认true。单节点无所谓,集群必须设置true才能配合ribbon使用负载均衡 fetch-registry: true service-url: #指向eureka集群 defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
第4步:主启动
新建OrderFeignMain80
主启动类:
package com.panziye.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableFeignClients public class OrderFeignMain80 { public static void main(String[] args) { SpringApplication.run(OrderFeignMain80.class,args); } }
提示:@EnableFeignClients用于激活OpenFeign客户端
第5步:业务类
1)在service包下新建OrderFeignService
接口:
注意:这里定义的接口应与服务提供者的service层接口对应,我这里只演示get,另外其返回值与服务提供者的controller对应,这就是我们希望的面向微服务接口编程。
package com.panziye.springcloud.service; import com.panziye.springcloud.entities.CommonResult; import com.panziye.springcloud.entities.Payment; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @Component @FeignClient(value = "CLOUD-PAYMENT-SERVICE") public interface OrderFeignService { //匹配服务提供者请求Mapping @GetMapping(value = "/payment/get/{id}") public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id); }
注意:@FeignClient注解表明此接口使用OpenFeign发请求,value指定服务提供者应用名称,@PathVariable不能丢
2)在controller包下新建OrderFeignController
类:
package com.panziye.springcloud.controller; import com.panziye.springcloud.entities.CommonResult; import com.panziye.springcloud.entities.Payment; import com.panziye.springcloud.service.OrderFeignService; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @Slf4j public class OrderFeignController { @Resource private OrderFeignService orderFeignService; @GetMapping("/consumer/payment/get/{id}") public CommonResult<Payment> getPayment(@PathVariable("id") Long id){ return orderFeignService.getPaymentById(id); } }
第6步:测试
我们先启动7001、7002、7003这3个eureka注册中心,再启动8001和8002这两个服务提供者,最后启动我们新建的OrderFeignMain80
,访问http://localhost/consumer/payment/get/1
测试,发现正常访问,默认使用的是Ribbon的轮询策略。