Spring Cloud有两种服务调用方式,一种是Ribbon+RestTemplate,另一种是Feign,上一篇文章,讲述了如何通过Ribbon+RestTemplate去调用服务,这篇文章主要讲述如何通过Feign去调用服务
Feign是一个声明式的Web服务客户端。这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其进行注释。它具有可插入注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并使用Spring Web中默认使用的HttpMessageConverters。Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。
Feign能使我们远程调用服务客户端变得更加容易,不再使用Ribbon+RestTemplate模板式的调用,在一套系统中, 不止一个接口需要调用,往往遇到这样的情况总合有方法解决那就是Feign,Fegin进行了封装,由他来帮助我们定义和实现依赖服务接口的定义.在Feign的实现下,我们只需要创建一个接口并使用注解的方式来配置它(以前是Dao接口上标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可),即可完成对服务提供方的接口绑定,简化了使用Spring cloud Ribbon时,自动封装服务调用客户端的开发量
SpringCloud详细教程 | 第二篇: 客户端负载平衡器Ribbon(Greenwich版本)
192.168.1.105:eureka-client:8764
192.168.1.105:eureka-client:8762
2.创建Feign服务
开始创建feign-server服务 创建时勾选feign依赖
<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>
改造后的pom.xml文件 继承父pom文件 和 父pom添加feign-server模块
<?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.li</groupId> <artifactId>SpringCloudLearn</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../</relativePath> </parent> <artifactId>feign-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>feign-server</name> <description>Demo project for Spring Boot</description>
<dependencies> <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> </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>
配置application.properties文件
# 端口号server.port=8766# 需要指明spring.application.name 这个很重要# 这在以后的服务与服务之间相互调用一般都是根据这个namespring.application.name=feign-server#服务注册中心实例的主机名eureka.instance.hostname=localhost#服务注册中心端口号eureka.port=8761#在此指定服务注册中心地址eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${eureka.port}/eureka/
在程序的启动类加上@EnableEurekaClient向注册中心注册和@EnableFeignClients注解开启Feign的功能
package com.li.feignserver;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients // 开启Feign功能@EnableEurekaClient@SpringBootApplicationpublic class FeignServerApplication {
public static void main(String[] args) { SpringApplication.run(FeignServerApplication.class, args); }
}
创建一个服务接口请求服务提供者eureka-client
package com.li.feignserver.server;
import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;
/** * eureka-client 是调用的服务名 */@FeignClient(value = "eureka-client")public interface EurekaCclientService {
@GetMapping(value = "/hello") String hello(@RequestParam(value = "name") String name);}
Feign配置重试机制和超时等
Request.Options 第一个参数建立socket超时时间 第二个读取响应socket超时时间
Retryer 第一个参数period是请求重试的间隔算法参数,第二个参数maxPeriod 是请求间隔最大时间,第三个参数是重试的次数
参考:https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html
package com.li.feignserver.server;
import feign.Request;import feign.Retryer;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
/** * @Classname FeignConfig * @Description TODO * @Author 李号东 lihaodongmail@163.com * @Date 2019-03-26 23:14 * @Version 1.0 */@Configurationpublic class FeignConfig {
/** * 建立socket超时时间 读取响应socket超时时间 * @return */ @Bean public Request.Options options() { return new Request.Options(12000, 12000); }
/** * 自定义重试次数 * @return */ @Bean public Retryer feignRetried(){ return new Retryer.Default(100, TimeUnit.SECONDS.toMillis(1L),5); }}
在启动类添加请求
package com.li.feignserver;
import com.li.feignserver.server.EurekaCclientService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.openfeign.EnableFeignClients;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;
@RestController@EnableFeignClients // 开启Feign功能@EnableEurekaClient@SpringBootApplicationpublic class FeignServerApplication {
public static void main(String[] args) { SpringApplication.run(FeignServerApplication.class, args); }
@Autowired private EurekaCclientService eurekaCclientService;
@GetMapping("/hello") public String hello(@RequestParam(value = "name") String name) { return eurekaCclientService.hello(name); }
}
启动项目,打开浏览器访问: http://localhost:8766/hello?name=lhd
第一次访问
第二次访问
是不是比Ribbon简单很多 赶快自己实现吧
个人是比较建议使用Feign 因为本身就包含Ribbon,数据的丢失率很低
源码下载: https://github.com/LiHaodong888/SpringCloudLearn