准备
检查是否安装jdk
[root@hao /usr/local/software/elk/logstash]# java -version
java version "1.8.0_341"
Java(TM) SE Runtime Environment (build 1.8.0_341-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.341-b10, mixed mode)
配置nacos
去github下载nacos:2.2.3:https://github.com/alibaba/nacos/tags
上传到linux的nacos文件夹并解压
[root@hao /usr/local/software/nacos]# tar -zxvf nacos-server-2.2.3.tar.gz
nacos/LICENSE
nacos/NOTICE
nacos/target/nacos-server.jar
nacos/conf/
nacos/conf/derby-schema.sql
nacos/conf/1.4.0-ipv6_support-update.sql
nacos/conf/application.properties.example
nacos/conf/nacos-logback.xml
nacos/conf/announcement.conf
nacos/conf/mysql-schema.sql
nacos/conf/cluster.conf.example
nacos/conf/application.properties
nacos/bin/startup.sh
nacos/bin/startup.cmd
nacos/bin/shutdown.sh
nacos/bin/shutdown.cmd
[root@hao /usr/local/software/nacos]# ls
nacos nacos-server-2.2.3.tar.gz
改名为nacos_8848
[root@hao /usr/local/software/nacos]# mv nacos nacos_8848
[root@hao /usr/local/software/nacos]# ls
nacos_8848 nacos-server-2.2.3.tar.gz
复制两份分别为nacos_8858、nacos_8868
[root@hao /usr/local/software/nacos]# cp -r nacos_8848 nacos_8858
[root@hao /usr/local/software/nacos]# cp -r nacos_8848 nacos_8868
[root@hao /usr/local/software/nacos]# ls
nacos_8848 nacos_8858 nacos_8868 nacos-server-2.2.3.tar.gz
删除application.properties文件,把application.properties.example改为application.properties文件
[root@hao /usr/local/software/nacos]# cd nacos_8848/
[root@hao /usr/local/software/nacos/nacos_8848]# ls
bin conf LICENSE NOTICE target
[root@hao /usr/local/software/nacos/nacos_8848]# cd conf/
[root@hao /usr/local/software/nacos/nacos_8848/conf]# ls
1.4.0-ipv6_support-update.sql application.properties cluster.conf.example mysql-schema.sql
announcement.conf application.properties.example derby-schema.sql nacos-logback.xml
[root@hao /usr/local/software/nacos/nacos_8848/conf]# rm -rf application.properties
[root@hao /usr/local/software/nacos/nacos_8848/conf]# mv application.properties.example application.properties
[root@hao /usr/local/software/nacos/nacos_8848/conf]# ls
1.4.0-ipv6_support-update.sql announcement.conf application.properties cluster.conf.example derby-schema.sql mysql-schema.sql nacos-logback.xml
再修改application.properties文件
### Count of DB:
db.num=1### Connect URL of DB:
db.url.0=jdbc:mysql://192.168.133.100:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai
db.user.0=root
db.password.0=123456
运行sql文件[解压文件夹下的conf/nacos-mysql.sql]
运行nacos:官网
启动:进入bin/文件夹下面
启动命令(standalone代表着单机模式运行,非集群模式):
sh startup.sh -m standalone
访问地址:http://192.168.133.100:8848/nacos,账号密码:nacos
SpringCloud-alibaba整合nacos作为配置中心
nacos作为项目的配置中心,替代config模块
新建命名空间
配置管理
新建配置
点击发布
SpringBoot整合SpringCloud-alibaba
配置文件bootstrap.yml、application.yml文件
spring:cloud:nacos:config:server-addr: 192.168.133.100:8848namespace: b4d78fc0-d403-43d0-b721-93375733c9d2name: smart-configgroup: devfile-extension: yamlapplication:name: smart-alibaba-nacos
server:port: 15360
logging:level:com.wnhz.smart.cloud.alibaba: debug
测试
package com.wnhz.smart.cloud.alibaba.nacos.controller;import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author Hao* @date 2023-12-06 17:13*/
@RestController
@RequestMapping("/api/nacos")
@RefreshScope
@Slf4j
public class NacosController {@Value("${name}")private String name;@GetMapping("ali")public String ali() {log.debug("读取nacos的配置数据:{}", name);return name;}
}
其中@RefreshScope注解是实时监控配置内容的变化
下面是启动一次程序,换了次配置文件的返回结果由xxx变为yyy
SpringCloud-alibaba整合nacos作为服务中心
为什么要使用集群
CAP,nacos实现了AP
配置nacos集群
进入/usr/local/software/nacos/nacos_8848/conf文件夹
把cluster.conf.example文件重命名为cluster.conf
[root@hao /usr/local/software/nacos/nacos_8848/conf]# ls
1.4.0-ipv6_support-update.sql announcement.conf application.properties cluster.conf.example derby-schema.sql mysql-schema.sql nacos-logback.xml
[root@hao /usr/local/software/nacos/nacos_8848/conf]# mv cluster.conf.example cluster.conf
编辑cluster.conf文件
[root@hao /usr/local/software/nacos/nacos_8848/conf]# vim cluster.conf
进入/nacos_8858/conf修改application.properties文件和cluster.conf文件,同上
分别启动8848和8858
[root@hao /usr/local/software/nacos/nacos_8848/bin]# ./startup.sh
[root@hao /usr/local/software/nacos/nacos_8858/bin]# ./startup.sh
进入网页:http://192.168.133.100:8848/nacos
成功
配置一键启动脚本
startup.sh
echo "准备开启nacos服务集群..."#调用启动nacos的脚本
/usr/local/software/nacos/nacos_8848/bin/startup.sh
/usr/local/software/nacos/nacos_8858/bin/startup.sh
shutdown.sh
echo "准备关闭nacos服务集群..."#调用启动nacos的脚本
/usr/local/software/nacos/nacos_8848/bin/shutdown.sh
/usr/local/software/nacos/nacos_8858/bin/shutdown.sh
restart.sh
echo "准备重启nacos服务集群..."#调用关闭、启动nacos的脚本
/usr/local/software/nacos/nacos_8848/bin/shutdown.sh
/usr/local/software/nacos/nacos_8858/bin/shutdown.sh
/usr/local/software/nacos/nacos_8848/bin/startup.sh
/usr/local/software/nacos/nacos_8858/bin/startup.sh
3个文件给777权限
完成
实现负载均衡
删除docker的nginx容器
修改nginx.conf文件
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {worker_connections 1024;
}
stream {#loadBlance is hereupstream nacos-tcp{server 192.168.201.107:9848;server 192.168.201.107:9858;}#server code is here....server {listen 8777;proxy_pass nacos-tcp;}
}
http {include /etc/nginx/mime.types;default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;sendfile on;#tcp_nopush on;keepalive_timeout 65;#gzip on;#loadBlance is hereupstream nacos-http{server 192.168.201.107:8848;server 192.168.201.107:8858;}#server code is here....server {keepalive_requests 120;listen 7777;location /{proxy_pass http://nacos-http;proxy_redirect off;}}include /etc/nginx/conf.d/*.conf;
}
创建新容器
docker run -it \
--name nginx \
-p 8075:80 \
-p 8080:8080 \
-p 7777:7777 \
-p 8777:8777 \
--privileged=true \
--network wn_docker_net \
--ip 172.18.12.90 \
-v /etc/localtime:/etc/localtime \
-v /usr/local/software/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /usr/local/software/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /usr/local/software/nginx/html:/usr/share/nginx/html \
-v /usr/local/software/nginx/logs:/var/log/nginx \
-d nginx