徐州建设工程网官网/seo优化系统

徐州建设工程网官网,seo优化系统,模板网站和定制网站的区别是什么,企业网站留言Nacos客户端入口 首先在我们使用Nacos时&#xff0c;会在客户端引入对应的依赖&#xff0c;例如需要Nacos的注册中心功能需要引入 <!--nacos-discovery 注册中心依赖--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-c…

Nacos客户端入口

首先在我们使用Nacos时,会在客户端引入对应的依赖,例如需要Nacos的注册中心功能需要引入

        <!--nacos-discovery  注册中心依赖--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>

而熟悉Springboot的自然知道,为什么引用一个starter就能引入对应的功能?这离不开Springboot的自动配置功能,这也是其特点之一。
而说到自动配置,那自然离不开自动配置类,他即是功能的入口。
那Nacos有哪些自动配置类呢?

NacosServiceAutoConfiguration

这是Nacos服务器通信的基础设施,是整个 Nacos 集成的"连接管理中心"。

@Configuration(proxyBeanMethods = false
)
@ConditionalOnDiscoveryEnabled
@ConditionalOnNacosDiscoveryEnabled
public class NacosServiceAutoConfiguration {public NacosServiceAutoConfiguration() {}@Beanpublic NacosServiceManager nacosServiceManager() {return new NacosServiceManager();}// Spring 应用 → NacosServiceManager → Nacos SDK → Nacos 服务器}

NacosServiceManager 被多个 Nacos 相关组件使用:

服务发现组件:
NacosServiceDiscovery 使用它获取 NamingService 实例
NacosRegistration 使用它进行服务注册

配置中心组件:
NacosConfigManager 使用它获取 ConfigService 实例
配置刷新机制依赖它提供的服务对象

延迟初始化: NacosServiceManager 不会在创建时立即连接 Nacos 服务器,而是采用延迟初始化策略

按需创建服务对象:
当系统需要 NamingService (服务发现) 时,按需创建并缓存
当系统需要 ConfigService (配置中心) 时,按需创建并缓存

服务对象缓存机制:

基于属性(如命名空间、分组)创建缓存键
相同配置的服务请求复用同一个服务对象实例
避免重复创建连接,优化资源使用

生命周期管理:
与 Spring 容器生命周期绑定
在应用关闭时释放资源、关闭连接

NacosDiscoveryAutoConfiguration

这是Nacos的服务发现自动配置类,主要有如下功能

