Spring Cloud(五):服务消费者Feign

来源:互联网 发布:淘宝怎么看微淘 编辑:程序博客网 时间:2024/06/05 11:51

一:概述
我们上节说过,Feign完美的整合了Ribbon和Hystrix,即Feign可以无缝对接的进行客户端负载均衡以及熔断器作用。
Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单。Feign还提供了HTTP请求的模板,通过编写简单的接口和插入注解,我们就可以定义好HTTP请求的参数、格式、地址等信息。接下来,Feign会完全代理HTTP的请求,我们只需要像调用方法一样调用它就可以完成服务请求。

二:原理
1.通过@EnableFeignCleints注解开启FeignCleint
2.根据Feign的规则实现接口,并加@FeignCleint注解,来绑定该接口对应服务
3.程序启动后,会进行包扫描,扫描所有的@ FeignCleint的注解的类,并将这些信息注入IOC容器中。
4.当接口的方法被调用,通过jdk的代理,来生成具体的RequesTemplate,RequesTemplate再生成Request
5.Request交给Client去处理,其中Client可以是HttpUrlConnection、HttpClient
6.最后Client被封装到LoadBalanceClient类,这个类结合类Ribbon做到了负载均衡。

三:项目的构建
结合之前的Eureka Server以及Eureka Provider项目。Feign项目如下,
3.1:pom.xml配置:

<parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.5.6.RELEASE</version>    <relativePath/> <!-- lookup parent from repository --></parent><properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>    <java.version>1.8</java.version></properties><dependencies>    <!-- Feign实现声明式HTTP客户端 -->    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-feign</artifactId>    </dependency>    <!-- eureka客户端 -->    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-eureka</artifactId>    </dependency>    <!-- spring boot实现Java Web服务-->    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <!--健康指标-->    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-actuator</artifactId>    </dependency>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-test</artifactId>        <scope>test</scope>    </dependency></dependencies><dependencyManagement>    <dependencies>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-dependencies</artifactId>            <version>Camden.SR6</version>            <type>pom</type>            <scope>import</scope>        </dependency>    </dependencies></dependencyManagement><build>    <plugins>        <plugin>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-maven-plugin</artifactId>        </plugin>    </plugins></build>

3.2:application.properties

#应用名称spring.application.name=feign-consumer#端口号server.port=8766#注册中心地址eureka.client.serviceUrl.defaultZone=http://eureka:123456@localhost:8761/eureka/#忽略安全认证management.security.enabled=false

3.3:入口程序

@EnableDiscoveryClient //启动服务发现@EnableFeignClients //开启Feign功能@SpringBootApplicationpublic class FeignConsumerApplication {public static void main(String[] args) {        SpringApplication.run(FeignConsumerApplication.class, args);    }}

3.4:新建一个API的依赖

@FeignClient(name = "eureka-provider", fallback ConsumerFallback.class)public interface RemoteClient {@RequestMapping(value = "/index")    String index(); }

说明:上述我们定义了一个远程调用API的接口,注解@FeignClient,Ribbon自动被应用,定义服务提供者的name,以及熔断器类:ConsumerFallback.class

3.5:新建熔断器ConsumerFallback

@Componentpublic class ConsumerFallback implements RemoteClient {@Overridepublic String index() {    return "Feign客户端访问失败!";    }}

注意:注入@Component

3.6:启动Feign项目
查看Eureka Server服务注册是否成功
这里写图片描述
访问消费者Feign项目API
这里写图片描述

3.7:终止服务提供者Eureka Provider的项目
再次访问消费者项目
这里写图片描述
我们可以看到,熔断起了作用。

3.8:客户端负载均衡
参考Ribbon那节内容

3.9:Hystrix的仪表盘
pom.xml新增依赖

  <!--hystrix-->    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-hystrix</artifactId>    </dependency>  <!--hystrix-dashboard 监控-->    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>    </dependency>

入口程序新增注解@EnableHystrixDashboard

@EnableDiscoveryClient //启动服务发现@EnableFeignClients //开启Feign功能@SpringBootApplication@EnableCircuitBreaker@EnableHystrixDashboard //开启Hystrix仪表盘public class FeignConsumerApplication {public static void main(String[] args) {    SpringApplication.run(FeignConsumerApplication.class, args);    }}

访问仪表盘界面
这里写图片描述
点击Monitor Stream进入仪表盘监控界面,可以查看相关熔断信息

代码地址:https://github.com/rubenYuan/Spring-Cloud-Samples
PPT:http://download.csdn.net/download/ruben95001/9974839

完!

原创粉丝点击