一、Feign简介
Feign是一种声明式、模板化的HTTP客户端。这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其进行注释。它具有可插入注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并使用Spring Web中默认使用的HttpMessageConverters。Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。这段话来源于官方文档,说白了就是通过Feign来调用Rest接口,而无需使用其他HTTP访问组件,并且同时还提供了负载均衡、编解码等功能,使用起来很方便。
二、环境介绍
首先在A服务器上启动Eureka服务,然后在B、C两台服务器上分别启动ms-demo-provider服务,这里也可以部署在一台服务器上采用不同的端口。访问Eureka界面查看服务注册状态,之后在本地新建客户端调用工程进行测试。此时A为注册中心,B、C分别为服务提供者(提供相同的接口),本地工程为服务消费者。
三、项目代码
在Idea中创建maven工程,ms-eurekaclient-demo工程代码结构如下:
1:pom.xml中的依赖如下:
<?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> <groupId>com.cloud.microservice</groupId> <artifactId>ms-eurekaclient-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>ms-eurekaclient-demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.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> <spring-cloud.version>Edgware.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <!-- 加入断路器依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <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> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</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> </project>
2:application.properties中的配置信息如下:
spring.application.name=ms-eurekaclient-demo server.port=9800 # 注册中心地址 eureka.client.serviceUrl.defaultZone=http://xx.xx.xx.xx:9000/eureka/ # Indicates whether this client should fetch eureka registry information from eureka server # 客户端是否要从eureka server获取注册信息,默认为true eureka.client.fetchRegistry=true # Indicates how often(in seconds) to fetch the registry information from the eureka server # 从eureka server获取注册信息的频率,默认为30秒,缩短配置时间可以缓解服务上线时间过长的问题 eureka.client.registryFetchIntervalSeconds=10
3:在入口类Application中增加@EnableEurekaClient和@EnableFeignClients的注解
- @EnableEurekaClient:注解用来标识开启服务发现功能,据说也可使用@EnableDiscoveryClient,网上有人说@EnableEurekaClient本身就是用@EnableDiscoveryClient来实现的,这点没仔细研究过
- @EnableFeignClients:注解用来开启Feign功能
package com.cloud.microservice.eurekaclientdemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableFeignClients public class FeignDemoApplication { public static void main(String[] args) { SpringApplication.run(FeignDemoApplication.class, args); } }
4:创建IUserFeignServiceClient接口类,代码如下:
package com.cloud.microservice.eurekaclientdemo.FeignClient; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @FeignClient("ms-demo-provider") public interface IUserFeignServiceClient { //Feign定义服务提供者接口 @RequestMapping(value = "/demo/user/1.0/findAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"}) String findAll(); }
5:创建IUserService接口类和UserServiceImp实现类,代码如下:
IUserService接口类如下:
package com.cloud.microservice.eurekaclientdemo.FeignClient; public interface IUserService { String findAll(); }
UserServiceImp实现类如下:
package com.cloud.microservice.eurekaclientdemo.FeignClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImp implements IUserService{ @Autowired private IUserFeignServiceClient userFeignServiceClient; public String findAll() { return "Feign: " + userFeignServiceClient.findAll(); } }
6:Rest接口定义,代码如下:
package com.cloud.microservice.eurekaclientdemo.FeignClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class FeignDemoController { @Autowired private IUserService userService; @RequestMapping(value = "/feigndemo/findAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"}) public String feignDemo() { return userService.findAll(); } }
启动工程,然后刷新Eureka界面,可以看到Feign已经注册到服务中心
四、运行测试
打开浏览器,访问ms-eurekaclient-demo中的接口,地址:http://localhost:9800/feigndemo/findAll,返回结果如下:
以上返回结果说明接口调用成功。同时我们也可以登陆到服务提供者的服务器上查看log。
通过多次访问http://localhost:9800/feigndemo/findAll这个接口,可以看到B、C两台服务器上的provider中都有接口被调用的记录。
原文地址:https://www.cnblogs.com/chxuyuan/p/8358998.html