SpringCloud Feign 服务调用 SpringCloud Feign 服务调用的实现

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringCloud Feign 服务调用 SpringCloud Feign 服务调用的实现

huanzi-qch   2021-03-30 我要评论

前言

前面我们已经实现了服务的注册与发现(请戳:SpringCloud系列——Eureka 服务注册与发现),并且在注册中心注册了一个服务myspringboot,本文记录多个服务之间使用Feign调用。

Feign是一个声明性web服务客户端。它使编写web服务客户机变得更容易,本质上就是一个http,内部进行了封装而已。

GitHub地址:https://github.com/OpenFeign/feign

官方文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.1.0.RC2/single/spring-cloud-openfeign.html

服务提供者

提供者除了要在注册中心注册之外,不需要引入其他东西,注意一下几点即可:

1、如果使用对象接参,必须使用@RequestBody,否则接不到数据

2、接参只能出现一个复杂对象,例:public Result<List<UserVo>> list(@RequestBody UserVo entityVo) { ... }

3、提供者如果又要向其他消费者提供服务,又要向浏览器提供服务,建议保持原先的Controller,新建一个专门给消费者的Controller

测试接口

@RestController
@RequestMapping("/user/")
public class UserController {

  @Autowired
  private UserService userService;
  @RequestMapping("list")
  public Result<List<UserVo>> list(@RequestBody UserVo entityVo) {
    return userService.list(entityVo);
  }

  @RequestMapping("get/{id}")
  public Result<UserVo> get(@PathVariable("id") Integer id) {
    return userService.get(id);
  }
}

服务消费者

消费者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);
}
/**
   * feign调用
   */
  @GetMapping("feign/get/{id}")
  Result<UserVo> get(@PathVariable("id") Integer id){
    return myspringbootFeign.get(id);
  }


  /**
   * feign调用
   */
  @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);
  }

}

效果

成功注册两个服务

成功调用

报错记录

1、启动时报了个SQL错误

解决:配置文件连接数据时指定serverTimezone=GMT%2B8

2、当我将之前搭好的一个springboot-springdata-jpa整合项目在eureka注册时出现了一个报错

然后在网上查了下说是因为springboot版本问题,之前这个项目用的是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>

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们