nacos客户端想要访问nacos服务端, 需要经过nginx负载均衡, 才能正常访问。因为是在本地,所以nginx搭建还是以windows环境做演示。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.html
首先我们把nginx压缩包解压一下,需要获取文件的可以直接在我公众号发送: nginx,也可以自行去官网下载。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.html
然后修改其conf文件下的nginx.conf配置文件:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.html
文件内容为:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.htmlworker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 配置nacos集群的地址集, 注意:这里的ip和端口配置成你们自己的nacos的服务地址
upstream nacos-cluster {
server 192.168.3.2:8840;
server 192.168.3.2:8841;
server 192.168.3.2:8842;
}
server {
listen 80;
server_name localhost;
# nacos客户端首先会访问到:/nacos, 获取到:/nacos-cluster,再找
# upstream nacos-cluster 下的其中一个server再访问;
location /nacos {
proxy_pass http://nacos-cluster;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
改好配置后,可以用 nginx -t 命令测试文件修改是否有问题:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.html
没有问题再用 start nginx 命令启动nginx文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.html
以下是我们前一章搭建的nacos服务端集群,我们就拿这个集群做测试文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.html
先在集群上加个配置:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.html
再修改nacos客户端里配置的所要访问的nacos服务端的地址信息,两个配置文件都需要改成80的端口,也就是要经过nginx再去访问nacos服务端的集群。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.html
order-service 的代码没有改变:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.html
测试访问:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.html
以上就通过nginx反向代理,拿到了nacos集群上的配置信息。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/36230.html