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/

http://blueskykong.com/

猿天地:http://cxytiandi.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

最新文章

  1. Exception in thread &quot;main&quot; java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.&lt;init&gt;(I)V
  2. 在v-for中利用index来对第一项添加class(vue2.0)
  3. Java远程方法调用(Remote Method Invocation,RMI)
  4. 彻底解决phpcms v9升级后,文章发布出现: Mysql 1267错误:MySQL Error : Illegal mix of collations 解决办法
  5. idea 14 svn安装
  6. 1 Yoga3 系统装机总结.
  7. OpenCV——写手势识别碰到的各种错误
  8. Red Gate系列之八 SQL Connect 1.1.1.19 Edition 数据库连接及操作工具 完全破解+使用教程
  9. Delphi中ShellExecute的妙用
  10. .NET Core脚本工具dotnet-script
  11. 论文选读二:Multi-Passage Machine Reading Comprehension with Cross-Passage Answer Verification
  12. Mongodb Mysql NoSQL的区别和联系
  13. npm 遇到的坑
  14. Swagger Annotation 详解(建议收藏)
  15. solidity fallback函数
  16. C语言——第三次作业(2)
  17. 世界各个地区WIFI 2.4G及5G信道划分表(附无线通信频率分配表)
  18. SQL Server 2008 附加数据库之后显示为 只读 的解决方法
  19. Jmeter中的XPath Assertion
  20. C# Panel控件截图

热门文章

  1. &lt;4&gt;Lua表
  2. python 文件写入错误
  3. IOS 开发体验测试问题
  4. python pprint
  5. 设计模式之Proxy(代理)(转)
  6. USB接口案例——多态和转型
  7. python练习题-打印斐波拉契数列前n项
  8. Linux基础命令---文本编辑sed
  9. Shell for while 循环
  10. POJ 2492 A Bug&#39;s Life (并查集)