天天看点

SpringCloud学习[6]-OpenFeign组件服务间通信

使用RestTemplate+ribbon已经可以完成对服务间的调用,为什么还要使用feign?

String getResult = restTemplate.getForObject(“http://{{服务ID}}/{{url}}”, String.class);
  • 存在问题
  1. 每次调用服务都需要写这些代码,存在大量的代码冗余
  2. 服务地址如果修改,维护成本增高
  3. 不能自动转换响应结果为对应对象
  4. 必须集成ribbon实现负载均衡

OpenFeign组件

一、简介

1. 发展

Feign组件 ------> OpenFiegn组件

Feign(Netflix)组件后期进入维护状态,因为Feign提供了非常好的服务间调用思想,但是由于进入了维护,后续feign组件的状态也没人管理,开发人员也享受不到feign组件的特性。springcloud作为最高提出微服务的大佬,总得给广大开发人员提出微服务的解决方案,所以springcloud团队开始吸收开源feign组件的精华,封装了OpenFeign组件。为了减轻开发人员的学习成本,并未对feign进行大改造,仅仅吸收了feign组件的精华。OpenFeign(Spring)和Feign(Netflix)组件特性使用方式一致的。

2. 介绍

官方网址:https://cloud.spring.io/spring-cloud-openfeign/reference/html/

Declarative REST Client: Feign

Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and for using the same HttpMessageConverters used by default in Spring Web. Spring Cloud integrates Eureka, as well as Spring Cloud LoadBalancer to provide a load-balanced http client when using Feign.

译:Feign是一个声明式 Web 服务客户端。它使编写 Web 服务客户端变得更容易。要使用 Feign 创建一个接口并对其进行注释。它具有可插入的注释支持,包括 Feign 注释和 JAX-RS 注释。Feign 还支持可插拔的编码器和解码器。Spring Cloud 添加了对 Spring MVC 注解的支持,并支持使用HttpMessageConvertersSpring Web 中默认使用的注解。Spring Cloud 集成了 Eureka,以及 Spring Cloud LoadBalancer,在使用 Feign 时提供负载均衡的 http 客户端。

OpenFeign和RestTemplate作用一致,都是一个http客户端

  • RestTemplate:Spring框架封装HttpClient对象
  • OpenFeign:伪HttpClient对象(可借鉴代理服务器理解)它可以使服务间通信更简单

二、使用

1. 创建两个springboot应用

FriendApplication 调用 ShareApplication

<!--springboot-->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
           

2. 注册到consul注册中心

引入consul依赖

<!--引入consul依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <!--健康检查依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
           

修改配置文件,向consul注册服务

# 注册到consul
spring.cloud.consul.host=192.168.0.133
spring.cloud.consul.port=8500

# 指定注册服务的服务名称,默认${spring.application.name}
spring.cloud.consul.discovery.service-name=${spring.application.name}

# 注册时按我的ip地址注册
spring.cloud.consul.discovery.prefer-ip-address=true
spring.cloud.consul.discovery.ip-address=192.168.0.119
           

3. 入口类加入服务注册client注解

@SpringBootApplication
@EnableDiscoveryClient
public class FriendsApplication {

    public static void main(String[] args) {
        SpringApplication.run(FriendsApplication.class, args);
    }

}
           

4. 启动查看consul web端

SpringCloud学习[6]-OpenFeign组件服务间通信

5. 使用OpenFeign进行服务调用

在服务调用方引入openfeign依赖

<!--openfeign依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
           

在服务调用方入口类加入注解,开启openfeign调用支持

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FriendApplication {

    public static void main(String[] args) {
        SpringApplication.run(FriendApplication.class, args);
    }

}
           

在调用方开发需要调用的服务客户端接口

/**
 * 调用Share服务的接口
 */
@FeignClient(value = "SHARE") // value=调用服务的ID
public interface ShareClients {

    /**
     * 调用Share服务 share接口
     * @return
     */
    @GetMapping("shareConApi/share")
    public String share();

}
           

FriendController

@RestController
@RequestMapping("friendConApi")
public class FriendController {

    private static Logger log = LoggerFactory.getLogger(FriendController.class);
    @Resource
    private ShareClients shareClients;

    @GetMapping("invokeShare")
    public String invokeShare() {
        String share = shareClients.share();
        log.info("invokeShare success={}", share);
        return "invokeShare success " + share;
    }
}
           
SpringCloud学习[6]-OpenFeign组件服务间通信
SpringCloud学习[6]-OpenFeign组件服务间通信

默认轮询的负载均衡策略

三、OpenFeign服务间调用之参数传递

https://blog.csdn.net/qq_31832209/article/details/118384828?spm=1001.2014.3001.5501

四、OpenFeign服务间调用之响应处理

继续阅读