Feign和openFeign

 

OpenFeign is the spiritual successor to Feign, originally provided by Netflix. The projects name was changed when the original project was released to the community. The correct dependency is spring-cloud-starter-openfeign. share | improve this answer

----OpenFeign是feign的继承者,原始feign是油NetFlix提供。当原始项目发布到社区时,项目名称已更改

Feign

  • Feign是Spring Cloud组件中的一个轻量级RESTful的HTTP服务客户端
  • Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。
  • Feign的使用方式是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务
  • Feign支持的注解和用法请参考官方文档:https://github.com/OpenFeign/feign
  • ​​​​​​​Feign本身不支持Spring MVC的注解,它有一套自己的注解

 

OpenFeign

  • ​​​​​​​OpenFeign是Spring Cloud 在Feign的基础上支持了Spring MVC的注解,如@RequesMapping等等。OpenFeign
  • @FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调其他服务

 

Feign消费非spring mvc服务实现

 

  1. 如果feign客户端和feign服务端都是springmvc的项目,我们使用openFeign的@FeignClient+springmvc @ReqeustMapping注解既可。
  2. 当面对非springmvc 的项目时候,可以使用原生feign+@RequestLine
  3. 以下示例包括get请求和post+application/json的请求

 

1.ClientConfiguration .java
@Configuration
public class ClientConfiguration {

    public FeignApi getFeignClient() {
        String url = buildonfiguration.getTektonUrl();
        TektonFeignApi feignClient = Feign.builder()
                .options(new Request.Options(1000,2*1000))
                .encoder(new GsonEncoder())
                .decoder(new GsonDecoder())
                .target(FeignApi .class, "lcoalhost://8888");
        return feignClient;
    }

    /*创建忽略认证client,暂时保留*/
    public Client feignClient() {
        return new Client.Default(getSSLSocketFactory(), new NoopHostnameVerifier());
    }

    private SSLSocketFactory getSSLSocketFactory() {
        try {
            SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
            return sslContext.getSocketFactory();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}


2.FeignAPi.class
public interface FeignApi {

  @Headers({"Content-Type: application/json"})
  @RequestLine("POST /createInfo")
  JSONObject postTest(Object body);//参数不要为String类型,否则json数据会多加双引号

  @Headers({"Content-Type: application/json"})
  @RequestLine("GET /getInfoByUser" )
  JSONObject getTest(@Param userName);
}


3.TestService .java

@Service
public class TestService {
    @Autowired
    FeignApi client;

    public String getInfo(String userName) {
        JSONObject result= tektonClient.getTest(userName);
        return JSONObject.toJSONString(result) ;
    }

    public String creatInfo() {
        JSONObject info= JSONObject.parseObject("{\"name\":\"cluster1\",\"size\":\"908\"}");
       JSONObject result= client.postTest(info);
        return JSONObject.toJSONString(result) ;
    }
}

 

Feign客户端忽略认证的方式

 

参考:https://stackoverflow.com/questions/43447320/how-to-ignore-ssl-cert-trust-errors-in-feign/53258234#53258234

 

方式1:feign.httpClient.disableSslValidation=true(没试)

 

方式二:重写feignConfiguration

public Client feignClient()
{
    Client trustSSLSockets = new Client.Default(getSSLSocketFactory(), new NoopHostnameVerifier());
    return trustSSLSockets;
}


private SSLSocketFactory getSSLSocketFactory() {
    try {
        TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
            @Override
            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                return true;
            }
        };

        SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
        return sslContext.getSocketFactory();
    } catch (Exception exception) {
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值