Feign使得 Java HTTP 客户端编写更方便。Feign 灵感来源于Retrofit、JAXRS-2.0和WebSocket。Feign最初是为了降低统一绑定Denominator到HTTP API的复杂度,不区分是否支持Restful。Feign旨在通过最少的资源和代码来实现和HTTP API的连接。通过可定制的解码器和错误处理,可以编写任意的HTTP API。
<dependency>
<groupId>com.netflix.feigngroupId>
<artifactId>feign-coreartifactId>
<version>8.18.0version>
dependency>
<dependency>
<groupId>com.netflix.feigngroupId>
<artifactId>feign-jacksonartifactId>
<version>8.18.0version>
dependency>
<dependency>
<groupId>io.github.lukehutchgroupId>
<artifactId>fast-classpath-scannerartifactId>
<version>2.18.1version>
dependency>
<dependency>
<groupId>com.netflix.feigngroupId>
<artifactId>feign-jacksonartifactId>
<version>8.18.0version>
dependency>
复制代码
RemoteService service = Feign.builder()
.options(new Options(1000, 3500))
.retryer(new Retryer.Default(5000, 5000, 3))
.encoder(new JacksonEncoder())
.decoder(new JacksonDecoder())
.target(RemoteService.class, "http://127.0.0.1:8085");
复制代码
随机定义一个远程调用的服务接口,并且声明相关的接口参数和请求地址。
通过@RequestLine指定HTTP协议及URL地址
public class User{
String userName;
}
public interface RemoteService {
User getOwner(User user);
public User getOwner( User user,
String requestToken,
Long userId,
String userName);
}
复制代码
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
public class UserController {
public User list( User user) throws InterruptedException{
System.out.println(user.getUsername());
user.setId(100L);
user.setUsername(user.getUsername().toUpperCase());
return user;
}
}
复制代码
与调用本地方法相同的方式调用feign包装的接口,直接获取远程服务提供的返回值。
String result = service.getOwner(new User("scott"));
复制代码
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public FeignApi {
String serviceUrl();
}
复制代码
import feign.Feign;
import feign.Request;
import feign.Retryer;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner;
import io.github.lukehutch.fastclasspathscanner.scanner.ScanResult;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;
import java.util.List;
public class FeignClientRegister implements BeanFactoryPostProcessor{
private String scanPath="com.xxx.api";
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
List classes = scan(scanPath);
if(classes==null){
return ;
}
System.out.println(classes);
Feign.Builder builder = getFeignBuilder();
if(classes.size()>0){
for (String claz : classes) {
Class targetClass = null;
try {
targetClass = Class.forName(claz);
String url=targetClass.getAnnotation(FeignApi.class).serviceUrl();
if(url.indexOf("http://")!=0){
url="http://"+url;
}
Object target = builder.target(targetClass, url);
beanFactory.registerSingleton(targetClass.getName(), target);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
}
}
public Feign.Builder getFeignBuilder(){
Feign.Builder builder = Feign.builder()
.encoder(new JacksonEncoder())
.decoder(new JacksonDecoder())
.options(new Request.Options(1000, 3500))
.retryer(new Retryer.Default(5000, 5000, 3));
return builder;
}
public List scan(String path){
ScanResult result = new FastClasspathScanner(path).matchClassesWithAnnotation(FeignApi.class, (Class aClass) -> {
}).scan();
if(result!=null){
return result.getNamesOfAllInterfaceClasses();
}
return null;
}
}
复制代码
import com.xiaokong.core.base.Result;
import com.xiaokong.domain.DO.DeptRoom;
import feign.Headers;
import feign.Param;
import feign.RequestLine;
import com.xiaokong.register.FeignApi;
import java.util.List;
public interface RoomApi {
Result selectById( String id);
Result> selectList();
}
复制代码
public class ServiceImpl{
private RoomApi roomApi;
public void demo(){
Result result = roomApi.selectById("1");
System.out.println(result);
}
}
复制代码