Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 假注册-春季云-更改目标,无彩带覆盖 介绍_Java_Spring Cloud_Spring Cloud Feign_Feign_Openfeign - Fatal编程技术网

Java 假注册-春季云-更改目标,无彩带覆盖 介绍

Java 假注册-春季云-更改目标,无彩带覆盖 介绍,java,spring-cloud,spring-cloud-feign,feign,openfeign,Java,Spring Cloud,Spring Cloud Feign,Feign,Openfeign,我想能够有两个不同的春天配置文件,并根据配置文件更改为我们的外国建设者硬编码地址 目前正在进行的工作包括: ab-document-store.ribbon.NIWSServerListClassName:com.netflix.loadbalancer.ConfigurationBasedServerList ab-document-store.ribbon.listOfServers: localhost:8025 returnbuilder.target(cls,“http://”+se

我想能够有两个不同的春天配置文件,并根据配置文件更改为我们的外国建设者硬编码地址

目前正在进行的工作包括:

ab-document-store.ribbon.NIWSServerListClassName:com.netflix.loadbalancer.ConfigurationBasedServerList
ab-document-store.ribbon.listOfServers: localhost:8025
returnbuilder.target(cls,“http://”+serviceName)

但实际上,我想做以下工作,并对地址进行概述:

returnbuilder.target(cls)http://our-server:8009/“+服务名称)

为什么? 有时我们不想在开发环境中运行所有服务。此外,有些服务有时只能通过zuul网关提供

因此,我们在不同的情况和条件下运行相同的代码

技术细节 我们有以下代码用于构建我们的外国客户

过去我们一直在使用
@FeignClient
注释,但最近我们决定开始手动构建我们的FeignClient

示例如下:

@FeignClient(name = "ab-document-store",  configuration = MultiPartSupportConfiguration.class, fallback = DocumentStoreFallback.class)
我们使用以下命令调用假注册器类:

return registerFeignClient(DocumentStoreClient.class, true);



@RequiredArgsConstructor
//@Component
@Slf4j
public class FeignRegistrar {

    @Autowired
    private Decoder decoder;

    @Autowired
    private Encoder encoder;

    @Autowired
    private Client client;

    @Autowired
    private Contract feignContract;

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Autowired
    private List<RequestInterceptor> interceptors;

    public <T> T register(Class<T> cls, String serviceName, boolean isDocumentStore) {

        if(isDocumentStore){
            encoder = new MultipartFormEncoder(new SpringEncoder(messageConverters));
        }

        //Client trustSSLSockets = new Client.Default(getSSLSocketFactory(), new NoopHostnameVerifier());

        Feign.Builder builder = Feign.builder()
            .client(client)
            .encoder(encoder)
            .decoder(decoder)
            .contract(feignContract)
            .logger(new Slf4Logger())
            .logLevel(Logger.Level.HEADERS);

        builder.requestInterceptor(new RequestInterceptor() {

            @Override
            public void apply(RequestTemplate template) {
                template.header("X-Service-Name", serviceName);
            }
        });

        for(RequestInterceptor interceptor : interceptors) { 

            builder.requestInterceptor(interceptor);
        }

        log.debug("Registering {} - as feign proxy ", serviceName);

        return builder.target(cls, "http://" + serviceName);
    }

    public static class Slf4Logger extends Logger {

        @Override
        protected void log(String configKey, String format, Object... args) {
            log.info("{} - {}", configKey, args);
        }
    }
}
不幸的是,
服务器列表对我们来说还不够。我们也希望能够分配一个目录/路径。比如:

ab-document-store.ribbon.listOfServers:localhost:8025/ab-document-store

其他解决方法 我曾考虑过使用一个假拦截器,在所有请求(如
X-SERVICE-NAME
)中偷偷插入一个头。然后,我们可以将所有服务指向一个地址(例如localhost:9001),并将这些请求转发/代理到localhost:9001/X-SERVICE-NAME

但是,我更喜欢更简单的解决方案,例如:

ab-document-store.ribbon.listOfServers: localhost:8025/ab-document-store
但这不起作用:(

引言 我找到了一个解决方案,使用一个检测头的代理。 因此,我在java端有一个假拦截器,它将头
x-service-name
附加到每个假请求

我还有一个NodeJS代理,它分析请求,查找
x-service-name
,然后将请求重新写入:
x-service-name
/
原始请求路径

这使我可以在zuul网关后面拥有所有的微服务,但也可以使用eureka over ride访问它们

Java伪拦截器 NodeJS代理 在本例中,我的zuul网关(或另一个代理)位于localhost:9001上。 我正在收听
localhost:1200

let enableProxyForJava = process.env.ENABLE_PROXY_FOR_JAVA;
if (enableProxyForJava != undefined &&  enableProxyForJava.toLowerCase() === 'true') {
    var httpProxyJava = require('http-proxy');
    var proxyJava = httpProxyJava.createProxy();

    gutil.log(  gutil.colors.green('Enabling Proxy for Java. Set your Eureka overrides to localhost:1200.') );

    require('http').createServer(function(req, res) {
        console.log("req.headers['x-service-name'] = " + req.headers['x-service-name']);
        console.log("Before req.url:"+ req.url);

        if( req.headers['x-service-name'] != undefined){
            let change =  req.headers['x-service-name'] +req.url;
            console.log("After req.url:"+ change);
            req.url = change;
        }

        proxyJava.web(req, res, {
            target: 'http://localhost:9001/'
        });

    }).listen(1200);
}
Java应用程序中具有外部客户端的属性文件
let enableProxyForJava = process.env.ENABLE_PROXY_FOR_JAVA;
if (enableProxyForJava != undefined &&  enableProxyForJava.toLowerCase() === 'true') {
    var httpProxyJava = require('http-proxy');
    var proxyJava = httpProxyJava.createProxy();

    gutil.log(  gutil.colors.green('Enabling Proxy for Java. Set your Eureka overrides to localhost:1200.') );

    require('http').createServer(function(req, res) {
        console.log("req.headers['x-service-name'] = " + req.headers['x-service-name']);
        console.log("Before req.url:"+ req.url);

        if( req.headers['x-service-name'] != undefined){
            let change =  req.headers['x-service-name'] +req.url;
            console.log("After req.url:"+ change);
            req.url = change;
        }

        proxyJava.web(req, res, {
            target: 'http://localhost:9001/'
        });

    }).listen(1200);
}
mbak-microservice1.ribbon.NIWSServerListClassName:com.netflix.loadbalancer.ConfigurationBasedServerList
mbak-microservice1.ribbon.listOfServers: localhost:1200

mbak-microservice2.ribbon.NIWSServerListClassName:com.netflix.loadbalancer.ConfigurationBasedServerList
mbak-microservice2.ribbon.listOfServers: localhost:1200

mbak-document-store.ribbon.NIWSServerListClassName:com.netflix.loadbalancer.ConfigurationBasedServerList
mbak-document-store.ribbon.listOfServers: localhost:1200