nacos单机部署
单机server端部署
我本地下载的是2.0.4版本
<revision>2.0.4</revision>
与springcloud结合
nacos | Spring Cloud Alibaba Version | Spring Cloud Version | Spring Boot Version |
---|---|---|---|
2.0.4 | 2.2.7.RELEASE* | Spring Cloud Hoxton.SR12 | 2.3.12.RELEASE |
版本匹配后,着手开发demo。
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${com.alibaba.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<spring-boot-dependencies.version>2.3.12.RELEASE</spring-boot-dependencies.version>
<spring.cloud-version>Hoxton.SR12</spring.cloud-version>
<com.alibaba.cloud.version>2.2.7.RELEASE</com.alibaba.cloud.version>
spring-cloud-alibaba-dependencies是引入了
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.client.version}</version>
</dependency>
<version>2.0.3</version>
说明我当前采用的是nacos-client的2.x版本
我搭建了一个user-service服务,作为demo工程
添加配置,链接本地的nacos
spring:
application:
name: user-service
cloud:
nacos:
config:
file-extension: yml
server-addr: 127.0.0.1:8848
#client共享配置
shared-configs[0]:
data-id: whyshare-config-share.yml
refresh: true
discovery:
server-addr: 127.0.0.1:8848
server:
port: 1994
servlet:
context-path: /userService
在nacos上创建配置
zlt:
datasource:
ip: 127.0.0.1
username: username
password: password
spring:
datasource:
url: jdbc:mysql://${zlt.datasource.ip}:3306/whyshare?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
username: ${zlt.datasource.username}
password: ${zlt.datasource.password}
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
id-type: auto
测试获取数据库配置:
成功获取到用户数据,证明配置中心生效。
添加配置,尝试热加载
2022-02-28 23:24:12.235 INFO 7282 --- [nio-1994-exec-8] c.h.w.u.controller.ConfigTestController : {"company":"fintech","interest":"sing"}
测试发现,默认支持热加载的。从配置参数也能看到。下篇文章介绍