一、服务注册
添加依赖:
1 | <dependency> |
新建 Spring Cloud 项目,在 bootstrap.yml 新增配置:
1 | spring: |
启动项目,可以在 Nacos 服务列表看到服务已注册
二、服务消费
新建项目 service2 (端口:8079)
在 bootstrap.yml 新增配置:
1 | spring: |
新增 Controller:
1 | @RestController |
访问 http://localhost:8079/greeting 可以看到与访问 http://localhost:8070/greeting 同样的结果
三、集成 Spring Cloud Gateway
新建项目 (本文使用的 Spring Cloud 的版本为 Hoxton.SR3):
1 | <dependency> |
配置:
1 | server: |
— 2020-05-26 更新开始 —
Spring Cloud Gateway 跨域设置
1 | spring: |
— 2020-05-26 更新结束 —
访问 http://localhost:8081/service1/greeting 即可路由到 service1 的 greeting 方法
参考: