部署
一 准备
1 依赖:MYSQL
2 创建数据库
CREATE database if NOT EXISTS `nacos` default character set utf8mb4 collate utf8mb4_unicode_ci;
3 导入初始化SQL
4 执行创建NACOS管理员账号SQL(初始账号密码:nacos/nacos)
INSERT INTO users (username, password, enabled) VALUES ('nacos', '$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu', TRUE);
INSERT INTO roles (username, role) VALUES ('nacos', 'ROLE_ADMIN');
5 执行创建MYSQL操作员SQL
CREATE USER 'nacos'@'%' IDENTIFIED BY 'nacos';
GRANT ALL PRIVILEGES ON nacos.* TO 'nacos'@'%';
FLUSH PRIVILEGES;
二 DOCKER部署
sudo docker run -d \
-p 8848:8848 \
-p 9848:9848 \
-e PREFER_HOST_MODE=hostname \
-e MODE=standalone \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=mysql \
-e MYSQL_SERVICE_DB_NAME=nacos \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_USER=nacos \
-e MYSQL_SERVICE_PASSWORD=nacos \
-e "MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true" \
-e NACOS_AUTH_ENABLE=true \
-e NACOS_AUTH_IDENTITY_KEY=nacos \
-e NACOS_AUTH_IDENTITY_VALUE=nacos \
-e NACOS_AUTH_TOKEN=SecretKey012345678901234567890123456789012345678901234567890123456789 \
-v /Users/wuzijing/Documents/middle/nacos/log:/home/nacos/logs \
--name nacos \
--privileged=true \
--restart=always \
nacos/nacos-server:v2.3.0
代码
一 JAVA配置
1 YAML
server:
port: 8904
spring:
application:
name: report
cloud:
nacos:
config:
server-addr: ip:8848
group: DEFAULT_GROUP
username: nacos
password: nacos
config:
import[0]: optional:nacos:/${spring.application.name}.yaml
2 POM
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2023.0.1.2</version>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId>
<version>2.1.0-RC</version>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-resolver-dns-native-macos</artifactId>
<classifier>osx-aarch_64</classifier>
</dependency>
3 启动类注解
import com.alibaba.nacos.spring.context.annotation.EnableNacos;
import com.alibaba.nacos.api.annotation.NacosProperties;
@EnableNacos(
globalProperties = @NacosProperties(
serverAddr = "${spring.cloud.nacos.config.server-addr}",
username = "${spring.cloud.nacos.config.username}",
password = "${spring.cloud.nacos.config.password}"
)
)