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 ...
随机推荐
- python pillow
https://www.cnblogs.com/morethink/p/8419151.html#%E7%9B%B4%E6%8E%A5%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA ...
- hdu5029 树链剖分 + 线段树
将树映射在线段上进行操作 然后每个 重链变成一个连续的区间 #include <iostream> #include <cstdio> #include <strin ...
- Java集合-----Set详解
Set是没有重复元素的集合,是无序的 1.HashSet HashSet它是线程不安全的 HashSet常用方法: add(E element) 将指定的元素添加到此集合(如果尚未存 ...
- [openjudge-动态规划]买书
题目描述 描述 小明手里有n元钱全部用来买书,书的价格为10元,20元,50元,100元.问小明有多少种买书方案?(每种书可购买多本) 输入 一个整数 n,代表总共钱数.(0 <= n < ...
- cmd强行终止进程
找到某个进程对应的端口号 Netstat -ano|findstr 8080 通过ip 地址,强行 taskkill /PID 11048 但有的情况下终止不了,需要通过下列方式进行 task ...
- Subversion客户端接受服务器证书出现“The certificate hostname does not match”的问题
当使用https://形式的URL连接Apache时,Subversion客户端将会受到两个类型的响应: 1.一个服务器证书 2.一个针对客户端证书的请求 在本人的应用场景主要涉及SVN仓库迁移的操作 ...
- vue之vue-cookies安装和使用说明
vue之vue-cookies安装和使用说明npm官方链接:https://www.npmjs.com/package/vue-cookies 安装,在对应项目根目录下执行:npm install v ...
- urlopen和urlretrieve
import urllib import re url = "https://www.duitang.com/search/?kw=%E9%AC%BC%E6%80%AA&type=f ...
- Codeforce 733B - Parade (枚举)
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldi ...
- .NET 常用ORM之Gentle.Net
.Net常用的就是微软的EF框架和Nhibernate,这两个框架用的都比较多就不做详细介绍了,今天我们来看看Gentle.Net,Gentle.Net是一个开源的优秀O/R Mapping的对象持久 ...