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 ...
随机推荐
- Marlin 溫度 sensor 校正
Marlin 溫度 sensor 校正 使用 Type-K 溫度計 將探針綑綁在加熱頭側面 開啟Marlin-Marlin_v1\Marlin\thermistortables.h 要修改的溫度對應表 ...
- JavaScript--详解typeof的用法
typeof定义 typeof是一元运算符,用来返回操作参数的类型(不是值) 检查一个变量是否存在,是否有值 typeof在两种情况下会返回"undefined&q ...
- Java重排序
重排序数据依赖性 如果两个操作访问同一个变量,且这两个操作中有一个为写操作,此时这两个操作之间就存在数据依赖性.数据依赖分下列三种类型: 名称 代码示例 说明 写后读 a = 1;b = a; 写一个 ...
- c# 使用checked和unchecked
首先要知道int型在c#中是一个32位的数.由此可以知道int型的取值范围是(-2147483648~2147483647)当要使用int的最小值或者是最大值的时候,可以使用int.MinValue和 ...
- <转>jmeter(九)逻辑控制器
本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...
- 案例:通过shell脚本实现mysql数据备份与清理
Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口.它接收用户输入的命令并把它送入内核去执行,实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核,不仅如此,Sh ...
- 费马小定理与GCD&LCM
若 t = 1 , a ^ ( p - 2 ) 为 a 在取模 p 意义下的乘法逆元 通常用 inv 表示 证明: b * a =(三等)1(mod p) a ^ ( p - 2 ) * a =(三 ...
- webpack 创建vue项目过程中遇到的问题和解决方法
目录 1 webpack简介 2 webpack实现多个输入输出多个html 3 webpack 中的module下rules 下的use和loader选项 4 webpack 文件更新,如何使页面 ...
- Inferred type 'S' for type parameter 'S' is not within its bound;
在使用springboot 方法报错: Inferred type 'S' for type parameter 'S' is not within its bound; should extends ...
- GoldenGate 12.2抽取Oracle 12c多租户配置过程
linux下安装12c 重启linux之后,dbca PDB/CDB使用 SQL> select instance_name from v$instance; INSTANCE_NAME --- ...