SpringCloud Alibaba场景实践(Nacos篇)

一、基于SpringCloud Alibaba的微服务电商解决方案

1.1、什么是SpringCloud Alibaba?

一系列框架的有序的集合

1.2、电商项目的三高

高并发、高新能、高可用

1.3、SpringCloud Alibaba电商微服务 架构


image.png

AKF扩展拆分


image.png

y轴


image.png

x轴


image.png

z轴


image.png

1.4 微服务工程搭建


image.png

搭建结构

版本选择


版本匹配

1.5 Nacos极简入门

1.5.1 介绍

帮助我们发现、配置、管理微服务(alibaba实现的服务注册中心)

1.5.2 使用场景


老项目使用


最新用法

1.5.3 使用

下载地址:https://github.com/alibaba/nacos/releases

启动:

访问:

1.5.4 Nacos与项目整合

启动类上加@EnableDiscoveryClient服务发现注解,在RestTemplate上加@LoadBalanced注解,实现动态路由功能,最后在yml文件中配置nacos地址

pom.xml

复制代码
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

String productName = restTemplate.getForObject("http://product-server/product/createProduct/" + productId, String.class);

二、Nacos Config配置中心

2.1 常见的配置中心

  • Appllo
  • Disconf
  • SpringCloud Config
  • Nacos

2.2 为什么使用配置中心

  • 配置文件分散在各个项目里,不方便维护
  • 配置内容安全与权限
  • 更新配置后,项目需要重启

2.3 配置中心得作用

系统配置的集中管理(编辑、存储、分发)、动态更新配置不重启、回滚配置(变更管理、历史版本管理、变更审计)等所有与配置相关的活动

2.4 不同环境的配置与使用

复制代码
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

spring:
  application:
    name: configcenter
  cloud:
    nacos:
      config:
        server-addr: ***********:8848
        file-extension: yaml
  profiles:
    active: dev
server:
  port: 7000

配置中心地址:http://47.93.249.168:8848/nacos

nacos配置中心


配置中心详细配置

复制代码
@RestController
@RefreshScope  //动态刷新nacos配置
@RequestMapping("/config")
public class ConfigController {
    @Value("${testStr}")
    private String testStr;
    @Value("${testCommon}")
    private String testCommon;

    @GetMapping("/test")
    public String hello() {
        return testStr + "," +"testCommon";
    }
}
思考:在nacos中配置了四个文件,configcenter-test.yaml,configcenter-dev.yaml,configcenter-prod.yaml,configcenter.yaml
如果同一个配置在四个文件中都存在且值不同,最终项目读取的是那个?

最后会读取configcenter.yaml

结论:如果配置了spring.profile.active则优先获取configcenter-${spring.profile.active}.yaml中的值
总结:
  • 在bootstrap.yaml文件中如果配置了spring.profile.active,在nacos多个配置中,不重合的值都会读取,重合的以active为主。没配置就以configcenter.yaml为主
  • nacos中Data ID(configcenter-dev)的取值要与bootstrap.yaml中 spring.application.name(configcenter)和spring.profiles.active(dev)的值一致

2.5 不同微服务的配置和使用

1、shard-configs
复制代码
spring:
  application:
    name: configcenter
  cloud:
    nacos:
      config:
        server-addr: **********:8848
        file-extension: yaml
        shared-configs[0]:
          dataId: mq.yaml          # 对应的nacos的Data Id
          refresh: true               # 是否刷新
        shared-configs[1]:
          dataId: redis.yaml
          refresh: true

/**
     * 不同微服务的nacos配置
     */
    @Value("${redisStr}")
    private String redisStr;
    @Value("${mqStr}")
    private String mqStr;
    @Value("${test}")
    private String test;

    @GetMapping("/test")
    public String hello() {
        return testStr + "," +testCommon;
    }

    @GetMapping("/test2")
    public String hello2() {
        return redisStr +","+ mqStr;
    }

    @GetMapping("/test3")
    public String hello3() {
        return test;
    }


nacos配置中心

思考:如果shard-configs的文件存在相同的配置,最终会以哪个配置文件中的值为主

会以后配置的配置文件中的值为主

2、extension-configs

extension-configs读取顺序和shard-config配置和读取顺序一样,后配置的为主

复制代码
spring:
  application:
    name: configcenter
  cloud:
    nacos:
      config:
        server-addr: **********:8848
        file-extension: yaml
        extension-configs[0]:
          dataId: redis.yaml          # 对应的nacos的Data Id
          refresh: true               # 是否刷新
        extension-configs[1]:
          dataId: mq.yaml
          refresh: true

总结

当shared-configs和extension-configs存在相同的配置,则以extension-config中的配置为主

三、Nacos Config动态刷新机制

3.1 原理:


image.png
最后编辑于:2025-06-05 21:07:25
© 著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务

喜欢的朋友记得点赞、收藏、关注哦!!!

相关推荐
Decay__3 小时前
05_MySQL 常用 SQL 大全 —— 诊断、管理、优化辅助 SQL 与三大系统库速查
运维·数据库·sql·mysql·架构
贞明电子5 小时前
工业三色报警灯怎么接入项目系统?开关量与RS485控制应用指南
运维·网络·微信公众平台·贞明电子
学长毕设6 小时前
基于SpringBoot的实验室资产管理系统
java·spring boot·后端
万联WANFLOW6 小时前
海外网络故障怎么排查?Cloudflare 三季度断网报告解读与 mtr 实战
运维·网络·故障分析
zzu123zsw7 小时前
从臃肿的模板调用到声明式契约:深入 UniHttp 动态代理与多渠道生命周期扩展
java·spring boot·microsoft·unihttp
Ruiery8 小时前
Linux 6.6内核内存管理深度解析(十三):slab/slub 分配器
linux·运维·服务器
源码集结号8 小时前
云诊所门诊系统源码,基于SpringBoot+Vue.js架构,支持多租户设计与Docker容器化部署
java·spring boot·源码·医院信息系统·门诊系统·诊所系统
论文复现现场8 小时前
WRF、FEA 能用 L40S 加速吗?求解器检查、断点恢复与长期运行
java·数据库·算法·wrf·有限元分析·gpu计算·断点续算
啃瓜子的松鼠9 小时前
泛微OAe9-后端二开异步调用
java
Decay__9 小时前
04_MySQL DBA 日常运维手册 —— 会话管理、锁与阻塞、事务监控、慢查询与日志巡检
运维·数据库·sql·mysql·架构