当前位置: 首页 > news >正文

英语课件做的好的网站优享购物官方网站

英语课件做的好的网站,优享购物官方网站,如何制作网站的横幅,苏州建能建设科技有限公司问题 Spring Cloud Gateway版本是2.2.9.RELEASE#xff0c;原本项目中依赖服务自动发现来自动配置路由到微服务的#xff0c;但是发现将spring.cloud.gateway.discovery.locator.enabledfalse 启动之后Gateway依然会将所有微服务自动注册到路由中#xff0c;百思不得其解原本项目中依赖服务自动发现来自动配置路由到微服务的但是发现将spring.cloud.gateway.discovery.locator.enabledfalse 启动之后Gateway依然会将所有微服务自动注册到路由中百思不得其解遂调试代码观察期启动过曾记录此文以供参考官方文档 可以看到此处明确表示可以通过设置该值来讲微服务中服务自动添加到配置中。按照这个线索找到了 org.springframework.cloud.gateway.discovery.DiscoveryClientRouteDefinitionLocator#getRouteDefinitions 这个类在之前的文章includeExpression中有提及过在构造函数中对成员变量 public class DiscoveryClientRouteDefinitionLocator implements RouteDefinitionLocator {private final DiscoveryLocatorProperties properties;...private FluxListServiceInstance serviceInstances;/*** Kept for backwards compatibility. You should use the reactive discovery client.* param discoveryClient the blocking discovery client* param properties the configuration properties* deprecated kept for backwards compatibility*/Deprecatedpublic DiscoveryClientRouteDefinitionLocator(DiscoveryClient discoveryClient,DiscoveryLocatorProperties properties) {this(discoveryClient.getClass().getSimpleName(), properties);serviceInstances Flux.defer(() - Flux.fromIterable(discoveryClient.getServices())).map(discoveryClient::getInstances).subscribeOn(Schedulers.boundedElastic());}public DiscoveryClientRouteDefinitionLocator(ReactiveDiscoveryClient discoveryClient,DiscoveryLocatorProperties properties) {this(discoveryClient.getClass().getSimpleName(), properties);serviceInstances discoveryClient.getServices().flatMap(service - discoveryClient.getInstances(service).collectList());}...这里会对serviceInstances进行初始化从注册中心获取所有已注册的微服务实例进行填充serviceInstance的获取结束了,在DiscoveryClientRouteDefinitionLocator还有一个重要的方法 #DiscoveryClientRouteDefinitionLocatorOverridepublic FluxRouteDefinition getRouteDefinitions() {SpelExpressionParser parser new SpelExpressionParser();Expression includeExpr parser.parseExpression(properties.getIncludeExpression());Expression urlExpr parser.parseExpression(properties.getUrlExpression());PredicateServiceInstance includePredicate;if (properties.getIncludeExpression() null|| true.equalsIgnoreCase(properties.getIncludeExpression())) {includePredicate instance - true;}else {includePredicate instance - {Boolean include includeExpr.getValue(evalCtxt, instance, Boolean.class);if (include null) {return false;}return include;};}return serviceInstances.filter(instances - !instances.isEmpty()).map(instances - instances.get(0)).filter(includePredicate).map(instance - {RouteDefinition routeDefinition buildRouteDefinition(urlExpr,instance);final ServiceInstance instanceForEval new DelegatingServiceInstance(instance, properties);for (PredicateDefinition original : this.properties.getPredicates()) {PredicateDefinition predicate new PredicateDefinition();predicate.setName(original.getName());for (Map.EntryString, String entry : original.getArgs().entrySet()) {String value getValueFromExpr(evalCtxt, parser,instanceForEval, entry);predicate.addArg(entry.getKey(), value);}routeDefinition.getPredicates().add(predicate);}for (FilterDefinition original : this.properties.getFilters()) {FilterDefinition filter new FilterDefinition();filter.setName(original.getName());for (Map.EntryString, String entry : original.getArgs().entrySet()) {String value getValueFromExpr(evalCtxt, parser,instanceForEval, entry);filter.addArg(entry.getKey(), value);}routeDefinition.getFilters().add(filter);}return routeDefinition;});} 根据includeExpr 配置的sl表达式判断当前的serviceInstance是否服务转发要求。填充好predicatefilter之后返回routeDefinitionurlExpr的默认值是lb://serverIdpredicate匹配的path就等于serverId有配置可以转换为小写spring.cloud.gateway.discovery.locator.lower-case-service-idtrue因为eureka默认会uppercase serverIdfilter中会StripPrefix1将路径中的serverId移除再转发给下游。路由定义的部分就这里了他的刷新逻辑依靠SpringRefreshContext 事件具体的定义逻辑在org.springframework.cloud.gateway.config.GatewayAutoConfiguration BeanConditionalOnClass(name org.springframework.cloud.client.discovery.event.HeartbeatMonitor)public RouteRefreshListener routeRefreshListener(ApplicationEventPublisher publisher) {return new RouteRefreshListener(publisher);} RouteRefreshListener 实现了ApplicationListenerApplicationEvent监控以下几个事件并按照条件进行逻辑执行 ContextRefreshEvent 启动容器或者beans重新加载RefreshScopeRefreshdEvent 是 Spring Cloud 中的一个事件当 RefreshScope 注解的 beans 被重新加载时发布。这个事件主要用于 Spring Cloud 的配置刷新机制当配置中心的配置信息发生变化时会触发该事件以刷新 beans。ParentHeartbeatEvent 是 Spring Cloud Bus 中的一个事件用于在父上下文通常是 Spring Cloud Config Server发生心跳时发布。这个事件用于通知子上下文如微服务关于父上下文的健康状态。HeartbeatEvent 是 Spring Cloud Bus 中的另一个事件用于在服务实例之间广播心跳消息以检测和维持各个服务的健康状态。 public void onApplicationEvent(ApplicationEvent event) {if (event instanceof ContextRefreshedEvent) {ContextRefreshedEvent refreshedEvent (ContextRefreshedEvent) event;if (!WebServerApplicationContext.hasServerNamespace(refreshedEvent.getApplicationContext(), management)) {reset();}}else if (event instanceof RefreshScopeRefreshedEvent|| event instanceof InstanceRegisteredEvent) {reset();}else if (event instanceof ParentHeartbeatEvent) {ParentHeartbeatEvent e (ParentHeartbeatEvent) event;resetIfNeeded(e.getValue());}else if (event instanceof HeartbeatEvent) {HeartbeatEvent e (HeartbeatEvent) event;resetIfNeeded(e.getValue());}}容器启动完成之后该监听器接收到ContextRefreshedEvent 会执行reset方法reset方法主要功能是发送一个RefreshRoutesEvent 事件定义如下 public class RefreshRoutesEvent extends ApplicationEvent {public RefreshRoutesEvent(Object source) {super(source);} }同样在AutoConfig中声明的CachingRouteLocator接收该参数 tip:另外还有个CachingRouteDefinitionLocator也生命监听 RefreshRoutesEvent事件但是默认配置下并不会产生任何逻辑 BeanPrimaryConditionalOnMissingBean(name cachedCompositeRouteLocator)// TODO: property to disable composite?public RouteLocator cachedCompositeRouteLocator(ListRouteLocator routeLocators) {return new CachingRouteLocator(new CompositeRouteLocator(Flux.fromIterable(routeLocators)));}event invoke方法如下 public void onApplicationEvent(RefreshRoutesEvent event) {try {fetch().collect(Collectors.toList()).subscribe(list - Flux.fromIterable(list).materialize().collect(Collectors.toList()).subscribe(signals - {applicationEventPublisher.publishEvent(new RefreshRoutesResultEvent(this));cache.put(CACHE_KEY, signals);}, throwable - handleRefreshError(throwable)));}catch (Throwable e) {handleRefreshError(e);}}CompositeRouteLocator的目的是混合 多个来源的Routers定义比如配置文件接口注册中心通过Bean形式定义的路由。可以理解成mixed route locator。 fetch方法就是获取所有RouteLocator的路由定义 private FluxRoute fetch() { return this.delegate.getRoutes().sort(AnnotationAwareOrderComparator.INSTANCE); } 这里其实就是指定之前org.springframework.cloud.gateway.discovery.DiscoveryClientRouteDefinitionLocator#getRouteDefinitions的方法也就是获取具体的路由定义获取成功之后放入缓存Fire RefreshRoutesResultEvent事件就解决了开发人员可以通过 监听RefreshRoutesResultEvent 主动感知当前的路由变化。路由加载流程到此结束 后语 我的问题是通过设置spring.cloud.gateway.discovery.locator.enabled依然没有办法让微服务注册失效在整个流程中并没有看到使用该数值的地方难道这是一个bug转念一想DiscoveryClientRouteDefinitionLocator只需要阻止该Bean的注册即可取消掉从eureka获取服务实例的能力通过引用关系可知 org.springframework.cloud.gateway.discovery.GatewayDiscoveryClientAutoConfiguration.ReactiveDiscoveryClientRouteDefinitionLocatorConfigurationorg.springframework.cloud.gateway.discovery.GatewayDiscoveryClientAutoConfiguration.BlockingDiscoveryClientRouteDefinitionLocatorConfiguration 这里两处找到了其定义一个是为reactive非阻塞io准备的一个是为阻塞io准备的根据源代码可知,具体执行提供那个Locator取决于spring.cloud.discovery.reactive.enabled我的项目中是通过VM Options参数的形式传入的-Dspring.cloud.config.discovery.enabledtrue所以我在这两个方法处都打了断点。 Configuration(proxyBeanMethods false)ConditionalOnProperty(value spring.cloud.discovery.reactive.enabled,matchIfMissing true)public static class ReactiveDiscoveryClientRouteDefinitionLocatorConfiguration {BeanConditionalOnProperty(name spring.cloud.gateway.discovery.locator.enabled)public DiscoveryClientRouteDefinitionLocator discoveryClientRouteDefinitionLocator(ReactiveDiscoveryClient discoveryClient,DiscoveryLocatorProperties properties) {return new DiscoveryClientRouteDefinitionLocator(discoveryClient, properties);}}Configuration(proxyBeanMethods false)DeprecatedConditionalOnProperty(value spring.cloud.discovery.reactive.enabled,havingValue false)public static class BlockingDiscoveryClientRouteDefinitionLocatorConfiguration {BeanConditionalOnProperty(name spring.cloud.gateway.discovery.locator.enabled)public DiscoveryClientRouteDefinitionLocator discoveryClientRouteDefinitionLocator(DiscoveryClient discoveryClient, DiscoveryLocatorProperties properties) {return new DiscoveryClientRouteDefinitionLocator(discoveryClient, properties);}}当我准备要执行调试模式想看看具体是哪个Bean被初始化的时候恍然大悟看到了DiscoveryClientRouteDefinitionLocator的依赖我自己在GatewayApplication 中手动注册了Bean Beanpublic DiscoveryClientRouteDefinitionLocator discoveryClientRouteLocator(ReactiveDiscoveryClient discoveryClient,DiscoveryLocatorProperties discoveryLocatorProperties) {return new DiscoveryClientRouteDefinitionLocator(discoveryClient, discoveryLocatorProperties);}真的是笑死出这个问题的原因是因为最开始的代码使用的出c4o老师生成的一开始并不明确该代码的作用后面查看文档才知道用于微服务发现。已经不在关注这里了。
http://www.dnsts.com.cn/news/178457.html

