【分布式注册中心】NACOS_2.3.0部署与实战

部署

一 准备

1 依赖:MYSQL

2 创建数据库

CREATE database if NOT EXISTS `nacos` default character set utf8mb4 collate utf8mb4_unicode_ci;

3 导入初始化SQL

https://raw.githubusercontent.com/alibaba/nacos/develop/distribution/conf/mysql-schema.sqlicon-default.png?t=N7T8https://raw.githubusercontent.com/alibaba/nacos/develop/distribution/conf/mysql-schema.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


  com.alibaba.cloud
  spring-cloud-starter-alibaba-nacos-config
  2023.0.1.2
  
    
      com.alibaba.nacos
      nacos-client
    
  


  com.alibaba.nacos
  nacos-spring-context
  2.1.0-RC
  
    
      com.alibaba.nacos
      nacos-client
    
  


  com.alibaba.nacos
  nacos-client
  2.3.0


  io.netty
  netty-resolver-dns-native-macos
  osx-aarch_64

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}"
    )
)

你可能感兴趣的:(后端开发,分布式,java,spring,boot)