Docker - MySql - Nacos - 單機(jī)(非鑒權(quán)/鑒權(quán))

1、準(zhǔn)備docker 容器內(nèi)相互訪問的網(wǎng)絡(luò)

創(chuàng)建容器內(nèi)部公用網(wǎng)絡(luò)

docker network create common-network

docker network 命令備查

# 連接容器到網(wǎng)絡(luò)
docker network connect 網(wǎng)絡(luò)名 

# 創(chuàng)建網(wǎng)絡(luò)
docker network create  網(wǎng)絡(luò)名 

# 斷開網(wǎng)絡(luò)
docker network disconnect  網(wǎng)絡(luò)名

# 查看網(wǎng)絡(luò)具體信息
docker network inspect  

# 查看所有網(wǎng)絡(luò)列表
docker network ls   

# 刪除無用網(wǎng)絡(luò)
docker network prune  

# 刪除指定的網(wǎng)絡(luò)
docker network rm 網(wǎng)絡(luò)ID

2、準(zhǔn)備mysql

2.1、docker拉取mysql鏡像

docker pull mysql

2.2、啟動(dòng)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、準(zhǔn)備MySql 數(shù)據(jù)庫腳本

數(shù)據(jù)庫腳本:下載地址

2.3、為nacos創(chuàng)建mysql用戶名、密碼

創(chuàng)建用戶

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

授予用戶對(duì) nacos_config 數(shù)據(jù)庫的所有權(quán)限

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

刷新權(quán)限

FLUSH PRIVILEGES;

3、準(zhǔn)備nacos

3.1、docker拉取nacos鏡像

docker pull nacos/nacos-server

3.2、單機(jī)模式啟動(dòng)nacos(非鑒權(quán))

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、單機(jī)模式啟動(dòng)nacos(鑒權(quán))

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版本不匹配會(huì)提示如下錯(cuò)誤,根據(jù)提示調(diào)整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;
    }
}

請(qǐng)求測試

http://localhost:8080/config/get

輸出

configValue: hello world~1
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容