相关文章:

  • 战队头像logo免费自动生成器seo网站优化培训要多少钱
  • 怎么建设自己的卡盟网站石家庄有哪些做网站的公司
  • 北京学校网站建设珠海房产网
  • 企业网站建设的核心现在去环球中心会变黄码吗
  • 提高网站转化率响应式网站解决方案
  • 哪个网站做的系统好用跨境电商开店
  • 济南专业做企业网站电子商务营销的概念
  • 湖南长沙微信平台号铁岭网站建设网络优化
  • 广东移动网站精品课程网站设计报告
  • 萧山做网站的企业WordPress行距太大
  • 域名和主机有了怎么做网站制作人是干什么的
  • 网站建设与管理维护的答案李建青哈尔滨电商设计企业
  • 建设网站的要点wordpress仿百度首页
  • 游戏网站开发找什么人可建旅游商城网站建设
  • php会了 怎么做网站广州网站建设排名一览表
  • 网站交互主要做什么软件开发的基本过程
  • 小说网站开发的目的wordpress开发网上商城
  • 马鞍山网站建设cnfg商城app定制
  • 国办网站建设规范惠州seo排名公司
  • 网站模板绑定域名wordpress 多站点 子目录
  • 如何把做的网站与域名连接不上镇江网站建设方式优化
  • 现在asp做网站政务网站建设交流发言
  • 淘宝客网站程序模板东乡建设网站
  • 网站开发主管岗位职责wordpress列表提取文章第一张图片
  • 婚纱设计网站wordpress放哪个文件夹
  • 湖北网站建站系统哪家好河南网站公司
  • 做网站的公司现在还 赚钱吗wordpress禁用自动保存
  • 网站建设能做什么工作自己开网站需要什么
  • 龙岗建网站公司全渠道分销零售平台
  • 华企立方网站代发货网站建设