Docker配置Nacos集群

需要对docker,docker-compose,nginx,springcloud,nacos有一定的了解
以下配置,nginx为可选项

准备Sql环境

添加数据库名为nacos_config
在该库下运行此处的SQL
添加Mysql账号

CREATE USER 'nacos'@'%' IDENTIFIED BY 'nacos';
GRANT ALL PRIVILEGES ON nacos_config.* TO nacos@'%' IDENTIFIED BY 'nacos';

添加Nacos账号
默认的账号密码为nacos/nacos
生成admin密钥

// 需要引入SpringSecurity
System.out.println(new BCryptPasswordEncoder().encode("admin"));

账号为root,密码为admin

USE nacos_config ;

INSERT INTO users (username, password, enabled) 
VALUES
  ('root', '$2a$10$9CMXe.ZXHjmDGuC7jXORTeYxTHf1/jrGkS.Opqyb5Vsg/1OtEqNf6', TRUE);
  
INSERT INTO roles(username, role)VALUES('root','ROLE_ADMIN');

docker-compose

version: "2"
services:
  nacos1:
    hostname: nacos1
    container_name: nacos1
    image: nacos/nacos-server:1.1.3
    volumes:
      - /data/nacos/logs:/home/nacos/logs
      - /data/nacos/conf:/home/nacos/init.d
    ports:
      - "3333:8848"
      - "9555:9555"
    env_file:
      - ./nacos-hostname.env
    restart: always
    depends_on:
      - mysql
  nacos2:
    hostname: nacos2
    image: nacos/nacos-server:1.1.3
    container_name: nacos2
    volumes:
      - /data/nacos2/logs:/home/nacos/logs
      - /data/nacos2/conf:/home/nacos/init.d
    ports:
      - "4444:8848"
    env_file:
      - ./nacos-hostname.env
    restart: always
    depends_on:
      - mysql
  nacos3:
    hostname: nacos3
    image: nacos/nacos-server:1.1.3
    container_name: nacos3
    volumes:
      - /data/nacos3/logs:/home/nacos/logs
      - /data/nacos3/conf:/home/nacos/init.d
    ports:
      - "5555:8848"
    env_file:
      - ./nacos-hostname.env
    restart: always
    depends_on:
      - mysql
  mysql:
    container_name: mysql
    image: mysql:5.7
    volumes:
      - /data/mysqlMaster/my.cnf:/etc/mysql
      - /data/mysqlMaster/data:/var/lib/mysql
    ports:
      - "3306:3306"

环境

可以指定数据库数量为1,我这里直接将主从指定为一个地址

# 可以使用主机名作为参数,默认为IP
PREFER_HOST_MODE=hostname
# 集群
NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848
# 数据库配置
SPRING_DATASOURCE_PLATFORM=mysql
MYSQL_MASTER_SERVICE_HOST=mysql
MYSQL_MASTER_SERVICE_PORT=3306
MYSQL_MASTER_SERVICE_USER=nacos
MYSQL_MASTER_SERVICE_PASSWORD=nacos
MYSQL_MASTER_SERVICE_DB_NAME=nacos_config 
MYSQL_SLAVE_SERVICE_HOST=mysql
# 防止内存溢出
JVM_XMS=128m
JVM_XMX=128m

启动
docker-compose up

[root@mengnan nacos]# docker-compose ps
 Name              Command             State                       Ports                     
---------------------------------------------------------------------------------------------
mysql    docker-entrypoint.sh mysqld   Up      0.0.0.0:3306->3306/tcp, 33060/tcp             
nacos1   bin/docker-startup.sh         Up      0.0.0.0:3333->8848/tcp, 0.0.0.0:9555->9555/tcp
nacos2   bin/docker-startup.sh         Up      0.0.0.0:4444->8848/tcp                        
nacos3   bin/docker-startup.sh         Up      0.0.0.0:5555->8848/tcp   

nginx

docker run --name test -d nginx
docker cp test:/etc/nginx/nginx.conf /data/nginx/conf/
docker cp test:/etc/nginx/conf.d/default.conf  /data/nginx/conf/conf.d

docker run -d --privileged -it \
-p 1111:1111 \
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /data/nginx/conf/conf.d:/etc/nginx/conf.d:ro \
-v /data/nginx/logs:/var/log/nginx \
nginx

修改配置default.conf再重启

# 负载均衡
upstream cluster {
   server [IP]:3333;
   server [IP]:4444;
   server [IP]:5555;
}
server {
	# 修改端口
    listen       1111;
   # listen  [::]:80;
    server_name  localhost;

	# 反向代理
    location / {
       # root   /usr/share/nginx/html;
       # index  index.html index.htm;
       proxy_pass http://cluster;
    }
}

修改配置

为需要注册到nacos的微服务添加配置

server:
  port: 9001
spring:
  application:
    name: nacos-provider
  cloud:
    # nacos服务发现
    nacos:
      discovery:
        # 集群地址,或许也可以不使用nginx,直接用nacos地址,多个逗号隔开
        server-addr: [IP]:1111

management:
  endpoints:
    web:
      exposure:
        include: "*"

启动类

@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerMain {
  public static void main(String[] args) {
    SpringApplication.run(ConsumerMain.class, args);
  }
}

显示这个说明成功nacos集群

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Docker上安装Nacos集群配置Zookeeper,可以按照以下步骤进行操作: 1. 首先,确保已经安装了DockerDocker Compose。 2. 创建一个名为`docker-compose.yml`的文件,并在其中添加以下内容: ```yaml version: '3' services: nacos-server-1: image: nacos/nacos-server:latest container_name: nacos-server-1 ports: - "8848:8848" environment: - PREFER_HOST_MODE=hostname - MODE=standalone - SPRING_DATASOURCE_PLATFORM=mysql - MYSQL_SERVICE_HOST=mysql - MYSQL_SERVICE_PORT=3306 - MYSQL_SERVICE_DB_NAME=nacos_config - MYSQL_SERVICE_USER=root - MYSQL_SERVICE_PASSWORD=root depends_on: - mysql nacos-server-2: image: nacos/nacos-server:latest container_name: nacos-server-2 ports: - "8849:8848" environment: - PREFER_HOST_MODE=hostname - MODE=standalone - SPRING_DATASOURCE_PLATFORM=mysql - MYSQL_SERVICE_HOST=mysql - MYSQL_SERVICE_PORT=3306 - MYSQL_SERVICE_DB_NAME=nacos_config - MYSQL_SERVICE_USER=root - MYSQL_SERVICE_PASSWORD=root depends_on: - mysql mysql: image: mysql:5.7 container_name: mysql ports: - "3306:3306" environment: - MYSQL_ROOT_PASSWORD=root - MYSQL_DATABASE=nacos_config zookeeper: image: zookeeper:3.4.14 container_name: zookeeper ports: - "2181:2181" ``` 3. 保存文件并在终端中进入该文件所在的目录。 4. 运行以下命令启动Nacos集群和Zookeeper: ```bash docker-compose up -d ``` 5. 等待一段时间,直到所有容器都成功启动。 现在,你已经成功在Docker上安装了Nacos集群,并配置了Zookeeper。你可以通过访问`http://localhost:8848/nacos`来访问Nacos控制台。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值