2026/4/18 3:18:21
网站建设
项目流程
vps能同时做网站同时做其它事吗,wordpress上传课件,完整网站开发看什么书,工业软件开发大家好#xff0c;我是小悟。
一、Nacos 详细介绍
1.1 什么是 Nacos
Nacos#xff08;Dynamic Naming and Configuration Service#xff09;是阿里巴巴开源的一款集服务发现、配置管理和服务管理于一体的平台。Nacos 的名字来源于 Naming and Configuration Service 的缩写…大家好我是小悟。一、Nacos 详细介绍1.1 什么是 NacosNacosDynamic Naming and Configuration Service是阿里巴巴开源的一款集服务发现、配置管理和服务管理于一体的平台。Nacos 的名字来源于Naming andConfigurationService 的缩写。1.2 Nacos 的核心特性1.2.1 服务发现与服务健康监测服务注册微服务启动时自动向 Nacos 注册自己的实例信息服务发现服务消费者可以从 Nacos 获取可用的服务实例列表健康检查支持 TCP/HTTP 健康检查自动剔除不健康实例负载均衡内置权重管理和流量控制策略1.2.2 动态配置管理配置集中管理所有环境配置集中存储在 Nacos 中动态刷新支持配置的动态刷新无需重启应用多环境支持支持命名空间Namespace隔离不同环境版本管理配置的版本管理和一键回滚监听查询实时监听配置变化并通知客户端1.2.3 动态 DNS 服务支持权重路由更易于实现自定义负载均衡策略支持路由规则灵活调整1.2.4 服务及其元数据管理服务元数据管理服务端点管理服务生命周期管理1.3 Nacos 的架构优势易用性提供简单的 UI 控制台易于操作高可用支持集群部署保证高可用性可扩展插件化设计易于扩展功能多语言支持支持 Java、Go、Python 等多种语言与 Spring Cloud 生态完美集成无缝替代 Eureka、Config 等组件二、Spring Cloud 集成 Nacos 详细步骤2.1 环境准备2.1.1 Nacos Server 安装# 下载 Nacos Server以 2.2.3 版本为例 wget https://github.com/alibaba/nacos/releases/download/2.2.3/nacos-server-2.2.3.tar.gz # 解压 tar -zxvf nacos-server-2.2.3.tar.gz # 启动单机模式 cd nacos/bin sh startup.sh -m standalone # 访问控制台 # http://localhost:8848/nacos # 默认账号/密码nacos/nacos2.2 创建 Spring Boot 项目2.2.1 Maven 父工程 pom.xml?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion groupIdcom.example/groupId artifactIdspring-cloud-nacos-demo/artifactId version1.0.0/version packagingpom/packaging parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version2.7.14/version relativePath/ /parent properties java.version1.8/java.version spring-cloud.version2021.0.8/spring-cloud.version spring-cloud-alibaba.version2021.0.5.0/spring-cloud-alibaba.version /properties dependencyManagement dependencies !-- Spring Cloud 依赖管理 -- dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-dependencies/artifactId version${spring-cloud.version}/version typepom/type scopeimport/scope /dependency !-- Spring Cloud Alibaba 依赖管理 -- dependency groupIdcom.alibaba.cloud/groupId artifactIdspring-cloud-alibaba-dependencies/artifactId version${spring-cloud-alibaba.version}/version typepom/type scopeimport/scope /dependency /dependencies /dependencyManagement modules modulenacos-provider/module modulenacos-consumer/module modulenacos-config/module modulenacos-gateway/module /modules /project2.3 服务注册与发现2.3.1 服务提供者Providerpom.xml?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd parent artifactIdspring-cloud-nacos-demo/artifactId groupIdcom.example/groupId version1.0.0/version /parent modelVersion4.0.0/modelVersion artifactIdnacos-provider/artifactId dependencies !-- Web 依赖 -- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency !-- Nacos 服务发现 -- dependency groupIdcom.alibaba.cloud/groupId artifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId /dependency !-- 健康检查 -- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-actuator/artifactId /dependency /dependencies /projectapplication.ymlserver: port: 8081 spring: application: name: nacos-provider-service cloud: nacos: discovery: # Nacos Server 地址 server-addr: localhost:8848 # 命名空间用于环境隔离默认public namespace: public # 分组默认DEFAULT_GROUP group: DEFAULT_GROUP # 集群名称 cluster-name: BJ # 服务注册的IP ip: 127.0.0.1 # 服务注册的端口 port: 8081 # 元数据 metadata: version: 1.0 author: example # 是否启用Nacos enabled: true # 健康检查配置 management: endpoints: web: exposure: include: * endpoint: health: show-details: always启动类 ProviderApplication.javapackage com.example.provider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; SpringBootApplication EnableDiscoveryClient // 启用服务发现 public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } } RestController class ProviderController { GetMapping(/hello/{name}) public String hello(PathVariable String name) { return String.format(Hello %s, this is nacos provider!, name); } GetMapping(/health) public String health() { return Provider service is healthy!; } }2.3.2 服务消费者Consumerpom.xml?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd parent artifactIdspring-cloud-nacos-demo/artifactId groupIdcom.example/groupId version1.0.0/version /parent modelVersion4.0.0/modelVersion artifactIdnacos-consumer/artifactId dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency !-- Nacos 服务发现 -- dependency groupIdcom.alibaba.cloud/groupId artifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId /dependency !-- OpenFeign 服务调用 -- dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-openfeign/artifactId /dependency !-- LoadBalancer 负载均衡 -- dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-loadbalancer/artifactId /dependency /dependencies /projectapplication.ymlserver: port: 8082 spring: application: name: nacos-consumer-service cloud: nacos: discovery: server-addr: localhost:8848 namespace: public group: DEFAULT_GROUP # 启用 LoadBalancer spring.cloud.loadbalancer.ribbon.enabled: false # Feign 配置 feign: client: config: default: connectTimeout: 5000 readTimeout: 5000 loggerLevel: basic启动类 ConsumerApplication.javapackage com.example.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; SpringBootApplication EnableDiscoveryClient EnableFeignClients public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } Bean LoadBalanced // 启用负载均衡 public RestTemplate restTemplate() { return new RestTemplate(); } } RestController class ConsumerController { private final RestTemplate restTemplate; private final ProviderFeignClient providerFeignClient; public ConsumerController(RestTemplate restTemplate, ProviderFeignClient providerFeignClient) { this.restTemplate restTemplate; this.providerFeignClient providerFeignClient; } // 使用 RestTemplate 调用 GetMapping(/call/rest/{name}) public String callByRestTemplate(PathVariable String name) { String url http://nacos-provider-service/hello/ name; return restTemplate.getForObject(url, String.class); } // 使用 Feign 调用 GetMapping(/call/feign/{name}) public String callByFeign(PathVariable String name) { return providerFeignClient.hello(name); } } // Feign 客户端接口 org.springframework.cloud.openfeign.FeignClient( value nacos-provider-service, path / ) interface ProviderFeignClient { GetMapping(/hello/{name}) String hello(PathVariable String name); GetMapping(/health) String health(); }2.4 配置中心集成2.4.1 配置管理模块pom.xml?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd parent artifactIdspring-cloud-nacos-demo/artifactId groupIdcom.example/groupId version1.0.0/version /parent modelVersion4.0.0/modelVersion artifactIdnacos-config/artifactId dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency !-- Nacos 配置管理 -- dependency groupIdcom.alibaba.cloud/groupId artifactIdspring-cloud-starter-alibaba-nacos-config/artifactId /dependency !-- Nacos 服务发现 -- dependency groupIdcom.alibaba.cloud/groupId artifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId /dependency !-- 配置刷新 -- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-actuator/artifactId /dependency /dependencies /projectbootstrap.yml配置文件优先级高于 application.yml# bootstrap.yml spring: application: name: nacos-config-service profiles: active: dev cloud: nacos: config: # Nacos Server 地址 server-addr: localhost:8848 # 配置文件后缀 file-extension: yaml # 命名空间环境隔离 namespace: dev # 分组 group: DEFAULT_GROUP # 共享配置 shared-configs: ->2.5 使用 Spring Cloud Gateway 集成 Nacospom.xml?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd parent artifactIdspring-cloud-nacos-demo/artifactId groupIdcom.example/groupId version1.0.0/version /parent modelVersion4.0.0/modelVersion artifactIdnacos-gateway/artifactId dependencies !-- Spring Cloud Gateway -- dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-gateway/artifactId /dependency !-- Nacos 服务发现 -- dependency groupIdcom.alibaba.cloud/groupId artifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId /dependency !-- Nacos 配置中心 -- dependency groupIdcom.alibaba.cloud/groupId artifactIdspring-cloud-starter-alibaba-nacos-config/artifactId /dependency /dependencies /projectapplication.ymlserver: port: 8080 spring: application: name: nacos-gateway cloud: nacos: discovery: server-addr: localhost:8848 namespace: public group: DEFAULT_GROUP gateway: discovery: locator: enabled: true # 启用服务发现 lower-case-service-id: true routes: - id: provider-service uri: lb://nacos-provider-service predicates: - Path/provider/** filters: - StripPrefix1 - AddRequestHeaderX-Request-Foo, Bar - name: RequestRateLimiter args: redis-rate-limiter.replenishRate: 10 redis-rate-limiter.burstCapacity: 20 key-resolver: #{pathKeyResolver} - id: consumer-service uri: lb://nacos-consumer-service predicates: - Path/consumer/** filters: - StripPrefix1 - id: config-service uri: lb://nacos-config-service predicates: - Path/config/** filters: - StripPrefix1网关配置类 GatewayApplication.javapackage com.example.gateway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver; import org.springframework.context.annotation.Bean; import reactor.core.publisher.Mono; SpringBootApplication EnableDiscoveryClient public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } Bean public KeyResolver pathKeyResolver() { return exchange - Mono.just( exchange.getRequest().getPath().value() ); } }2.6 Nacos 集群部署配置可选cluster.conf 配置properties# Nacos 集群节点配置 192.168.1.101:8848 192.168.1.102:8848 192.168.1.103:8848application.properties 配置properties# 集群模式 spring.datasource.platformmysql db.num1 db.url.0jdbc:mysql://127.0.0.1:3306/nacos?characterEncodingutf8connectTimeout1000socketTimeout3000autoReconnecttrueuseUnicodetrueuseSSLfalseserverTimezoneUTC db.userroot db.password123456三、详细总结3.1 集成优势3.1.1 一站式解决方案Nacos 提供了服务发现和配置管理的统一平台避免了使用多个独立组件如 Eureka Config Bus带来的复杂性和维护成本。3.1.2 高性能与高可用服务发现基于 Raft 协议保证数据一致性配置管理基于 Derby/MySQL 存储支持集群部署健康检查支持 TCP/HTTP/MYSQL 多种健康检查方式3.1.3 易于使用和运维提供友好的 Web 控制台完整的 RESTful API丰富的监控指标3.2 最佳实践建议3.2.1 命名空间规划# 建议按环境划分命名空间 namespace: dev: 开发环境 test: 测试环境 pre: 预发环境 prod: 生产环境3.2.2 配置管理策略配置分类存储公共配置使用shared-configs应用配置按应用独立存储环境配置使用不同命名空间配置版本控制重要配置变更前进行备份使用配置历史版本功能建立配置变更审核机制3.2.3 服务治理权重管理根据服务器性能设置不同权重元数据管理利用 metadata 存储版本、区域等信息集群容灾配置多个集群实现容灾切换3.3 注意事项3.3.1 网络与安全生产环境建议使用内网域名访问启用 Nacos 的认证功能配置合适的网络策略和防火墙规则3.3.2 监控与告警监控 Nacos Server 的 CPU、内存、磁盘使用率设置服务实例数异常告警监控配置变更频率3.3.3 容量规划根据服务实例数量规划 Nacos 集群规模定期清理无效的服务实例监控配置数量避免配置过多影响性能3.4 故障排查指南3.4.1 常见问题服务注册失败检查网络连通性验证 Nacos Server 状态检查命名空间和分组配置配置无法刷新确认RefreshScope注解已添加检查配置 Data ID 和 Group 是否正确验证应用是否有读取配置的权限服务发现异常检查健康检查配置验证负载均衡策略查看服务实例元数据3.4.2 日志分析Nacos Server 日志logs/nacos.log客户端日志设置logging.level.com.alibaba.cloud.nacosDEBUG网关日志启用 Gateway 的详细日志3.5 性能优化建议客户端配置优化spring: cloud: nacos: discovery: # 心跳间隔默认5秒 heart-beat-interval: 5000 # 心跳超时默认15秒 heart-beat-timeout: 15000 # 实例刷新间隔默认30秒 instance-poll-interval: 30000服务端优化根据实例数量调整 JVM 参数使用 MySQL 替代内嵌数据库配置合适的集群节点数量3.6 扩展功能3.6.1 配置监听NacosConfigListener(dataId nacos-config-service-dev.yaml) public void onMessage(String config) { log.info(Config changed: {}, config); // 处理配置变更逻辑 }3.6.2 服务事件监听Component public class ServiceChangeListener { EventListener public void onInstanceChange(NamingEvent event) { log.info(Service {} instances changed: {}, event.getServiceName(), event.getInstances()); } }3.7 迁移建议从传统的 Spring Cloud Netflix 组件迁移到 NacosEureka → Nacos Discovery无缝迁移只需更改依赖和配置Config Bus → Nacos Config简化架构减少组件依赖Ribbon → Nacos LoadBalancer更灵活的服务路由3.8 最后Nacos 作为云原生时代的服务基础设施正在持续演进更好的 Kubernetes 集成更强大的服务治理能力更完善的可观测性支持多语言 SDK 的持续丰富通过 Spring Cloud Alibaba Nacos 的集成可以获得一个功能完整、性能优异、易于运维的微服务基础设施平台大大降低了微服务架构的复杂度和维护成本。谢谢你看我的文章既然看到这里了如果觉得不错随手点个赞、转发、在看三连吧感谢感谢。那我们下次再见。您的一键三连是我更新的最大动力谢谢山水有相逢来日皆可期谢谢阅读我们再会我手中的金箍棒上能通天下能探海