介绍:
我们用ribbon时通常是用restTemplate来调用接口,restTemplate是封装了http的请求处理,形成了模板化的请求接口模式,Feign是进一步的封装,进而更简单的调用接口。Feign是一个声明式的REST客户端,Feign提供了http的请求模板,原生的Feign是不支持SpringMvc的注解的,但是springCloud对Feign又进一步封装,让其支持了MVC的注解。
快速入门:
新建一个名字为FeignConsumer的springboot项目。
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>
<groupId>com.feign</groupId>
<artifactId>feign-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>feign-consumer</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<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>
</dependency>
<!-- 支持feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类:
package com.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //支持feign的注解
public class FeignConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(FeignConsumerApplication.class, args);
}
}
helloservice:这是写一个接口用于去请求其他服务接口的
package com.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.*;
/**
* Created by qhe on 2018/7/30.
*/
@FeignClient(value = "hello-service",configuration = FeignConfiguration.class) //value是服务名称,configuration日志配置,后面会说
public interface HelloService {
@RequestMapping("/hello")
String hello();
//简单的接口调用
@RequestMapping(value = "/hello_name",method = RequestMethod.GET)
String index(@RequestParam("name") String name);
//请求头里带参数
@RequestMapping(value = "/hello_age",method = RequestMethod.GET)
String index(@RequestHeader("name") String name, @RequestHeader("age") Integer age);
//带对象的post请求
@RequestMapping(value = "/hello_user",method = RequestMethod.POST)
String index(@RequestBody User user);
}
HelloController:
package com.feign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* Created by qhe on 2018/7/30.
*/
@RestController
public class ConsumerController {
//这里用注解注入上面的helloservice的feign接口,
//就像注入自己的service一样,调用feign接口的方法,feign会去请求其他服务的对应接口
@Autowired
private HelloService helloService;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String helloConsumer(){
return helloService.hello();
}
@RequestMapping(value = "/hello_name",method = RequestMethod.GET)
public String index(){
return helloService.index("feignName");
}
@RequestMapping(value = "/hello_age",method = RequestMethod.GET)
public String index2(){
return helloService.index("Name",11);
}
@RequestMapping(value = "/hello_user",method = RequestMethod.GET)
public String index3(){
User user = new User("haha",12);
return helloService.index(user);
}
}
启动服务,请求controller里的接口,这里请求/hello接口,就会去调用之前的hello-service服务里的hello接口,返回hello.
日志配置:
调用feign接口,总会有出错的时候,这个时候我们需要查看接口的一些信息,所以需要为feign配置日志功能。
写一个配置类,用于启动feign的日志:
package com.feign;
import feign.Logger; //feign的日志
import feign.Request;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by qhe on 2018/8/21.
*/
@Configuration
public class FeignConfiguration {
/**
* 日志配置
* 这里的Logger的包要引用Feign的Logger
* @return
*/
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
然后再helloservice接口里配置一下,@FeignClient(value = "hello-service",configuration = FeignConfiguration.class) ,
最后在配置文件里配置一下指定级别(这里要指定哪个接口和级别debug还是info):
不知道可不可以不用指定具体哪个接口,直接全局???知道的麻烦说一下哈。
logging.level.com.feign.HelloService=debug
还可以配置其他的一些配置:
package com.feign;
import feign.Logger;
import feign.Request;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by qhe on 2018/8/21.
*/
@Configuration
public class FeignConfiguration {
/**
* 日志配置
* @return
*/
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
/**
* 超时配置
* @return
*/
@Bean
public Request.Options options(){
return new Request.Options(5000,10000);
}
/**
* basic认证配置,用于拦截权限认证
* @return
*/
// @Bean
// public BasicAuthorizationInterceptor basicAuthorizationInterceptor(){
// return new BasicAuthorizationInterceptor("user","password");
// }
}
Ribbon 配置:
Feign客户端的负载均衡也是通过Ribbon来实现的,如果需要修改一些配置参数,只要在配置文件里配置一下就行,
全局配置,比如
ribbon.ConnectTimeout=500 //连接超时
ribbon.ReadTimeout=5000//请求超时
指定服务配置(前面只要加上服务名就行)
Hello-service.ribbon.ConnectTimeout=500
Hello-service.ribbon.ReadTimeout=5000
Hello-service.ribbon.OkToRetryOnAllOperations=true
Hello-service.ribbon.MaxAutoRetriesNextServer=2 //重试次数
Hello-service.ribbon.MaxAutoReries=1
重试机制,Feign默认实现重试机制,当失败后会再次请求,也是只要在配置修改我们自定义的,上面就是重试的一些配置。
Hystrix(容错)配置
feign.hystrix.enabled=true//开启Hystrix功能,false就是关闭
hystrix.command.default.execution.isolation.thread.timeoutInMillisecinds=5000//全局超时时间
关闭某个服务的容错保护只要在配置类里加一个
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder(){
return Feign.builder();
}
然后再Feign接口,helloservice里加上这个配置类就行
学习资料《SpringCloud微服务实战》《SpringCloud微服务全栈技术与案例解析》