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 ...
随机推荐
- c#之如何正确地实现IDisposable接口
见实例: public class TestClass : IDisposable { //供程序员显式调用的Dispose方法 public void Dispose() { //调用带参数的Dis ...
- [3]传奇3服务器源码分析一 DBServer
留存 服务端下载地址: 点击这里
- Python 7 -- 文件存储数据
上一节总结了一个基本web应用的代码,这一节主要讲用户访问的数据记录在log文件中,并显示在页面上. 这节步骤: 按以下目录建好相应的文件夹及内容 webapp|----vsearch4web.py ...
- Memento Mori (二维前缀和 + 枚举剪枝)
枚举指的是枚举矩阵的上下界,然后根据p0, p1, p2的关系去找出另外的中间2个点.然后需要记忆化一些地方防止重复减少时间复杂度.这应该是最关键的一步优化时间,指的就是代码中to数组.然后就是子矩阵 ...
- sklearn异常检测demo
sklearn 异常检测demo代码走读 # 0基础学python,读代码学习python组件api import time import numpy as np import matplotlib ...
- linux常用命令:mkdir 命令
linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录. 1.命令格式: mkdir [选项] 目录... 2.命令 ...
- Linux服务器安装部署redis
参考地址: redis教程:http://www.runoob.com/redis/redis-tutorial.html redis百度百科:https://baike.baidu.com/item ...
- bzoj4358 premu
题目链接 莫队算法 没有用线段树,而是看了showson的并查集%%% #include<algorithm> #include<iostream> #include<c ...
- The Little Prince-11/26
WRITE BEFORE THE BOOK REVIEW I have read The Little Prince for three or four times. However I still ...
- ubuntu 18.04下安装Java
参照以下链接,这个是我找到的最易上手的学习教程了 https://blog.csdn.net/sangewuxie/article/details/80958611 按其步骤,我下载的是Java 11 ...