  • 自动装配 Nacos 服务发现组件
  • 初始化服务发现相关的基础设施 Bean
  • 使应用能够与 Nacos 服务器进行服务注册与发现交互
@Configuration(proxyBeanMethods = false
)
@ConditionalOnDiscoveryEnabled
@ConditionalOnNacosDiscoveryEnabled
public class NacosDiscoveryAutoConfiguration {public NacosDiscoveryAutoConfiguration() {}@Bean@ConditionalOnMissingBeanpublic NacosDiscoveryProperties nacosProperties() {return new NacosDiscoveryProperties();}@Bean@ConditionalOnMissingBean // 需要传入NacosServiceManager ,通过其进行服务发现public NacosServiceDiscovery nacosServiceDiscovery(NacosDiscoveryProperties discoveryProperties, NacosServiceManager nacosServiceManager) { return new NacosServiceDiscovery(discoveryProperties, nacosServiceManager);}
}

NacosServiceRegistryAutoConfiguration

上面两个,一个是提供操作服务的Service,一个是提供服务发现的功能,那这个就是提供自动注册的功能,其自动配置类如下:

@Configuration(proxyBeanMethods = false
)
@EnableConfigurationProperties
@ConditionalOnNacosDiscoveryEnabled // 自定义Condition注解,具体参考自动配置相关知识
@ConditionalOnProperty(value = {"spring.cloud.service-registry.auto-registration.enabled"},matchIfMissing = true
)
// 用于控制自动配置顺序
@AutoConfigureAfter({AutoServiceRegistrationConfiguration.class, AutoServiceRegistrationAutoConfiguration.class, NacosDiscoveryAutoConfiguration.class})
public class NacosServiceRegistryAutoConfiguration {public NacosServiceRegistryAutoConfiguration() {}@Bean // 用于提供注册功能的Service,其调用NacosServiceManager 进行实现 父类为 ServiceRegistry<R>public NacosServiceRegistry nacosServiceRegistry(NacosServiceManager nacosServiceManager, NacosDiscoveryProperties nacosDiscoveryProperties) {return new NacosServiceRegistry(nacosServiceManager, nacosDiscoveryProperties);}@Bean // 承载服务注册所需的实例信息,是注册数据的容器@ConditionalOnBean({AutoServiceRegistrationProperties.class})public NacosRegistration nacosRegistration(ObjectProvider<List<NacosRegistrationCustomizer>> registrationCustomizers, NacosDiscoveryProperties nacosDiscoveryProperties, ApplicationContext context) {return new NacosRegistration((List)registrationCustomizers.getIfAvailable(), nacosDiscoveryProperties, context);}@Bean // 自动服务注册的触发器,监听事件并执行注册@ConditionalOnBean({AutoServiceRegistrationProperties.class})public NacosAutoServiceRegistration nacosAutoServiceRegistration(NacosServiceRegistry registry, AutoServiceRegistrationProperties autoServiceRegistrationProperties, NacosRegistration registration) {return new NacosAutoServiceRegistration(registry, autoServiceRegistrationProperties, registration);}
}

自动注册剖析

在上述第三个自动配置生效后,会返回一个 new NacosAutoServiceRegistration(registry, autoServiceRegistrationProperties, registration);
分析其继承关系。
在这里插入图片描述
可以发现其关系图中,有 ApplicationListener< WebServerInitializedEvent > 这个类,其作用是会监听对应事件,而这个事件就是WebServerInitializedEvent 代表嵌入式 Web 服务器已完成初始化并启动的通知机制。

public void onApplicationEvent(WebServerInitializedEvent event) {ApplicationContext context = event.getApplicationContext();if (!(context instanceof ConfigurableWebServerApplicationContext) || !"management".equals(((ConfigurableWebServerApplicationContext)context).getServerNamespace())) {this.port.compareAndSet(0, event.getWebServer().getPort());this.start(); // start方法}}
==

start 方法

public void start() {if (!this.isEnabled()) {if (logger.isDebugEnabled()) {logger.debug("Discovery Lifecycle disabled. Not starting");}} else {if (!this.running.get()) {this.context.publishEvent(new InstancePreRegisteredEvent(this, this.getRegistration()));this.registrationLifecycles.forEach((registrationLifecycle) -> {registrationLifecycle.postProcessBeforeStartRegister(this.getRegistration());});this.register(); // 进行注册this.registrationLifecycles.forEach((registrationLifecycle) -> {registrationLifecycle.postProcessAfterStartRegister(this.getRegistration());});if (this.shouldRegisterManagement()) {this.registrationManagementLifecycles.forEach((registrationManagementLifecycle) -> {registrationManagementLifecycle.postProcessBeforeStartRegisterManagement(this.getManagementRegistration());});this.registerManagement();this.registrationManagementLifecycles.forEach((registrationManagementLifecycle) -> {registrationManagementLifecycle.postProcessAfterStartRegisterManagement(this.getManagementRegistration());});}this.context.publishEvent(new InstanceRegisteredEvent(this, this.getConfiguration()));this.running.compareAndSet(false, true);}}}

register方法

private final ServiceRegistry<R> serviceRegistry;
......
protected void register() {if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) {log.debug("Registration disabled.");} else {if (this.registration.getPort() < 0) {this.registration.setPort(this.getPort().get());}super.register();}}
// ==> super.register()
protected void register() {this.serviceRegistry.register(this.getRegistration()); // NacosRegistration}

serviceRegistry.register

public void register(Registration registration) {if (StringUtils.isEmpty(registration.getServiceId())) {log.warn("No service to register for nacos client...");} else {NamingService namingService = this.namingService();String serviceId = registration.getServiceId();String group = this.nacosDiscoveryProperties.getGroup();Instance instance = this.getNacosInstanceFromRegistration(registration);try {namingService.registerInstance(serviceId, group, instance); // 进行注册log.info("nacos registry, {} {} {}:{} register finished", new Object[]{group, serviceId, instance.getIp(), instance.getPort()});} catch (Exception var7) {Exception e = var7;if (this.nacosDiscoveryProperties.isFailFast()) {log.error("nacos registry, {} register failed...{},", new Object[]{serviceId, registration.toString(), e});ReflectionUtils.rethrowRuntimeException(e);} else {log.warn("Failfast is false. {} register failed...{},", new Object[]{serviceId, registration.toString(), e});}}}}

namingService.registerInstance

public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {NamingUtils.checkInstanceIsLegal(instance);this.checkAndStripGroupNamePrefix(instance, groupName);this.clientProxy.registerService(serviceName, groupName, instance); // 通过代理去获取对应的注册服务}

clientProxy.registerService

public void registerService(String serviceName, String groupName, Instance instance) throws NacosException {this.getExecuteClientProxy(instance).registerService(serviceName, groupName, instance);}

getExecuteClientProxy

 private NamingClientProxy getExecuteClientProxy(Instance instance) {return (NamingClientProxy)(!instance.isEphemeral() && !this.grpcClientProxy.isAbilitySupportedByServer(AbilityKey.SERVER_SUPPORT_PERSISTENT_INSTANCE_BY_GRPC) ? this.httpClientProxy : this.grpcClientProxy);}

是否临时服务判断

Nacos 提供两种注册方式,一种是临时,会进行心跳检测,如果心跳检测不超过,或者超时,就会被任务不健康实例,会对其进行下线,其次会将其加入一个队列中,去检测是否健康,如果不健康,那就移除。
一种是永久注册,下面详细介绍

临时实例 (Ephemeral = true)

  1. 生命周期特点

    • 基于心跳维持,客户端需要定期发送心跳包
    • 如果一定时间内未收到心跳,Nacos会自动将实例标记为不健康,并最终移除
  2. 适用场景

    • 适合大多数微服务场景,特别是云原生环境
    • 实例会随应用的启停自动注册和注销
  3. 数据存储

    • 存储在内存中,提供更高的性能
    • 在Nacos服务器重启时数据会丢失

持久化实例 (Ephemeral = false)

  1. 生命周期特点

    • 实例信息持久化到磁盘
    • 不依赖心跳维持,即使客户端宕机实例也不会被自动移除
  2. 适用场景

    • 适合那些需要保持稳定注册状态的场景
    • 如某些关键基础设施服务,需要手动管理其上下线
  3. 数据存储

    • 数据持久化到磁盘数据库中
    • Nacos重启后数据仍然存在

HttpClient.registerService

public void registerService(String serviceName, String groupName, Instance instance) throws NacosException {LogUtils.NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with instance: {}", new Object[]{this.namespaceId, serviceName, instance});String groupedServiceName = NamingUtils.getGroupedName(serviceName, groupName);if (instance.isEphemeral()) {throw new UnsupportedOperationException("Do not support register ephemeral instances by HTTP, please use gRPC replaced.");} else {Map<String, String> params = new HashMap(32);params.put("namespaceId", this.namespaceId);params.put("serviceName", groupedServiceName);params.put("groupName", groupName);params.put("clusterName", instance.getClusterName());params.put("ip", instance.getIp());params.put("port", String.valueOf(instance.getPort()));params.put("weight", String.valueOf(instance.getWeight()));params.put("enable", String.valueOf(instance.isEnabled()));params.put("healthy", String.valueOf(instance.isHealthy()));params.put("ephemeral", String.valueOf(instance.isEphemeral()));params.put("metadata", JacksonUtils.toJson(instance.getMetadata()));this.reqApi(UtilAndComs.nacosUrlInstance, params, "POST");}}

Grpc.registerService

 public void registerService(String serviceName, String groupName, Instance instance) throws NacosException {LogUtils.NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with instance {}", new Object[]{this.namespaceId, serviceName, instance});if (instance.isEphemeral()) {this.registerServiceForEphemeral(serviceName, groupName, instance);} else {this.doRegisterServiceForPersistent(serviceName, groupName, instance);}}

自上,自动注册服务就到此为止

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/898031.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java中关于Optional的 orElse 操作,以及 orElse 与 orElseGet 的区别

文章目录 1. 大概说明2. 详细分析2.1 .orElse 操作2.2 .orElse 的作用&#xff1a;避免空指针异常2.3 为什么要用&#xff1f;2.4 orElseGet如何使用2.5 orElse和orElseGet的区别 1. 大概说明 这篇文章的目的是为了说明&#xff1a; orElse 如何使用orElseGet 如何使用两者的…

【eNSP实战】配置端口映射(NAT Server)

拓图 要求&#xff1a; 将AR1上的GE 0/0/1接口的地址从TCP协议的80端口映射到内网 Web服务器80端口 AR1接口配置 interface GigabitEthernet0/0/0ip address 192.168.0.1 255.255.255.0 # interface GigabitEthernet0/0/1ip address 11.0.1.1 255.255.255.0 # ip route-s…

prometheus自定义监控(pushgateway和blackbox)和远端存储VictoriaMetrics

1 pushgateway采集 1.1 自定义采集键值 如果自定义采集需求时&#xff0c;就可以通过写脚本 定时任务定期发送数据到 pushgateway 达到自定义监控 1.部署 pushgateway&#xff0c;以 10.0.0.42 节点为例 1.下载组件 wget https://github.com/prometheus/pushgateway/relea…

feign配置重试次数不生效

一、问题产生 自定义重试次数&#xff0c;实现如下 ConditionalOnProperty(prefix "feign.client", name "enable", havingValue "true") Configuration public class FeignConfig {Beanpublic FeignInterceptor feignInterceptor() {retur…

Dify使用部署与应用实践

最近在研究AI Agent&#xff0c;发现大家都在用Dify&#xff0c;但Dify部署起来总是面临各种问题&#xff0c;而且我在部署和应用测试过程中也都遇到了&#xff0c;因此记录如下&#xff0c;供大家参考。Dify总体来说比较灵活&#xff0c;扩展性比较强&#xff0c;适合基于它做…

剖析sentinel的限流和熔断

sentinel的限流和熔断 前言源码分析滑动窗口源码限流源码熔断源码 完结撒花&#xff0c;sentinel源码还是挺简单的&#xff0c;如有需要收藏的看官&#xff0c;顺便也用发财的小手点点赞哈&#xff0c;如有错漏&#xff0c;也欢迎各位在评论区评论&#xff01; 前言 平时发起一…

硬盘分区误删后的数据救赎

一、硬盘分区误删的概述 硬盘分区误删&#xff0c;是许多电脑用户在使用过程中可能遭遇的棘手问题。分区&#xff0c;作为硬盘上存储数据的逻辑单元&#xff0c;一旦被误删除&#xff0c;不仅会导致该分区内的所有数据瞬间消失&#xff0c;还可能影响到整个硬盘的存储结构和数…

老牌软件,方便处理图片,量大管饱。

今天介绍的图片查看器名字是&#xff1a;FastStone Image Viewer&#xff0c;是一款可查看、编辑、批量重命名、批量转换的图片查看软件。文末有分享链接。 软件以资源管理器的方式管理你电脑里的图片&#xff0c;点击左侧可选择文件夹&#xff0c;右边可预览图片。 软妹用得最…

Python进阶编程总结

&#x1f9d1; 博主简介&#xff1a;CSDN博客专家&#xff0c;历代文学网&#xff08;PC端可以访问&#xff1a;https://literature.sinhy.com/#/literature?__c1000&#xff0c;移动端可微信小程序搜索“历代文学”&#xff09;总架构师&#xff0c;15年工作经验&#xff0c;…

Redis复制(replica)主从模式

Redis主从复制 Redis 的复制&#xff08;replication&#xff09;功能允许用户根据一个 Redis 服务器来创建任意多个该服务器的复制品&#xff0c;其中被复制的服务器为主服务器&#xff08;master&#xff09;&#xff0c;而通过复制创建出来的服务器复制品则为从服务器&#…

R语言零基础系列教程-03-RStudio界面介绍与关键设置

代码、讲义、软件回复【R语言03】获取。 设置位置: 菜单栏 - Tools - Blobal Options 设置 通用设置 设置面板左侧General选项 版本选择: 一般只用一个版本即可 默认工作目录设置: 你希望RStudio打开时是基于哪个目录进行工作可以不设置, 因为脚本一般都是放置在特定项目路…

微信小程序刷题逻辑实现:技术揭秘与实践分享

页面展示&#xff1a; 概述 在当今数字化学习的浪潮中&#xff0c;微信小程序以其便捷性和实用性&#xff0c;成为了众多学习者刷题备考的得力工具。今天&#xff0c;我们就来深入剖析一个微信小程序刷题功能的实现逻辑&#xff0c;从代码层面揭开其神秘面纱。 小程序界面布局…

JVM--垃圾回收

垃圾回收的概念 垃圾回收主要针对的是堆中的对象&#xff0c;堆是一个共享的区域&#xff0c;创建的对象和数组都放在这个位置。但是我们不能一直的创建对象&#xff0c;也不是所有的对象能一直存放&#xff0c;如果不进行垃圾回收&#xff0c;内存迟早会耗尽&#xff0c;及时…

【教程】继承中的访问控制 C++

目录 简介public&#xff0c;protected 和 private继承中的 public&#xff0c;protected 和 private示例 简介 在 C 中派生类可以通过 public&#xff0c;protected 和 private 三种修饰符决定基类成员在派生类中的访问级别 public&#xff0c;protected 和 private 公有成…

【2025】基于python+django的驾校招生培训管理系统(源码、万字文档、图文修改、调试答疑)

课题功能结构图如下&#xff1a; 驾校招生培训管理系统设计 一、课题背景 随着机动车保有量的不断增加&#xff0c;人们对驾驶技能的需求也日益增长。驾校作为驾驶培训的主要机构&#xff0c;面临着激烈的市场竞争和学员需求多样化等挑战。传统的驾校管理模式往往依赖于人工操作…

要登录的设备ip未知时的处理方法

目录 1 应用场景... 1 2 解决方法&#xff1a;... 1 2.1 wireshark设置... 1 2.2 获取网口mac地址&#xff0c;wireshark抓包前预过滤掉自身mac地址的影响。... 2 2.3 pc网口和设备对接... 3 2.3.1 情况1&#xff1a;... 3 2.3.2 情…

go 安装swagger

1、依赖安装&#xff1a; # 安装 swag 命令行工具 go install github.com/swaggo/swag/cmd/swaglatest# 安装 gin-swagger 和 swagger 文件的依赖 go get -u github.com/swaggo/gin-swagger go get -u github.com/swaggo/files 2、测试 cmd中输入&#xff1a; swag -v 如果…

网络安全反渗透 网络安全攻防渗透

网络渗透防范主要从两个方面来进行防范&#xff0c;一方面是从思想意识上进行防范&#xff0c;另一方面就是从技术方面来进行防范。 1.从思想意识上防范渗透 网络攻击与网络安全防御是正反两个方面&#xff0c;纵观容易出现网络安全事故或者事件的公司和个人&#xff0c;在这些…

技术视界|构建理想仿真平台,加速机器人智能化落地

在近期的 OpenLoong 线下技术分享会 上&#xff0c;松应科技联合创始人张小波进行了精彩的演讲&#xff0c;深入探讨了仿真技术在机器人智能化发展中的关键作用。他结合行业趋势&#xff0c;剖析了现有仿真平台的挑战&#xff0c;并描绘了未来理想仿真系统的设计理念与实现路径…

环境配置 | 5分钟极简Git入门:从零上手版本控制

你是否刚接触Git&#xff1f;别担心&#xff01;这篇指南将用最简单的步骤带你掌握Git核心操作&#xff0c;快速开启版本控制之旅&#xff01;✨ 1.git在win10上的下载安装 1.1.下载git 打开官方网站 Git - Downloadshttps://git-scm.com/downloads ​ ​​ 1.2.git安装 …