一、Nacos 下载与初始化配置

本文使用1.2.0,下载地址:https://github.com/alibaba/nacos/releases

Nacos 单机模式支持持久化配置到 MySQL 数据库,修改 conf/application.properties 配置:

spring.datasource.platform=mysql

db.num=1

db.url.0=jdbc:mysql://数据库地址:端口/数据库名?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=用户名
db.password=密码

然后在数据库执行 conf/nacos-mysql.sql 初始化数据库

Windows 环境下双击 bin/startup.cmd 启动 Nacos

二、Nacos 配置及在 Spring Cloud 中的使用

Nacos 可以根据三种模式区分不同环境下的配置:

  1. 根据 Data Id 和 profiles (spring.profiles.active)
  2. 根据 Group
  3. 根据 Namespace (命名空间)

本人认为根据 Namespace 区分不同环境下的配置应该更适用于实际项目,故本文以 Namespace 模式为例

2.1 新建命名空间

新增名字为 dev 的命名空间

2.2 新增配置

在 dev 命名空间新增配置,Data Id 为 service1.yml,Group 为 service1,配置格式为 YAML,内容为:

server:
port: 8070

2.3 在 Spring Cloud 中使用

添加依赖:

    <dependencies>
... <dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency> ...
</dependencies> <dependencyManagement>
<dependencies>
... <dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency> ...
</dependencies>
</dependencyManagement>

spring-cloud-alibaba-dependencies 是 Spring Cloud Alibaba BOM,包含了 Spring Cloud Alibaba 的所有依赖的版本,使用 Nacos, Sentinel 等时不用再指定版本

新建 Spring Cloud 项目,在 bootstrap.yml 新增配置:

spring:
application:
name: service1 cloud:
nacos:
config:
server-addr: 192.168.92.1:8848 # Nacos 地址与端口
namespace: e5fc372c-ad66-4e0e-a353-a217d0a315ba # 命名空间ID
group: service1
file-extension: yml

启动项目,可以看到项目使用的端口为在 Nacos 中配置的 8070 说明配置生效

2.4 扩展配置

有时我们想要将配置放置在不同的文件或者多个项目共用部分配置,则可以添加扩展配置

2.4.1 在 Nacos 添加配置

在 dev 命名空间新增配置,Data Id 为 greeting.yml,Group 为 common,配置格式为 YAML,内容为:

greeting: Hello, world!

在 dev 命名空间新增配置,Data Id 为 author.yml,Group 为 common,配置格式为 YAML,内容为:

author: karonda

2.4.2 Spring Cloud 配置

方法一:

spring:
application:
name: service1 cloud:
nacos:
config:
server-addr: 192.168.92.1:8848
namespace: e5fc372c-ad66-4e0e-a353-a217d0a315ba
group: service1
file-extension: yml
extension-configs[0]:
data-id: greeting.yml
group: common
refresh: true # 配置修改后是否自动更新
extension-configs[1]:
data-id: author.yml
group: common
refresh: true

方法二:

spring:
application:
name: service1 cloud:
nacos:
config:
server-addr: 192.168.92.1:8848
namespace: e5fc372c-ad66-4e0e-a353-a217d0a315ba
group: service1
file-extension: yml
extension-configs:
- data-id: greeting.yml
group: common
refresh: true
- data-id: author.yml
group: common
refresh: true

2.4.3 测试

@RefreshScope # 配置自动更新
@RestController
public class GreetingController { @Value("${greeting:}")
private String greetingStr;
@Value("${author:}")
private String authorStr; @RequestMapping("/greeting")
public String greeting() {
return greetingStr + " from " + authorStr;
}
}

访问 http://localhost:8070/greeting

参考:

  1. Nacos支持三种部署模式
  2. Nacos学习笔记(五)—— NacosConfig配置
​

