1,文档
spring cloud alibaba nacos 集成文档
https://sca.aliyun.com/docs/2023/user-guide/nacos/quick-start/?spm=7145af80.74fdb509.0.0.605970212LoM4b
spring cloud 文档(对国人不太友好)
https://www.springcloud.cc/
2,安装nacos
请参照:https://www.jianshu.com/writer#/notebooks/54745399/notes/124936132
3,maven 配置
版本
spring-boot 3.4.1
spring-cloud 2024.0.0
spring-cloud-alibaba 2023.0.3.2
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
这个版本的spring-cloud loadbalancer 从gateway里分离出来了,所以一定要单独引入
3,gateway 项目
3.1 启动类 要加上 @EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
@Slf4j
public class GatewayMain {
public static void main(String[] args) {
SpringApplication.run(GatewayMain.class, args);
log.info("网关启动成功");
}
}
3.2 注册中心配置
nacos:
discovery:
server-addr: 127.0.0.1:8848
username: nacos
password: 123456
namespace: test-dubbo-local
group: DEFAULT_GROUP
enabled: true
注意点:
- 同一个项目的微服务(包括网关),要在同一个命名空间(namespace)与group下
3.3 网关路由配置
server:
port: 20001
spring:
application:
name: gateway
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
filter:
strip-prefix:
enabled: true
# routes:
# - id: dubbo-demo
# uri: lb://dubbo-demo
# predicates:
# - Path=/dubbo-demo/**
# filters:
# - StripPrefix=1
# - id: dubbo-demo1
# uri: http://127.0.0.1:10002
# predicates:
# - Path=/d1/**
# filters:
# - StripPrefix=1
# 打印日志
logging:
level:
com.alibaba.cloud.nacos.discovery: debug
# 这个带着nacos 路由信息
org.springframework.cloud.gateway: debug
两种方式(不要同时用,否则可能会造成混乱)
1,根据服务名称自动路由
必须得引入loadbalancer 否则503
gateway:
discovery:
locator:
# 打开后就根据nacos上的服务名 自动路由
enabled: true
lower-case-service-id: true
filter:
# 实际访问时 去掉uri里的 服务名
strip-prefix:
enabled: true
url 例子:http://127.0.0.1:20001/dubbo-demo/test/t1
2,主动配置路由:可以修改请求前缀
routes:
- id: dubbo-demo
uri: lb://dubbo-demo
predicates:
- Path=/dubbo-demo-1/**
filters:
- StripPrefix=1
- id: dubbo-demo1
uri: http://127.0.0.1:10002
predicates:
- Path=/d1/**
filters:
- StripPrefix=1
lb://dubbo-demo 的意思就是 从nacos里取路由信息 负载均衡。与自动路由一样 需要引入loadbalancer
url例子:http://127.0.0.1:20001/dubbo-demo-1/test/t1
【lb://】后面是nacos 的服务名
写死ip的 就不需要引入loadbalancer
url例子:http://127.0.0.1:20001/d1/test/t1
请求日志
[gateway] [ctor-http-nio-2] o.s.c.g.h.RoutePredicateHandlerMapping : Route matched: ReactiveCompositeDiscoveryClient_dubbo-demo
[gateway] [ctor-http-nio-2] o.s.c.g.h.RoutePredicateHandlerMapping : Mapping [Exchange: GET http://127.0.0.1:20001/dubbo-demo/test/t1] to Route{id='ReactiveCompositeDiscoveryClient_dubbo-demo', uri=lb://dubbo-demo, order=0, predicate=Paths: [/dubbo-demo/**], match trailing slash: true, gatewayFilters=[[[RewritePath /dubbo-demo/?(?<remaining>.*) = '/${remaining}'], order = 1]], metadata={dubbo.metadata-service.url-params={"prefer.serialization":"hessian2,fastjson2","version":"2.0.0","dubbo":"2.0.2","release":"3.3.1","side":"provider","port":"10002","protocol":"tri"}, nacos.instanceId=192.168.2.33#10002#null#dubbo-demo, nacos.weight=1.0, nacos.cluster=DEFAULT, nacos.ephemeral=true, nacos.healthy=true, dubbo.endpoints=[{"port":10002,"protocol":"dubbo"},{"port":10002,"protocol":"tri"}], dubbo.metadata.revision=24d6431f291b5cdb7ae0ba58b4b97c15, dubbo.metadata.storage-type=local, meta-v=2.0.0, timestamp=1736486700990}}
[gateway] [ctor-http-nio-2] o.s.c.g.h.RoutePredicateHandlerMapping : [e04a8b0b-1] Mapped to org.springframework.cloud.gateway.handler.FilteringWebHandler@2ec5cc14
2025-01-10T14:43:57.732+08:00 DEBUG 13428 --- [gateway] [ctor-http-nio-2] o.s.c.g.handler.FilteringWebHandler : Sorted gatewayFilterFactories: [[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RemoveCachedBodyFilter@4d2f9e3c}, order = -2147483648], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter@69d667a5}, order = -2147482648], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@671da0f9}, order = -1], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardPathFilter@646e6d07}, order = 0], [[RewritePath /dubbo-demo/?(?<remaining>.*) = '/${remaining}'], order = 1], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@686b1e2d}, order = 10000], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ReactiveLoadBalancerClientFilter@6cc8da1c}, order = 10150], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.LoadBalancerServiceInstanceCookieFilter@39a3984c}, order = 10151], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@5501d1c8}, order = 2147483646], GatewayFilterAdapter{delegate=com.zx.filter.MyGlobalFilter@29c244e}, [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyRoutingFilter@4404a6b}, order = 2147483647], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardRoutingFilter@6e7e0259}, order = 2147483647]]
4,引入配置中心
请参考:https://www.jianshu.com/p/ec1c3154c86e
引入配置中心后,更改路由就不用重启项目了