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