openfeign配置代理服务器[通俗易懂]

openfeign配置代理服务器[通俗易懂]第一步:配置文件允许覆盖Beanspring:main:allow-bean-definition-overriding:true第二步:配置Beanpackagecom.ciih.refine.config;importokhttp3.*;importorg.springframework.cloud.commons.httpclient.DefaultOkHttpClientFactory;importorg.springframework.cloud.

大家好,欢迎来到IT知识分享网。

目录

第一步:配置文件允许覆盖Bean

第二步:配置Bean


 

第一步:配置文件允许覆盖Bean

spring:
  main:
    allow-bean-definition-overriding: true

第二步:配置Bean

package com.ciih.refine.config;

import okhttp3.*;
import org.springframework.cloud.commons.httpclient.DefaultOkHttpClientFactory;
import org.springframework.cloud.commons.httpclient.OkHttpClientFactory;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;

@Configuration
//配置openfein接口的文件件路径
@EnableFeignClients(basePackages = "com.ciih.refine.server")
public class Config {

    @Bean
    public OkHttpClientFactory okHttpClientFactory(OkHttpClient.Builder builder) {
        return new ProxyOkHttpClientFactory(builder);
    }

    static class ProxyOkHttpClientFactory extends DefaultOkHttpClientFactory {

        public ProxyOkHttpClientFactory(OkHttpClient.Builder builder) {
            super(builder);
            //配置IP、端口
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("190.168.1.1", 8080));
            builder.proxy(proxy);

            builder.proxyAuthenticator(new Authenticator() {
                @Override
                public Request authenticate(Route route, Response response) throws IOException {
                    //设置代理服务器账号密码
                    String credential = Credentials.basic("admin", "admin");
                    return response.request().newBuilder()
                            .header("Proxy-Authorization", credential)
                            .build();
                }
            });
            //如果要配置限制域则加上下面
            /*List<Proxy> proxyList = new ArrayList<>(1);
            proxyList.add(proxy);
            builder.proxySelector(new ProxySelector() {
                //限制域
                Set<String> domainList;
                @Override
                public List<Proxy> select(URI uri) {
                    if (uri == null || !domainList.contains(uri.getHost())) {
                        return Collections.singletonList(Proxy.NO_PROXY);
                    }
                    return proxyList;
                }

                @Override
                public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
                }
            });*/
        }
    }
}

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/24570.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信