Docker - MySql - Nacos - 单机(非鉴权/鉴权)

1、准备docker 容器内相互访问的网络

创建容器内部公用网络

docker network create common-network

docker network 命令备查

# 连接容器到网络
docker network connect 网络名 

# 创建网络
docker network create  网络名 

# 断开网络
docker network disconnect  网络名

# 查看网络具体信息
docker network inspect  

# 查看所有网络列表
docker network ls   

# 删除无用网络
docker network prune  

# 删除指定的网络
docker network rm 网络ID

2、准备mysql

2.1、docker拉取mysql镜像

docker pull mysql

2.2、启动mysql

docker run -d \
--name mysql \
--restart=always \
--network common-network \
-v /etc/docker/mysql/conf:/etc/mysql/conf.d \
-v /etc/docker/mysql/logs:/var/log/mysql \
-v /etc/docker/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=你的密码 \
-e MYSQL_ROOT_HOST=% \
--privileged=true \
-p 3306:3306 \
mysql:latest \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_general_ci \
--lower_case_table_names=1 \
--skip-name-resolve=1 \
--max_connections=1000 \
--wait_timeout=31536000 \
--interactive_timeout=31536000 \
--default-time-zone='+8:00'

2.2、准备MySql 数据库脚本

数据库脚本:下载地址

2.3、为nacos创建mysql用户名、密码

创建用户

CREATE USER 'nacos'@'%' IDENTIFIED BY 'nacos';

授予用户对 nacos_config 数据库的所有权限

GRANT ALL PRIVILEGES ON nacos_config.* TO 'nacos'@'%';

刷新权限

FLUSH PRIVILEGES;

3、准备nacos

3.1、docker拉取nacos镜像

docker pull nacos/nacos-server

3.2、单机模式启动nacos(非鉴权)

docker run -d \
--name nacos_standalone \
--restart=always \
--network common-network \
-v /etc/docker/nacos/logs:/home/nacos/logs \
-v /etc/docker/nacos/data:/home/nacos/data \
-e MODE=standalone \
-e PREFER_HOST_MODE=hostname \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=mysql \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_DB_NAME=nacos_standalone \
-e MYSQL_SERVICE_USER=nacos \
-e MYSQL_SERVICE_PASSWORD=nacos \
-e MYSQL_SERVICE_DB_PARAM='characterEncoding=utf8&connectTimeout=2000&allowPublicKeyRetrieval=true&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC' \
-e MYSQL_DATABASE_NUM=1 \
-p 8848:8848 \
-p 9848:9848 \
-p 9849:9849 \
nacos/nacos-server:latest

3.3、单机模式启动nacos(鉴权)

docker run -d \
--name nacos_standalone_auth \
--restart=always \
--network common-network \
-v /etc/docker/nacos/logs:/home/nacos/logs \
-v /etc/docker/nacos/data:/home/nacos/data \
-e MODE=standalone \
-e PREFER_HOST_MODE=hostname \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=mysql \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_DB_NAME=nacos_config \
-e MYSQL_SERVICE_USER=nacos \
-e MYSQL_SERVICE_PASSWORD=nacos \
-e MYSQL_SERVICE_DB_PARAM='characterEncoding=utf8&connectTimeout=2000&allowPublicKeyRetrieval=true&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC' \
-e MYSQL_DATABASE_NUM=1 \
-e NACOS_AUTH_ENABLE=true \
-e NACOS_AUTH_TOKEN=NzNhMzhlMGMtMjMzMC0xN2Q3LTA0NjgtYjJjZjg1NDQ0MjNj \
-e NACOS_AUTH_IDENTITY_KEY=dc83751f-f3da-7d14-0dee-479e7a42c477 \
-e NACOS_AUTH_IDENTITY_VALUE=1f2030ce-3168-3e6b-8315-bd5099eafaf1 \
-p 8848:8848 \
-p 9848:9848 \
-p 9849:9849 \
nacos/nacos-server:latest

4、在nacos中配置

image.png

5、在springboot中使用

如果nacos和springboot版本不匹配会提示如下错误,根据提示调整springboot版本即可

- Change Spring Boot version to one of the following versions [3.0.x] .
You can find the latest Spring Boot versions here [https://spring.io/projects/spring-boot#learn]. 
If you want to learn more about the Spring Cloud Release train compatibility, you can visit this page [https://spring.io/projects/spring-cloud#overview] and check the [Release Trains] section.
If you want to disable this check, just set the property [spring.cloud.compatibility-verifier.enabled=false]

application.yml

spring:
  application:
    name: cloud-study
  profiles:
    active: dev
  config:
    import: nacos:${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        username: nacos
        password: nacos1
        file-extension: yaml
        group: DEFAULT_GROUP

测试类

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 测试
 * 
 * @author test
 */
@Controller
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
    @Value("${custom.key:'default value'}")
    private String configValue;

    @GetMapping("/get")
    @ResponseBody
    public String getConfig() {
        return "configValue: " + configValue;
    }
}

请求测试

http://localhost:8080/config/get

输出

configValue: hello world~1
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容