title: SpringCloudAlibaba-4-nacos服务注册与发现整合spring-cloud-gateway调用
date: 2020-01-27 13:12:39
tags:
SpringCloudAlibaba
前言
我们前几节主要介绍了nacos注册中心实现服务之间的互相调用,我们这一节通过讲解nacos作为注册中心实现服务路由转发功能。
内容
1.Gateway
2.本节简介
使用SpringCloud GateWay实现如下功能:
1.浏览器通过gateway(8054)端口访问到:provider(8051)服务
2.需要通过网关请求到provider服务,需要使用:LoadBalancerClient Filter
实现如下:
- 1.创建项目:nacos-discovery-gateway-server
- 2.修改pom.xml:去除spring-boot-starter-web和spring-boot-starter-test,引入:
|
|
spring:
application:
name: nacos-discovery-gateway-server
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
gateway:
routes:
- id: nacos-discovery-provider
uri: lb://nacos-discovery-provider
predicates:
- Path=/hello
server:
port: 8054
//设置nacos-discovery-gatewayr的application.yml
spring:
application:
name: nacos-discovery-gateway-server
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
gateway:
routes:
- id: nacos-discovery-provider
uri: lb://nacos-discovery-provider
predicates:
- Path=/provider
//设置nacos-discovery-provider的context-path
server:
port: 8051
servlet:
context-path: provider
spring:
application:
name: nacos-discovery-gateway-server
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
metadata:
name: lengleng
gateway:
routes:
- id: nacos-discovery-provider
uri: lb://nacos-discovery-provider
predicates:
- Path=/provider/**
filters:
- StripPrefix=1
```