Spring Cloud Alibaba 初体验(一) Nacos 配置中心的更多相关文章

  1. Spring Cloud Alibaba 初体验(二) Nacos 服务注册与发现 + 集成 Spring Cloud Gateway

    一.服务注册 添加依赖: <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>s ...

  2. Spring Cloud Alibaba 初体验(三) Nacos 与 Dubbo 集成

    一.新建项目 新建项目,只放置接口,用于暴露 Dubbo 服务接口 public interface GreetingService { String greeting(); } 二.provider ...

  3. Spring Cloud Alibaba基础教程:Nacos配置的多环境管理

    前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ...

  4. Spring Cloud Alibaba基础教程:Nacos配置的多文件加载与共享配置

    前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ...

  5. Spring Cloud Config、Apollo、Nacos配置中心选型及对比

    Spring Cloud Config.Apollo.Nacos配置中心选型及对比 1.Nacos 1.1 Nacos主要提供以下四大功能 2.Spring Cloud Config 3.Apollo ...

  6. Spring Cloud Alibaba基础教程:Nacos配置的加载规则详解

    前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式(Res ...

  7. Spring Cloud Alibaba 初体验(六) Seata 及结合 MyBatis 与 MyBatis-Plus 的使用

    一.下载与运行 本文使用 Seata 1.1.0:https://github.com/seata/seata/releases Windows 环境下双击 bin/seata-server.bat ...

  8. Spring Cloud Alibaba 初体验(四) Sentinel

    一.Sentinel 下载与运行 本文使用 Sentinel 1.7.1:https://github.com/alibaba/Sentinel/releases 使用自定义端口 8089 运行 Se ...

  9. Spring Cloud Alibaba 初体验(五) SkyWalking

    一.下载与运行 本文使用 SkyWalking 7.0.0:https://www.apache.org/dyn/closer.cgi/skywalking/7.0.0/apache-skywalki ...

随机推荐

  1. Java集合(类)框架(一)

    集合类均在java.util包之下 集合类方法的功能基本为增.删.改.查,部分外加方法除外(如toArray().toString()等) 1.List接口 底层为Object 数组,存放的数据可以重 ...

  2. node.js+express框架 修改后自启【不需要再执行start】

    我们每次修改完后都需要重新启动下才能刷新,就很麻烦'nodemon'解决了这个问题. 这里直接进行全局安装 npm install -g nodemon 安装到本地 npm install nodem ...

  3. 错误解析:org.apache.catalina.LifecycleException: Protocol handler start failed

    以下是报错代码: org.apache.catalina.LifecycleException: Protocol handler start failed at org.apache.catalin ...

  4. linux修改ssh远程端口22

    建议先查看redhat的release版本,CentOS 7的启动服务不同: # more /etc/redhat-release 正文: 如题: 在此前,建议先查看redhat的release版本, ...

  5. 833. Find And Replace in String —— weekly contest 84

    Find And Replace in String To some string S, we will perform some replacement operations that replac ...

  6. 【Kata Daily 191010】Grasshopper - Summation(加总)

    题目: Summation Write a program that finds the summation of every number from 1 to num. The number wil ...

  7. markdown语法入门笔记

    Markdown 是一种轻量级标记语言 1.标题 # ## ... ###### 分别为1到6级标题 (#后要加空格) 7个以上的#的没有效果 阿萨德阿萨德 阿萨德 2.字体 *斜体文本* _斜体文本 ...

  8. 《Machine Learning in Action》—— 剖析支持向量机,优化SMO

    <Machine Learning in Action>-- 剖析支持向量机,优化SMO 薄雾浓云愁永昼,瑞脑销金兽. 愁的很,上次不是更新了一篇关于支持向量机的文章嘛,<Machi ...

  9. Failed connect to mirrors.cloud.aliyuncs.com:80

    在yum insatall 安装是报错 Failed connect to mirrors.cloud.aliyuncs.com:80; Connection refused 解决方法: cd /et ...

  10. Proftp最简匿名访问配置

    前言 每一次做ftp的配置都要弄半天,找文档,各种权限控制的坑,折腾半天,这次还是准备记录下来,以备不时之需,这里不配置什么高级的功能,就去实现一个最简单的配置 匿名用户的上传和下载 配置proftp ...