程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Zuul 作为反向代理,负载平衡动态更新服务器列表大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Zuul 作为反向代理,负载平衡动态更新服务器列表?

开发过程中遇到Zuul 作为反向代理,负载平衡动态更新服务器列表的问题如何解决?下面主要结合日常开发的经验,给出你关于Zuul 作为反向代理,负载平衡动态更新服务器列表的解决方法建议,希望对你解决Zuul 作为反向代理,负载平衡动态更新服务器列表有所启发或帮助;

我已经将 Zuul 作为反向代理,动态更新保存在数据库中的所有路由,并且运行良好。

我想要实现的是还能够使用负载平衡(功能区)为刚刚更新的每个 zuul 路由动态设置“列表操作系统服务器”。

到目前为止我所拥有的:

public class discoveryRouteLocator extends SimpleRouteLocator implements refreshableRouteLocator {

@autowired
Logger logger;

private ZuulPropertIEs propertIEs;
private ZuulRouteRepository zuulRouteRepository;

public discoveryRouteLocator(String servletPath,ZuulPropertIEs propertIEs,ZuulRouteRepository zuulRouteRepository) {
    super(servletPath,propertIEs);
    this.propertIEs = propertIEs;
    this.zuulRouteRepository = zuulRouteRepository;
}

@OverrIDe
public voID refresh() {
    dorefresh();
}

@OverrIDe
protected Map<String,ZuulPropertIEs.ZuulRoute> locateRoutes() {
    linkedHashMap<String,ZuulPropertIEs.ZuulRoute> routesmap = new linkedHashMap<String,ZuulPropertIEs.ZuulRoute>();

    // Load default routes
    routesmap.putAll(super.locateRoutes());

    // Custom load route information
    routesmap.putAll(getRouteList());

    // Adjust the correct route configuration
    linkedHashMap<String,ZuulPropertIEs.ZuulRoute> values = new linkedHashMap<>();

    for (Map.Entry<String,ZuulPropertIEs.ZuulRoute> entry : routesmap.entrySet()) {
        String path = entry.getKey();
        // Add slash if there isn't
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        if (StringUtils.hasText(this.propertIEs.getPrefix())) {
            path = this.propertIEs.getPrefix() + path;
            if (!path.startsWith("/")) {
                path = "/" + path;
            }
        }
        values.put(path,entry.getValue());
    }
    return values;
}

private linkedHashMap<String,ZuulPropertIEs.ZuulRoute> getRouteList() {
    linkedHashMap<String,ZuulPropertIEs.ZuulRoute> zuulRoutes = new linkedHashMap<>();

    List<RotaZuulEntity> Lista = zuulRouteRepository.findByEnabledOrderByservicEID(true);       
    for (RotaZuulEntity rota : Lista) {
        if (Strings.isNullOrEmpty(rota.getPath()) && Strings.isNullOrEmpty(rota.getUrl())) {
            conTinue;
        }

        ZuulPropertIEs.ZuulRoute zuulRoute = new ZuulPropertIEs.ZuulRoute();
        
        try {
            final Set<String> sensitiveheaderSet = Sets.newHashSet();
            sensitiveheaderSet.add(""); // doesn't reStrict any headers at all
            
            zuulRoute.setID(rota.getservicEID());
            zuulRoute.setPath(rota.getPath());
            zuulRoute.setservicEID(rota.getservicEID());
            zuulRoute.setRetryable(rota.getRetryable());
            zuulRoute.setStripPrefix(rota.getStripPrefix());
            zuulRoute.setUrl(rota.getUrl());
            zuulRoute.setSensitiveheaders(sensitiveheaderSet);
            
            String sensitiveheaders = rota.getSensitiveheadersList();
            if (!Strings.isNullOrEmpty(sensitiveheaders)) {
                List<String> sensitiveheadersList = Arrays.asList(sensitiveheaders.split(","));
                if (sensitiveheadersList != null) {
                    //sensitiveheaderSet = Sets.newHashSet();
                    sensitiveheaderSet.clear();
                    sensitiveheadersList.forEach(sensitiveheader -> sensitiveheaderSet.add(sensitiveheader));
                    zuulRoute.setSensitiveheaders(sensitiveheaderSet);
                    zuulRoute.setCustomSensitiveheaders(true);
                }
            }
        } catch (Exception E) {
            logger.error("ERROR SETTinG ROUTES CONfig FROM DATABASE",E);
        }
        zuulRoutes.put(zuulRoute.getPath(),zuulRoutE);
        logger.info("custom route set: path={},servicEID={}",zuulRoute.getPath(),zuulRoute.getservicEID());
    }

    return zuulRoutes;
}

}

