【feign vs openFeign及openFeign的简单应用】

Feign vs OpenFeign
feign
Feign是Springcloud组件中的一个轻量级Restful的HTTP服务客户端,Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。Feign的使用方式是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务。Feign本身不支持Spring MVC的注解,它有一套自己的注解。
Feign 本身已经集成了Ribbon依赖和⾃动配置,因此我们不需要额外引⼊依赖,可以直接通过配置来设置负载均衡;Feign内置的有Hystrix功能,因此使用的时候可以对Feign客户端接口进行熔断处理。
openfeign
OpenFeign 是一个声明式、模板化的 RESTful 网络请求客户端。OpenFeign是springcloud在Feign的基础上支持了SpringMVC的注解,如@RequestMapping等等,OpenFeign 也支持多种自带的注解。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。 OpenFeign 会根据带有注解的函数信息构建出网络请求的模板,在发送网络请求之前,OpenFeign 会将函数的参数值设置到这些请求模板中。
比较:
OpenFeign是由Feign演变过来,平时说的Feign指的是Netflix旗下的Feign,现在我们使用的是OpenFeign是Pivotal 提供的。
Feign不支持SpringMVC的注解,OpenFeign是springcloud在Feign的基础上支持了SpringMVC的注解

使用OpenFeign

前提:
目前我们有两个微服务video和score,他们之间存在调用关系,video 要调用score 的接口,此时,使用OpenFeign我们应该怎么做呢?
在score-api中暴露相应的接口,video中引入score-api的依赖,然后调用。
步骤:

1.1 配置pom.xml,导入依赖

找到服务消费者 video ,修改pom.xml添加依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <exclusions>
        <exclusion>
            <artifactId>guava</artifactId>
            <groupId>com.google.guava</groupId>
        </exclusion>
        <exclusion>
            <artifactId>archaius-core</artifactId>
            <groupId>com.netflix.archaius</groupId>
        </exclusion>
        <exclusion>
            <artifactId>spring-cloud-netflix-ribbon</artifactId>
            <groupId>org.springframework.cloud</groupId>
        </exclusion>
    </exclusions>
</dependency>

这里简单介绍一下maven的exclusions标签:排除因当前依赖而间接引入的某个依赖

为什么要这样做?

在写pom的时候,我们写的一个依赖往往会依赖于其他的包,而这些包可能是过时的不安全的或者我们不需要的,因此需要排除并重新引用安全的版本,先在依赖这个项目的pom中去除想排除的依赖,再添加指定版本的依赖。

如果有这样的依赖关系:projectA->projectB->projectC,但是projectA不依赖projectC,那么应该怎么做呢?

在projectA中的pom.xml中添加如下配置:

<dependency>
      <!-- projectA依赖projectB -->
      <groupId>example.ProjectB</groupId>
      <artifactId>ProjectB</artifactId>
      <version>2.0</version>
      <!-- 排除projectA对projectC的依赖 -->
      <exclusions>
        <exclusion>
          <groupId>example.ProjectC</groupId>
          <artifactId>ProjectC</artifactId>
        </exclusion>
      </exclusions>
</dependency>

1.2服务消费者主启动类加上注解@EnableFeignClients注解,对应到我们这里是video的主启动类

@EnableFeignClients申明该项目是Feign客户端,扫描对应的FeignClient。

@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class ScoreApplication {
    public static void main(String[] args) {
        SpringApplication.run(ScoreApplication.class,args);
    }

    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

1.3 新增提供者API接口并添加@FeignClient注解

我们需要集中化管理API,就可以通过接口统一管理,需要新增添加积分接口AddScoreFeignApi,其实可以简单理解成把之前我们写好的接口抽象出API,方便大家调用,在调用时引入相应API即可;

并添加@FeignClient(value = "video")注解,其中name/value就是我们要访问的微服务的名称。addScore方法中@GetMapping("/addScore/videoAddScore")是该服务提供者的接口路径。

@FeignClient(value = "score",fallback = AddScoreFeignHystrix.class)
public interface AddScoreFeignApi {
    @GetMapping("/score/addScore")
    public selectCommentOpenFeign(@RequestParam("videoId") String videoId);

}

与此同时,我们也要给服务提供者score-api添加OpenFeign的依赖

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

1.4服务消费者调用服务提供者提供的api

首先,需要在服务消费者score的pom.xml文件中引入服务提供者的api video-api依赖

<!--调用其他服务的feign-->
<dependency>
    <groupId>com.tfjybj</groupId>
    <artifactId>video-api</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</dependency>

调用服务提供者的api

public class ScoreServiceImpl {
    @Autowired
    //调用其他服务时,直接通过@Autowired注解注入OpenFeign接口对象就可以像调用本地方法一样调用远程服务。
    private selectCommentOpenFeign feignApi;
   // 测试Feign
    public int test(String videoId) {
        Boolean Result = feignApi.selectComment(videoId);
        return Result;
    }
}

OK,完成!

猜你喜欢

转载自blog.csdn.net/hejingfang123/article/details/119742203