前言
前面我们已经实现了服务的注册与发现 (请戳: SpringCloud 系列 --Eureka 服务注册与发现), 并且在注册中心注册了一个服务 myspringboot, 本文记录多个服务之间使用 Feign 调用.
Feign 是一个声明性 web 服务客户端. 它使编写 Web 服务客户机变得更容易, 本质上就是一个 http, 内部进行了封装而已.
GitHub 地址: https://github.com/OpenFeign/feign
官方文档:
服务提供者
提供者除了要在注册中心注册之外, 不需要引入其他东西, 注意一下几点即可:
1, 如果使用对象接参, 必须使用 @RequestBody, 否则接不到数据
2, 接参只能出现一个复杂对象, 例: public Result<List<UserVo>> list(@RequestBody UserVo entityVo) { ... }
3, 提供者如果又要向其他消费者提供服务, 又要向浏览器提供服务, 建议保持原先的 Controller, 新建一个专门给消费者的 Controller
服务消费者
消费者 maven 引入 jar
- <!-- feign -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-openfeign</artifactId>
- </dependency>
配置文件
对日期的解析, 消费者要跟提供者一致, 不然会报 JSON 解析错误
- # 超时时间
- feign.httpclient.connection-timeout=30000
- #mvc 接收参数时对日期进行格式化
- spring.mvc.date-format=yyyy-MM-dd HH:mm:ss
- #jackson 对响应回去的日期参数进行格式化
- spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
- spring.jackson.time-zone=GMT+8
服务调用
1,springdatejpa 应用名称, 是服务提供者在 eureka 注册的名字, Feign 会从注册中心获取实例
2, 如果不想启动 eureka 服务, 直连本地开发:@FeignClient(name = "springdatejpa", path = "/user/",url = "http://localhost:10086")
3, 如果使用 @RequestMapping, 最好指定调用方式
4, 消费者的返回值必须与提供者的返回值一致, 参数对象也要一致
更多 @FeignClient 注解参数配置, 请参阅官方文档
- @FeignClient(name = "springdatejpa", path = "/user/")
- public interface MyspringbootFeign {
- @RequestMapping(value = "get/{id}")
- Result<UserVo> get(@PathVariable("id") Integer id);
- @RequestMapping(value = "list", method = RequestMethod.GET)
- Result<List<UserVo>> list(@RequestBody UserVo entityVo);
- }
- /**
- * controller 调用
- */
- @GetMapping("feign/list")
- Result<List<UserVo>> list(UserVo userVo){
- return myspringbootFeign.list(userVo);
- }
启动类
启动类加入注解:@EnableFeignClients
- @EnableEurekaClient
- @EnableFeignClients
- @SpringBootApplication
- public class MyspringbootApplication{
- public static void main(String[] args) {
- SpringApplication.run(MyspringbootApplication.class, args);
- }
- }
效果
成功注册两个服务
成功调用
报错记录
当我将之前搭好的一个 springboot-springdata-jpa 整合项目在 eureka 注册时出现了一个报错
然后在网上查了下说是因为 springboot 版本问题 (请戳: http://www.cnblogs.com/hbbbs/articles/8444013.html), 之前这个项目用的是 2.0.1.RELEASE, 现在要在 eureka 注册, pom 引入了就出现了上面的报错
- <!-- eureka-client -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- <!-- actuator -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>Greenwich.RC1</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <repositories>
- <repository>
- <id>spring-milestones</id>
- <name>Spring Milestones</name>
- <url>https://repo.spring.io/milestone</url>
- </repository>
- </repositories>
解决: 升级了 springboot 版本, 2.1.0, 项目正常启动
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.0.RELEASE</version>
- <!--<version>2.0.1.RELEASE</version>-->
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
来源: https://www.cnblogs.com/huanzi-qch/p/10135946.html