Nacos整合Spring Cloud Gateway实践
Spring Cloud Gateway官网:http://spring.io/projects/spring-cloud-gateway
Eureka1.0的问题和Nacos对比:https://www.sohu.com/a/240906237_355140
Nacos功能和优势介绍:https://blog.csdn.net/yelvgou9995/article/details/84655163
https://my.oschina.net/javaroad/blog/2996880
http://www.sohu.com/a/243605227_355140
https://www.sohu.com/a/246377339_494948
Eureka2.0闭源的讨论:https://www.oschina.net/news/97521/eureka-2-0-discontinued
博客推荐:许进的博客https://xujin.org/
新的Spring Cloud中国社区http://springcloud.cn/
优质个人博客网站:https://windmt.com/
GitChat:https://gitbook.cn/
1.下载curl的win版,设置环境变量为curl路径,注意使用curl向Nacos注册服务时,命令行中的url需要用双引号,单引号会报错。
快速入手官网地址:https://nacos.io/zh-cn/docs/quick-start.html
2.Spring Cloud项目整合Nacos为配置中心、注册中心
Maven更换阿里云镜像仓库:https://yq.aliyun.com/ziliao/459921
快速入手官网地址:https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html
将其中的官方demo下载下来跑一下就明白了
3.spring cloud gateway整合Nacos为注册中心,并整合2中的服务提供者、消费者项目,实现均以Nacos为注册中心的,面向服务的路由
a.注意gateway引入依赖时,spring boot与spring cloud版本冲突造成的各种依赖报错问题,这里以2中阿里的demo为准,
spring boot 2.0.4.RELEASE版本对应spring cloud Finchley.RELEASE版本。
b.注意gateway是与spring-boot-starter-web冲突的,后者使用Tomcat启动,而根据gateway正确启动日志:
......
2019-01-08 14:52:01.219 INFO 4528 --- [ctor-http-nio-1] r.ipc.netty.tcp.BlockingNettyContext : Started HttpServer on /0:0:0:0:0:0:0:0:9999
2019-01-08 14:52:01.220 INFO 4528 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 9999
Tue Jan 08 14:52:01 CST 2019 sun.misc.Launcher$AppClassLoader@18b4aac2 JM.Log:INFO Init JM logger with Slf4jLoggerFactory success, sun.misc.Launcher$AppClassLoader@18b4aac2
Tue Jan 08 14:52:01 CST 2019 sun.misc.Launcher$AppClassLoader@18b4aac2 JM.Log:INFO Log root path: C:\Users\Administrator.admin-PC\logs\
Tue Jan 08 14:52:01 CST 2019 sun.misc.Launcher$AppClassLoader@18b4aac2 JM.Log:INFO Set nacos log path: C:\Users\Administrator.admin-PC\logs\nacos
2019-01-08 14:52:01.320 INFO 4528 --- [ main] o.s.c.a.n.registry.NacosServiceRegistry : nacos registry, service-gateway 192.168.4.152:9999 register finished
2019-01-08 14:52:01.323 INFO 4528 --- [ main] com.sunfield.gateway.GatewayApplication : Started GatewayApplication in 3.344 seconds (JVM running for 4.099)
它是用Netty启动的,如果在同一个端口集成Tomcat会造成端口冲突。
c.spring cloud nacos的版本需要单独管理:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
d.gateway面向服务的默认路由规则
参考:https://blog.csdn.net/zxl646801924/article/details/80764420
https://www.cnblogs.com/linjunwei2017/p/9238083.html
配置参考:
server:
port: 9999
spring:
application:
name: service-gateway
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
gateway:
discovery: #是否与服务发现组件进行结合,通过 serviceId(必须设置成大写) 转发到具体的服务实例。默认为false,设为true便开启通过服务中心的自动根据 serviceId 创建路由的功能。
locator: #路由访问方式:http://Gateway_HOST:Gateway_PORT/大写的serviceId/**,其中微服务应用名默认大写访问。
enabled: true
routes:
- id: host_route
uri: http://www.baidu.com
predicates:
- Path=/a/**
filters:
- StripPrefix=1
这样将gateway,provider,consumer均注册到Nacos后,通过访问网关地址,拼接服务名,再拼接该服务中具体资源路径,即可通过gateway路由到该服务资源,例如:
http://localhost:9999/service-consumer/echo/38wrfewf
将访问consumer服务资源,这个资源又调用了provider服务的资源。也可通过
http://localhost:9999/service-provider/echo/38wrfewf
直接访问provider的资源。
试验过程中,访问不到consumer路径的资源是因为consumer服务后于gateway启动,没有及时通过Nacos同步资源信息,重新启动gateway后,可正常访问。
gateway demo的github地址:https://github.com/Xiaobai0419/gateway-demo.git
4.gateway动态路由配置(同样参照上面demo工程)
参考:
https://blog.csdn.net/tianyaleixiaowu/article/details/83412301
https://www.jianshu.com/p/b02c7495eb5e
https://blog.csdn.net/X5fnncxzq4/article/details/80221488
http://springcloud.cn/view/368
http://springcloud.cn/view/256
https://my.oschina.net/tongyufu/blog/1844573
https://www.colabug.com/5144483.html
https://blog.csdn.net/lazasha/article/details/84942823
AddRequestHeader的解释:
https://www.jianshu.com/p/7112871e7fc7
5.整合过滤器
需要gateway网关默认大写访问服务名,Nacos或也对服务名称大小写敏感,小写的服务名无法通过大写名字调用,所以这里服务名一律定义为大写,一定注意!!
http://springcloud.cn/view/266
已整合进上述github地址的demo。
原理:https://www.jianshu.com/p/eb3a67291050
了解金丝雀发布,蓝绿部署,滚动发布:https://www.cnblogs.com/apanly/p/8784096.html
Nacos整合Spring Cloud Gateway实践的更多相关文章
- Nacos整合Spring Cloud Gateway组件
一.什么是Spring Cloud Gateway Spring Cloud Gateway是Spring Cloud官方推出的网关框架,网关作为流量入口有着非常大的作用,常见的功能有路由转发.权限校 ...
- Dubbo想要个网关怎么办?试试整合Spring Cloud Gateway
一.背景 在微服务架构中 API网关 非常重要,网关作为全局流量入口并不单单是一个反向路由,更多的是把各个边缘服务(Web层)的各种共性需求抽取出来放在一个公共的"服务"(网关)中 ...
- Spring Cloud Alibaba学习笔记(15) - 整合Spring Cloud Gateway
Spring Cloud Gateway 概述 Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于Netty.Reactor以及WEbFlux构建,它 ...
- 通过Nacos动态刷新Spring Cloud Gateway的路由
通过Nacos动态刷新Spring Cloud Gateway的路由 一.背景 二.解决方案 三.实现功能 四.实现步骤 1.网关服务的实现 1.pom文件 2.bootstrap.yml配置文件 3 ...
- Nacos集成Spring Cloud Gateway 基础使用
项目结构 项目 端口 描述 nacos-provider 8000 服务 nacos-getway 8001 网关 nacos-provider项目依赖 <dependencies> &l ...
- 搭建一套ASP.NET Core+Nacos+Spring Cloud Gateway项目
前言 伴随着随着微服务概念的不断盛行,与之对应的各种解决方案也层出不穷.这毕竟是一个信息大爆发的时代,各种编程语言大行其道,各有各的优势.但是有一点未曾改变,那就是他们服务的方式,工作的时候各 ...
- Spring Cloud Gateway + Nacos(1)简单配置
当初我学习时候就是参考这位大佬的博客: Nacos集成Spring Cloud Gateway 基础使用 现在学习到spring cloud alibaba 使用nacos做服务中心,dubbo做通信 ...
- Spring Cloud实战 | 最终篇:Spring Cloud Gateway+Spring Security OAuth2集成统一认证授权平台下实现注销使JWT失效方案
一. 前言 在上一篇文章介绍 youlai-mall 项目中,通过整合Spring Cloud Gateway.Spring Security OAuth2.JWT等技术实现了微服务下统一认证授权平台 ...
- spring Cloud网关之Spring Cloud Gateway
Spring Cloud Gateway是什么?(官网地址:https://cloud.spring.io/spring-cloud-gateway/reference/html/) Spring C ...
随机推荐
- idea软件上设置爱彼迎字体
- Day5 装饰器和文件操作
一.装饰器 1.什么是装饰器 装饰器即函数 装饰即修饰,意指为其他函数添加新功能 装饰器定义:本质就是函数,功能是为其他函数添加新功能 2. 装饰器需要遵循的原则 1.不修改被装饰函数的源代码(开放封 ...
- 使用Groovy+Spock构建可配置的订单搜索接口测试用例集
概述 测试是软件成功上线的安全网.基本的测试包含单元测试.接口测试.在 "使用Groovy+Spock轻松写出更简洁的单测" 一文中已经讨论了使用GroovySpock编写简洁的单 ...
- 转:【专题十二】实现一个简单的FTP服务器
引言: 休息一个国庆节后好久没有更新文章了,主要是刚开始休息完心态还没有调整过来的, 现在差不多进入状态了, 所以继续和大家分享下网络编程的知识,在本专题中将和大家分享如何自己实现一个简单的FTP服务 ...
- WebStorm: The Smartest JavaScript IDE by JetBrains
WebStorm: The Smartest JavaScript IDE by JetBrains https://www.jetbrains.com/webstorm/?fromMenu
- hashCode 一致性hash 算法
1 如果两个对象相同,那么它们的hashCode值一定要相同.也告诉我们重写equals方法,一定要重写 hashCode方法,同一个对象那么hashcode就是同一个(同一个对象什么都是相同的).2 ...
- Codeforce 287A - IQ Test (模拟)
In the city of Ultima Thule job applicants are often offered an IQ test. The test is as follows: the ...
- [转载]表单校验之datatype
凡要验证格式的元素均需绑定datatype属性,datatype可选值内置有10类,用来指定不同的验证格式. 如果还不能满足您的验证需求,可以传入自定义datatype,自定义datatype是一个非 ...
- Lambda表达式select()和where()的区别
可能很多同学和我一样对于select()和where()区别并不是太清晰,其实两者还是有本质区别的. 1.where()用法:必须加条件,且返回对象结果. static void Main(strin ...
- [trick] 玩弄svn的目录结构
今天在使用svn进行版本管理时出现了一个小问题: 原本在s目录下有一个c目录,不知为何被删除了,而svn st命令并没有认为它消失,svn up命令也无法下载回来: 从另一个地方拷贝过来一个c,svn ...