首先springcloud 后台 创建 两个连接Nginx的微服务
两个yml配置如下
server:
port: 9002 #或者9001
spring:
application:
name: nacos-payment-provider #Nacos註冊微服務名字
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:9999 #这里是Nginx的地址
# server-addr: 127.0.0.1:9999 #配置Nacos地址 注册服务地址
management:
endpoints:
web:
exposure:
include: '*' #暴露端口 *代表全部
然后Nginx的config配置 配置Nacos的三台服务
最后是Nacos conf目录下的cluster.conf 配置集群
先启动Nacos集群
再启动Nginx
再启动cloud两个服务
哦豁~~~~ 报错 400
java.lang.IllegalStateException: failed to req API:/nacos/v1/ns/instance after all
servers([127.0.0.1:9999]) tried: failed to req API:127.0.0.1:9999/nacos/v1/ns/instance.
code:400 msg: <html><body><h1>Whitelabel Error Page</h1><p>This application has n
explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>
Tue May 24 16:50:06 CST 2022</div><div>There was an unexpected error
(type=Bad Request, status=400).</div><div>receive invalid redirect
request from peer 10.161.139.216</div></body></html>
然看了一篇文章
http://t.csdn.cn/6Loym
说是错误的原因是nginx和nacos的ip映射出了问题
不过他说的有点模糊我整理了一下思路然后解决了
-------------------------------------分割线---------------------------------------------
首先接下来直接打开Nacos的cluster.conf
这里可以看到 他其实不是Nacos其实不是按照真正的ip地址的
我这里IP地址应该是10.161.139.216 但是他连接的是其他的 ip 一个虚拟的 192.168.32.1
而且如果你看Nginx访问的Nacos看他的节点列表你会看见
192.168.32.1 :3333 它也已经把正确的ip显示出来了
接下来就是修改Nacos的配置文件了 改正确后 访问Nginx的Nacos页面看节点列表就会正常
Nacos的配置文件也不会多出来一条
然后重启cloud服务 Nginx的Nacos页面就会显示出我的服务
这里你如果想避免接下里的一些隐患 应该也把Nginx的config配置也改了 ip改的跟Nacos一样
好!!!到此问题解决