SpringCloud--04声明式调用Feign

 1.Feign

   Feign采用了声明式API接口的风格,将java Http客户端绑定到他的内部,Fegin的首要目标是将java Http客户端的调用过程变得简单,

工程项目基于上一节的内容新建一个Model模块,继承了父依赖,eureka的客户端 起步依赖,Feign的起步依赖,Web的起步依赖,和测试的起步依赖。

pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.wx</groupId>
        <artifactId>learnspringcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
        <!--  <relativePath/>--> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wx</groupId>
    <artifactId>eureka-feign-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-feign-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-feign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 yml文件配置如下:

server:
  port: 8766
spring:
  application:
    name: eureka-feign-client
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/

 启动类上添加注解@EnableEurekaClient   @EnableFeignClients开启Eureka和Feign的功能。

接下来就是如何使用Fegin了,新建一个接口EurekaClientFeign,加上@FeignClient(value = "service-hi",configuration = FeginConfig.class)的注解,表示他会去调用名为service-hi的服务,然后配置类是FeginConfig。接口里面写使用注解GetMapping("/hi"),表示请求哪个service-hi的方法:

@FeignClient(value = "service-hi",configuration = FeginConfig.class)
public interface EurekaClientFeign {
    @GetMapping(value = "/hi")
    public String  sayHiClientEureka(@RequestParam(value = "name") String name);
}

Feignd的配置类,在该配置类中注入一个重试的Bean,表示Feign在远程调用在失败后,会进行重试,三个参数为周期,最大周期,和最大尝试次数。重试的时间间隔为100毫秒,最大重试时间为1秒,重试次数为5次。

@Configuration
public class FeginConfig {
    @Bean
    public Retryer feignRetryer(){
        return new Retryer.Default(100,TimeUnit.SECONDS.toMillis(1),5);
    }
}

然后就是如何将Feign用起来的问题,在service层中使用Feign客户端的接口:

@Service
public class HiServcie {
    @Autowired
    private EurekaClientFeign  eurekaClientFeign;

    public String sayHi(String name){
        return eurekaClientFeign.sayHiClientEureka(name);
    }
}

在controller中提供接口供浏览器访问:

@RestController
public class HiController {
    @Autowired
    private HiServcie hiServcie;

    @GetMapping("/hi")
    public String sayHi(@RequestParam(value = "name") String name){
        return hiServcie.sayHi(name);
    }
}

然后需要启动一个eureka server,启动两个eureka client服务提供者,因为feign默认集成了Ribbon的功能,feign还默认集成了Hystrix,所以测一下,其次就是启动集成了Feign的服务消费者:

访问:http://localhost:8766/hi?name=Mr.Wang

        

Feign实现负载均衡的原理和Ribbon是一样的,在客户端实现的。

2.@FeginClient注解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FeignClient {
    @AliasFor("name")
    String value() default "";

    /** @deprecated */
    @Deprecated
    String serviceId() default "";

    @AliasFor("value")
    String name() default "";

    String qualifier() default "";

    String url() default "";

    boolean decode404() default false;

    Class<?>[] configuration() default {};

    Class<?> fallback() default void.class;

    Class<?> fallbackFactory() default void.class;

    String path() default "";

    boolean primary() default true;
}

  首先看这个注解,这个注解作用范围实在接口上面,其次注解的生命周期是运行时,表示可以通过反射获得注解并解析注解,

name()和value()所表达的意思是一样的,都是serviceId,url是硬编码时候的URL地址,decode404即404是被解码实施抛异常,

configuration指明FeignClient的配置类,fallback为配置熔断器的处理类。

Fegin Clent有他自己的默认配置类FeginClientsConfiguration,比如他有配置是失败了重试次数为0等等。

3.Fegin的源码实现过程

    1.首先通过@EnableFeignClients注解开启FeginClient的功能,只有这个注解存在,才会在程序启动的时候开启@FeignClient

注解的包扫描。

   2.根据Fegin的规则实现接口。

   3.启动程序后会进行包扫描,扫描所有@FeignClient接口的类,并且将这些信息注入到IOC容器中。

   4.当接口的方法被调用的时候,通过JDK代理来生成具体的RequestTemplate模板对象。

  5.根据RequestTemplate再生成Http请求的Request对象。

  6.Request交给Client组件去处理,网络请求框架可以是HttpURLConnection(默认),HttpClient,OkHttp。

   7. 最后Clent被封装到了LoadBalanceClient类,这个类结合Ribbon做到了负载均衡。

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时空恋旅人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值