SpringCloud项目使用nacos配置
SpringCloud项目
pom.xml
注意SpringCloud和SpringCloudAlibaba的版本对应。
Spring Cloud Alibaba Version | Spring Cloud Version | Spring Boot Version |
---|---|---|
2022.0.0.0* | Spring Cloud 2022.0.0 | 3.0.2 |
2022.0.0.0-RC2 | Spring Cloud 2022.0.0 | 3.0.2 |
2022.0.0.0-RC1 | Spring Cloud 2022.0.0 | 3.0.0 |
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zjw</groupId>
<artifactId>sc-03-provider-config-8081</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sc-03-provider-config-8081</name>
<description>sc-03-provider-config-8081</description>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2022.0.0</spring-cloud.version>
<spring-cloud-alibaba.version>2022.0.0.0</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</exclusion>
</exclusions>
</dependency>
<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>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<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>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<builder>paketobuildpacks/builder-jammy-base:latest</builder>
</image>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.yaml
server-addr: nacos-local:8848
这里nacos-local
我的电脑修改了hosts文件,指向了本机127.0.0.1地址
spring:
application:
name: depart-provider
profiles:
active: dev
config:
import:
- optional:nacos:${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
cloud:
nacos:
server-addr: nacos-local:8848
config:
file-extension: yaml
username: nacos
password: nacos
nacos添加配置
depart-provider-dev.yaml
server:
port: 8081
spring:
datasource:
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/sc?serverTimezone=Asia/Shanghai&useSSL=false&characterEncoding=utf-8&rewriteBatchedStatements=true
application:
name: depart-provider # 微服务名称
config:
import:
- optional:nacos:${spring.application.name}.${spring.cloud.nacos.config.file-extension}
cloud:
nacos:
config:
file-extension: yaml
server-addr: nacos-local:8848 # nacos注册中心地址
username: nacos # 用户名密码
password: nacos
discovery:
server-addr: nacos-local:8848 # nacos注册中心地址
username: nacos # 用户名密码
password: nacos
mybatis-plus:
# 别名包扫描路径
type-aliases-package: com.zjw.domain
global-config:
db-config:
# 设置id字段为自增长
id-type: auto
logic-delete-field: deleted #逻辑删除的字段
logic-delete-value: 1 # 已经逻辑删除的记录该字段值
logic-not-delete-value: 0 # 未被逻辑删除的记录该字段值
configuration:
# mybatis二级缓存,默认为true,开启
cache-enabled: false
# 是否开启自动驼峰命名规则,默认为true,开启
map-underscore-to-camel-case: true
# 设置控制台日志打印,默认不显示SQL语句
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 枚举字段加上了@EnumValue注解后默认使用的是MybatisEnumTypeHandler,否则默认为mybatis EnumTypeHandler
default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler
# Mapper 所对应的 XML 文件位置,默认为“classpath*:/mapper/**/*.xml”
mapper-locations:
- classpath*:/mapper/**/*.xml
启动SpringCloud项目即可使用nacos中的配置文件。
nacos不同版本配置可能不能,谨慎对待!!!
---------------
我每一次回头,都感觉自己不够努力,所以我不再回头。
---------------