SpringCloudAlibaba-入门操作

    技术2026-04-09  5

    本文主要是预研Spring Cloud Alibaba技术。主要有Nacos、Disconvery、Config、Sentiel。

    参考资料 SpringCloud官网Nacos中文文档SpringCloudGateway 技术框架 SpringBoot-2.1.5.RELEASESpringCloud-Greenwich.SR3SpringCloudAlibaba-2.1.0SpringCloudGateway-2.1.3 源码地址:https://github.com/CNXMBuyu/springclouddemo/tree/alibaba

    Nacos

    Nacos是阿里巴巴开源的一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。Nacos是单独的一个应用程序。

    点击查看使用手册

    下载地址

    地址:GIT官网,下载偏慢;百度网盘,提取码: q75e解压: unzip nacos-server- v e r s i o n . z i p 或 者 t a r − x v f n a c o s − s e r v e r − version.zip 或者 tar -xvf nacos-server- version.ziptarxvfnacosserverversion.tar.gz单例启动:sh nacos/bin/startup.sh -m standalone

    源码打包

    GIT地址:https://github.com/alibaba/nacos.git分支选择:选择tag-1.3.0maven打包命令:mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U解压:distribution/target单例启动:sh nacos/bin/startup.sh -m standalone

    默认用户名/密码:nacos/nacos

    集群搭建

    配置

    复制cluster.conf.example,改为cluster.conf。集群配置如下:

    #it is ip 172.16.85.223:8846 172.16.85.223:8847 172.16.85.223:8848

    启动命令

    sh startup.sh -p embedded

    如果不加属性-p embedded,则默认使用mysql作为持久化数据库

    Discovery - 服务发现

    官方帮助手册官方示例Nacos中服务管理使用说明

    使用步骤

    // 1. pom.xml 添加依赖 <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> // 2. application.properties添加nacos地址,其它配置参考配置列表 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 // 3. 添加注解 @SpringBootApplication @EnableDiscoveryClient public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }

    配置列表

    点击查看参数说明

    配置说明

    # 服务发现配置,参考:https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html#_more_information_about_nacos_discovery_starter_configurations ## 服务器地址 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 ## 服务名称,默认值:spring.application.name #spring.cloud.nacos.discovery.service=provider ## 权重,取值范围 1 到 100,数值越大,权重越大。默认:1 #spring.cloud.nacos.discovery.weight=80 ## 网卡名称,当多网卡时,注意选择。默认:取第一张网卡 #spring.cloud.nacos.discovery.network-interface= ## IP地址,如果没有配置,默认根据网卡选择IP #spring.cloud.nacos.discovery.ip ## 端口,默认值:server.port #spring.cloud.nacos.discovery.port ## 一个典型的场景是隔离针对不同环境的服务注册,例如测试和生产环境之间的资源(配置、服务等)隔离 #spring.cloud.nacos.discovery.namespace=dev ## 阿里云的账号 #spring.cloud.nacos.discovery.access-key=access ## 阿里云的密码 #spring.cloud.nacos.discovery.secret-key=secret ## 用于存储信息 #spring.cloud.nacos.discovery.metadata.username=user #spring.cloud.nacos.discovery.metadata.password=password ## 日志文件名称 #spring.cloud.nacos.discovery.log-name= ## 集群名称,默认值:DEFAULT #spring.cloud.nacos.discovery.cluster-name= ## 地域的某个服务的入口域名,通过此域名可以动态地拿到服务端地址 #spring.cloud.nacos.discovery.endpoint= ## 集成ribbon,默认是true #ribbon.nacos.enabled=true ## #spring.cloud.nacos.discovery.watch.enabled=true

    特性/原理

    默认集成了Ribbon,可以使用RestTemplate和FeignClient

    服务注册

    Discovery遵循了spring cloud common标准,实现了AutoServiceRegistration、ServiceRegistry、Registration 这三个接口。

    在spring cloud应用的启动阶段,监听了WebServerInitializedEvent事件,当Web容器初始化完成后,即收到WebServerInitializedEvent事件后,会触发注册的动作,调用ServiceRegistry的register方法,将服务注册到Nacos Server。

    服务发现

    NacosServerList实现了com.netflix.loadbalancer.ServerList接口,并在@ConditionOnMissingBean的条件下进行自动注入,默认集成了Ribbon。

    如果需要有更加自定义的可以使用 @Autowired注入一个NacosRegistration实例,通过其持有的NamingService字段内容直接调用 Nacos API。

    信息查询

    http://localhost:8001/actuator/nacos-discovery

    Config - 配置中心

    SpingCloudAlibaba的config没有服务端,直接在nacos的配置管理中添加配置即可。

    官方帮助手册官方示例Nacos中配置管理使用说明

    使用步骤

    // 1. pom添加依赖 <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> // 2. 添加bootstrap.properties配置,其它配置参考配置列表 spring.cloud.nacos.config.server-addr=127.0.0.1:8848

    配置列表

    点击查看参数说明

    配置说明

    # config 配置 ## config服务端地址 spring.cloud.nacos.config.server-addr=127.0.0.1:8848 ## 配置文件格式:${prefix}-${spring.profiles.active}.${file-extension}。例如:provider-dev.properties spring.cloud.nacos.config.prefix=provider ## 配置的编码,默认UTF-8 #spring.cloud.nacos.config.encode ## 默认DEFAULT_GROUP #spring.cloud.nacos.config.group ## 默认类型:properties #spring.cloud.nacos.config.fileExtension=properties ## 获取配置的超时时间,默认3000ms #spring.cloud.nacos.config.timeout=3000 ## 通过此域名可以动态地拿到服务端地址 #spring.cloud.nacos.config.endpoint ## 一个典型的场景是隔离针对不同环境的服务注册,例如测试和生产环境之间的资源(配置、服务等)隔离 #spring.cloud.nacos.config.namespace ## 阿里云的账号 #spring.cloud.nacos.config.accessKey ## 阿里云的密码 #spring.cloud.nacos.config.secretKey ## 服务端 API 的相对路径 #spring.cloud.nacos.config.contextPath ## 集群名称 #spring.cloud.nacos.config.clusterName ## #spring.cloud.nacos.config.sharedDataids ## #spring.cloud.nacos.config.refreshableDataids ## #spring.cloud.nacos.config.extConfig

    特性/原理

    Nacos Config数据结构

    Nacos Config主要通过dataId和group来唯一确定一条配置,我们假定你已经了解此背景。

    Nacos Client从Nacos Server端获取数据时,调用的是此接口ConfigService.getConfig(String dataId, String group, long timeoutMs)。

    自动注入

    Nacos Config Starter实现了org.springframework.cloud.bootstrap.config.PropertySourceLocator接口,并将优先级设置成了最高。

    在Spring Cloud应用启动阶段,会主动从Nacos Server端获取对应的数据,并将获取到的数据转换成PropertySource且注入到Environment的PropertySources 属性中,所以使用 @Value 注解也能直接获取Nacos Server端配置的内容。

    动态刷新

    Nacos Config Starter默认为所有获取数据成功的Nacos的配置项添加了监听功能,在监听到服务端配置发生变化时会实时触发org.springframework.cloud.context.refresh.ContextRefresher的refresh方法 。

    如果需要对Bean进行动态刷新,请参照Spring和Spring Cloud规范。推荐给类添加@RefreshScope或@ConfigurationProperties 注解,更多详情请参考 ContextRefresher Java Doc。

    信息查询

    http://127.0.0.1:8001/actuator/nacos-config

    Gateway - 网关 - 预计20200706更新

    Gateway并不属于SpringCloudAlibaba的一个组件。

    https://github.com/spring-cloud/spring-cloud-gateway/blob/v2.1.3.RELEASE/spring-cloud-gateway-sample/pom.xml

    Sentinel - 熔断器+ - 预计20200706更新

    Sentinel是阿里巴巴开源的分布式系统的流量防卫组件,Sentinel把流量作为切入点,从流量控制,熔断降级,系统负载保护等多个维度保护服务的稳定性。

    example:https://github.com/alibaba/spring-cloud-alibaba/tree/v2.1.0.RELEASE/spring-cloud-alibaba-examples/sentinel-example

    Dubbo

    略过

    RocketMQ

    产品介绍

    ANS(Application Naming Service)

    阿里云EDAS的组件,提供商业版的服务注册功能。EDAS介绍

    ACM(Application Configuration Management)

    阿里云的应用配置管理。ACM介绍

    OSS(Object Storage Service)

    阿里云的对象存储。OSS介绍

    SchedulerX

    阿里云EDAS的组件,提供符合Spring Cloud规范的分布式作业调度。EDAS介绍

    SMS(Short Message Service)

    阿里云短信服务

    Processed: 0.008, SQL: 9