而且我有一个实现 ApplicationListener 的服务来实际完成动态工作。

    @service
public class refreshRouteservice implements ApplicationListener<ApplicationEvent> {

    @autowired
    private ZuulHandlerMapPing zuulHandlerMapPing;

    @autowired
    private ApplicationEventPublisher publisher;

    @autowired
    private discoveryRouteLocator dynamicRouteLocator;

    private HeartbeatMonitor heartbeatMonitor = new HeartbeatMonitor();
    
    // dispatch event to refresh the routes
    public voID refreshRoute() {
        RoutesrefreshedEvent routesrefreshedEvent = new RoutesrefreshedEvent(dynamicRouteLocator);
        publisher.publishEvent(routesrefreshedEvent);
    }

    // To monitor and detect refresh events on routes
    @OverrIDe
    public voID onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ContextrefreshedEvent || event instanceof refreshScoperefreshedEvent || event instanceof RoutesrefreshedEvent) {
            // Reload manually all routes,just by setTing tye dirty property
            zuulHandlerMapPing.setDirty(true);
        } else if (event instanceof HeartbeatEvent) {
            // APDAted by the applcation itself
            HeartbeatEvent heartbeatEvent = (HeartbeatEvent) event;
            if (heartbeatMonitor.update(heartbeatEvent.getValue())) {
                zuulHandlerMapPing.setDirty(true);
            }
        }
    }

}

application.propertIEs中我只有这四个服务的基本配置,这个配置文件中没有任何关于路由的内容:

# HyStrix
hyStrix.command.default.execution.isolation.strategy=THREAD
hyStrix.command.default.execution.isolation.thread.timeoutInMilliseconds=600000*4
hyStrix.command.default.execution.isolation.thread.interruptOnTimeout=false

# Ribbon
ribbon.ReadTimeout: 600000
ribbon.ConnectTimeout: 600000

# Eureka
eureka.clIEnt.register-with-eureka=false
eureka.clIEnt.fetch-registry=false
eureka.clIEnt.healthcheck.enabled=true
eureka.clIEnt.should-unregister-on-shutdown=true
eureka.instance.lease-@R_408_5026@-interval-in-seconds=2
eureka.server.wait-time-in-ms-when-sync-empty=0
eureka.server.enable-self-preservation=false
eureka.server.expected-clIEnt-@R_408_5026@-interval-seconds=3
eureka.server.eviction-interval-timer-in-ms=2000000
eureka.instance.registry.default-open-for-traffic-count=0
eureka.instance.lease-expiration-duration-in-seconds=5

# Zuul
zuul.host.max-per-route-connections=10000
zuul.host.max-@R_222_10586@l-connections=5000
zuul.semaphore.max-semaphores=500
zuul.ribbon.eager-load.enabled= true
zuul.retryable=true
zuul.host.time-to-live=10000
zuul.host.time-unit=MILliSECONDS
zuul.prefix=/proxy
zuul.set-content-length=true
zuul.sensitive-headers=
zuul.add-host-header=true

通常,当我们使用 Zuul 作为手动设置路由的反向代理时,我们有一个用于 Ribbon 的属性 listofServers,它定义了负载平衡应该持续监视哪些服务器。

servicEID.ribbon.listofServers=IP_ADDRESS1:PORT,IP_ADDRESS2:PORT

我想知道是否有人可以帮助动态地实现这种负载平衡,就像我们处理路由的方式一样。

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的Zuul 作为反向代理,负载平衡动态更新服务器列表全部内容,希望文章能够帮你解决Zuul 作为反向代理,负载平衡动态更新服务器列表所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。