当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Cloud第七篇 | 声明式服务调用Feign

Spring Cloud第七篇 | 声明式服务调用Feign

2019年12月15日  | 移动技术网IT编程  | 我要评论

本文是spring cloud专栏的第七篇文章,了解前六篇文章内容有助于更好的理解本文:

一、feign是什么

    feign是netflix公司开发的一个声明式的rest调用客户端; ribbon负载均衡、 hystrⅸ服务熔断是我们spring cloud中进行微服务开发非常基础的组件,在使用的过程中我们也发现它们一般都是同时出现的,而且配置也都非常相似,每次开发都有很多相同的代码,因此spring cloud基于netflix feign整合了ribbon和hystrix两个组件,让我们的开发工作变得更加简单, 就像spring boot是对spring+ springmvc的简化, spring cloud feign对ribbon负载均衡、 hystrⅸ服务熔断进行简化,在其基础上进行了进一步的封装,不仅在配置上大大简化了开发工作,同时还提供了一种声明式的web服务客户端定义方式。使用方式类似dubbo的使用方式。

二、使用feign实现消费者

1、创建消费者服务命名为(springcloud-service-feign)

2、添加依赖

    <dependency>
      <groupid>org.springframework.cloud</groupid>
      <artifactid>spring-cloud-starter-netflix-eureka-client</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework.cloud</groupid>
      <artifactid>spring-cloud-starter-openfeign</artifactid>
    </dependency>

3、在启动类上添加注解

@enablefeignclients

4、声明服务

    定义一个helloservice接口,通过@feignclient注解来指定服务名称进而绑定服务,然后在通过spring  mvc中提供的注解来绑定服务提供者的接口,如下:

//使用feign的客户端注解绑定远程的名称,名称可以是大写,也可以小写
@feignclient(value = "springcloud-service-provider")
public interface helloservice {
    //声明一个方法,这个方法就是远程的服务提供者提供的方法
    @requestmapping("/provider/hello")
    public string hello();   
}

5、使用controller中调用服务,代码如下

    @autowired
    private helloservice helloservice;

    @requestmapping("/hello")
    public string hello(){
        //调用声明式接口方法,实现对远程服务的调用
        return helloservice.hello();
    }

6、application.yml配置如下

spring:
  application:
    name: springcloud-service-feign
server:
  port: 9091
eureka:
  client:
    service-url:
      defaultzone: http://localhost:8700/eureka
    #客户端每隔30秒从eureka服务上更新一次服务信息
    registry-fetch-interval-seconds: 30
    #需要将我的服务注册到eureka上
    register-with-eureka: true
    #需要检索服务
    fetch-registry: true
  #心跳检测检测与续约时间
  instance:
    #告诉服务端,如果我10s之内没有给你发心跳,就代表我故障了,将我剔除掉,默认90s
    #eureka服务端在收到最后一次心跳之后等待的时间上限,单位为秒,超过则剔除(客户端告诉服务端按照此规则等待自己)
    lease-expiration-duration-in-seconds: 10
    #每隔2s向服务端发送一次心跳,证明自已依然活着,默认30s
    #eureka客户端向服务端发送心跳的时间间隔,单位为秒(客户端告诉服务端自己会按照该规则)
    lease-renewal-interval-in-seconds: 2

7、启动测试,访问地址http://localhost:9091/feign/hello

三、使用feign支持的特性

负载均衡:

    spring cloud提供了ribbon来实现负载均衡,使用ribbo直接注入一个resttemplate对象即可, resttemplate已经做好了负载均衡的配置在spring cloud下,使用 feign也是直接可以实现负载均衡的,定义一个有@feignclient注解的接口,然后使用@requestmappin注解到方法上映射远程的rest服务,此方法也是做好负责均衡配置的。

服务熔断:

1、在 application.yml文件开启hystrix功能

#开启hystrix熔断机制
feign:
  hystrix:
    enabled: true

2、指定熔断回调逻辑

@feignclient(value = "springcloud-service-provider", fallback = myfallback.class)
@component
public class myfallback implements helloservice {
    @override
    public string hello() {
        return "远程服务不可用,暂时采用本地逻辑代替。。。。。";
    }
}

3、测试,让服务提供者超时就行了

如果需要捕获提供者抛出的异常可以用:

@feignclient(value = "springcloud-service-provider", fallbackfactory = myfallbackfactory.class)
@component
public class myfallbackfactory implements fallbackfactory<helloservice> {
    @override
    public helloservice create(throwable throwable) {

        return new helloservice() {
            @override
            public string hello() {
                return throwable.getmessage();
            }
        };
    }
}

 

详细参考案例源码:

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网