springcloud

一、Nacos 介绍

Nacos ​ = Na ming + Co nfiguration + Service

阿里巴巴开源的注册中心配置中心组件

🎯 核心功能

  • 服务发现与注册​ (Naming Service)

  • 动态配置管理​ (Configuration Service)

  • 服务健康监测

  • 动态DNS服务

📦 依赖坐标

复制代码
<!-- 服务注册与发现 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<!-- 配置中心 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

二、安装与启动

1. 安装步骤

复制代码
# 1. 上传并解压
cd /usr/upload
tar -zxvf nacos-server-1.4.1.tar.gz -C /usr/local

# 2. 启动(单机模式)
cd /usr/local/nacos/bin
./startup.sh -m standalone

# 3. 关闭
./shutdown.sh

2. 访问测试

  • 地址:http://192.168.61.132:8848/nacos

  • 账号:nacos/nacos


三、Nacos 作为注册中心

🔧 服务提供者 (Provider)

application.yml

复制代码
spring:
  application:
    name: nacos-provider
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.61.132:8848

启动类

复制代码
@SpringBootApplication
@EnableDiscoveryClient  // 开启服务发现
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}

🔧 服务消费者 (Consumer)

Controller示例

复制代码
@RestController
public class ConsumerController {
    
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate;
    
    @RequestMapping("/getUserById/{id}")
    public User getUserById(@PathVariable Integer id) {
        // 1. 获取服务实例
        List<ServiceInstance> instances = 
            discoveryClient.getInstances("nacos-provider");
        ServiceInstance instance = instances.get(0);
        
        // 2. 拼接URL
        String url = String.format("http://%s:%s/provider/getUserById/%s",
            instance.getHost(),
            instance.getPort(),
            id);
        
        // 3. 调用服务
        return restTemplate.getForObject(url, User.class);
    }
}

四、Nacos 作为配置中心

1. 基础配置

bootstrap.yml​ (优先级高于application.yml)

复制代码
spring:
  cloud:
    nacos:
      config:
        server-addr: 192.168.61.132:8848
        prefix: nacos-config      # 配置前缀
        file-extension: yaml      # 配置文件后缀
  application:
    name: nacos-config-service   # 服务名称

2. 配置管理规则

复制代码
Data Id 命名规则:
${prefix}-${spring.profiles.active}.${file-extension}
或
${prefix}.${file-extension}(无profile时)

示例:nacos-config-dev.yaml

3. 动态刷新

复制代码
@RestController
@RefreshScope  // 支持配置动态刷新
public class ConfigController {
    
    @Value("${config.info}")
    private String configInfo;
    
    @GetMapping("/config")
    public String getConfig() {
        return configInfo;
    }
}

🎯 核心概念补充

1. 命名空间 (Namespace)

  • 用于多环境/多租户隔离

  • 默认:public

2. 配置分组 (Group)

  • 默认:DEFAULT_GROUP

  • 可对不同服务/不同模块进行分组

3. 配置优先级

复制代码
bootstrap.yml > 远程配置 > application.yml

4. 配置示例(Nacos控制台)

复制代码
# Data Id: nacos-config.yaml
server:
  port: 8081

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456

custom:
  config: hello-nacos

💡 最佳实践建议

  1. 多环境配置

    复制代码
    # 开发环境
    spring:
      profiles: dev
    
    # 生产环境  
    spring:
      profiles: prod
  2. 共享配置

    • 使用shared-configsextension-configs共享通用配置
  3. 服务治理

    • 结合Ribbon/OpenFeign实现负载均衡

    • 设置服务权重、元数据等

  4. 健康检查

    • 支持TCP/HTTP/MySQL健康检查

    • 自动剔除不健康实例

相关推荐
恼书:-(空寄1 小时前
Spring AOP底层是怎么织入的?——ProxyFactory、JDK Proxy vs CGLIB与编译期织入
spring·动态代理
IT_陈寒1 小时前
Redis的KEYS命令把我搞崩溃了,改用SCAN才活过来
前端·人工智能·后端
长不胖的路人甲2 小时前
SpringCloud 服务雪崩、熔断、降级
后端·spring·spring cloud
爱折腾的小黑牛3 小时前
小程序多人协作的权限设计:三级权限的实现
后端
蓝银草同学3 小时前
Stream 实战:博客列表排序、过滤与分页(AI 辅助学习 Java 8)
java·前端·后端
小小猪的春天4 小时前
AI 编程 30 天实验复盘:效率提升 65%,线上 bug 涨到 11 个,问题出在哪
后端
爱勇宝4 小时前
3位工程师靠“删AI代码”创业,一周收费1万美元:以后最贵的能力,可能不是写代码
前端·后端·架构
不能放弃治疗5 小时前
rule 和 skill
后端
ADIT5 小时前
一、反射的定义与原理
后端