需要对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);
}
}
显示这个说明成功