Spring Cloud GateWay整合nacos应用

Spring Cloud GateWay整合nacos应用

1.安装运行nacos服务器
yum install nacos

cd /usr/local/nacos/bin

sh startup.sh -m standalone

2.服务网关集成
1)加入必要的依赖配置

Spring Cloud GateWay整合nacos应用

此处应注意版本对应:

<spring.boot.version>2.1.0.RELEASE</spring.boot.version>
<spring.cloud.version>Greenwich.SR2</spring.cloud.version>
<spring.cloud.alibaba.version>2.1.0.RELEASE</spring.cloud.alibaba.version>

2)配置服务名称和Nacos地址并配置Path路由规则

Spring Cloud GateWay整合nacos应用

Spring Cloud GateWay整合nacos应用

3)开启Spring Cloud的服务注册与发现和构建路由

Spring Cloud GateWay整合nacos应用

3.GateWay验证Route转发
1)验证网关服务注册

启动nacos,远程访问nacos服务器查看网关服务

Spring Cloud GateWay整合nacos应用

2)测试服务转发

验证Java API方式构建的服务转发,浏览器端访问http://localhost:8820/gateway-nacos/shenzhen/hehe,会自动转发https://www.jd.com/

Spring Cloud GateWay整合nacos应用

可以在控制台的debug info中看到

Route matched: CompositeDiscoveryClient_gateway-nacos

Route matched: forward

Spring Cloud GateWay整合nacos应用

验证配置文件方式构建的服务转发,浏览器端访问http://localhost:8820/gateway-nacos/foundation,会自动转发到http://acpache.org/foundation/
Spring Cloud GateWay整合nacos应用

可以在控制台的debug info中看到

Route matched: CompositeDiscoveryClient_gateway-nacos

Route matched: Path_Route

Spring Cloud GateWay整合nacos应用

两种方式配置路由转发默认使用的是Path_Route的路由规则,也可按照实际效果对其他路由规则进行配置