当前位置:   article > 正文

SpringCloudGateWay+SpringCloudAlibabaNacos讲解_引入spring-cloud-starter-gateway时必需要加nacos依赖吗

引入spring-cloud-starter-gateway时必需要加nacos依赖吗

SpringCloud官方地址 Spring Cloud

源码地址:https://gitee.com/datadogapache/dashboard/projects

一.添加Nacos依赖:

  

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  4. <version>0.2.2.RELEASE</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.alibaba.cloud</groupId>
  8. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  9. <version>2.2.1.RELEASE</version>
  10. </dependency>

二.添加GateWay依赖:

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-gateway</artifactId>
  4. <exclusions>
  5. <exclusion>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-web</artifactId>
  8. </exclusion>
  9. <exclusion>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-webflux</artifactId>
  12. </exclusion>
  13. </exclusions>
  14. </dependency>

注意:坑!坑!坑!

springcloudgateway的底层实现是基于webflux,而webflux和传统的webmvc冲突,所以当项目中存在spring-boot-starter-web依赖时,需排除webmvc,且在spring-cloud-starter-gateway依赖中排除spring-boot-starter-web和spring-boot-starter-webflux,单独引入spring-boot-starter-webflux依赖替代webmvc,pom.xml中完整的依赖如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.3.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>springcloudgateway</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springcloudgateway</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring.cloud.version>Hoxton.RELEASE</spring.cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. <exclusions>
  25. <exclusion>
  26. <groupId>org.springframework</groupId>
  27. <artifactId>spring-webmvc</artifactId>
  28. </exclusion>
  29. <exclusion>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-tomcat</artifactId>
  32. </exclusion>
  33. </exclusions>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-webflux</artifactId>
  38. </dependency>
  39. <!-- <dependency>
  40. <groupId>io.projectreactor.netty</groupId>
  41. <artifactId>reactor-netty</artifactId>
  42. <version>0.9.2.RELEASE</version>
  43. </dependency>-->
  44. <dependency>
  45. <groupId>org.springframework.cloud</groupId>
  46. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  47. <version>0.2.2.RELEASE</version>
  48. </dependency>
  49. <dependency>
  50. <groupId>com.alibaba.cloud</groupId>
  51. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  52. <version>2.2.1.RELEASE</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework.cloud</groupId>
  56. <artifactId>spring-cloud-starter-gateway</artifactId>
  57. <exclusions>
  58. <exclusion>
  59. <groupId>org.springframework.boot</groupId>
  60. <artifactId>spring-boot-starter-web</artifactId>
  61. </exclusion>
  62. <exclusion>
  63. <groupId>org.springframework.boot</groupId>
  64. <artifactId>spring-boot-starter-webflux</artifactId>
  65. </exclusion>
  66. </exclusions>
  67. </dependency>
  68. <dependency>
  69. <groupId>org.projectlombok</groupId>
  70. <artifactId>lombok</artifactId>
  71. <optional>true</optional>
  72. </dependency>
  73. <dependency>
  74. <groupId>org.springframework.boot</groupId>
  75. <artifactId>spring-boot-starter-test</artifactId>
  76. <scope>test</scope>
  77. </dependency>
  78. </dependencies>
  79. <dependencyManagement>
  80. <dependencies>
  81. <dependency>
  82. <groupId>org.springframework.cloud</groupId>
  83. <artifactId>spring-cloud-dependencies</artifactId>
  84. <version>${spring.cloud.version}</version>
  85. <type>pom</type>
  86. <scope>import</scope>
  87. </dependency>
  88. </dependencies>
  89. </dependencyManagement>
  90. <build>
  91. <plugins>
  92. <plugin>
  93. <groupId>org.springframework.boot</groupId>
  94. <artifactId>spring-boot-maven-plugin</artifactId>
  95. <configuration>
  96. <excludes>
  97. <exclude>
  98. <groupId>org.projectlombok</groupId>
  99. <artifactId>lombok</artifactId>
  100. </exclude>
  101. </excludes>
  102. </configuration>
  103. </plugin>
  104. </plugins>
  105. </build>
  106. </project>

最后也是最重要的一点就是!!!

1.springboot的版本和springcloud的版本对应一定要一致,否者项目将报各种错误。spring官网中有其对应的版本,上面代码中spingboot的版本为2.3.3.RELEASE,springcloud的版本为Hoxton.RELEASE.

2.因为webflux基于netty,所以还需在spring-boot-starter-web中排除tomcat依赖:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. <exclusions>
  5. <exclusion>
  6. <groupId>org.springframework</groupId>
  7. <artifactId>spring-webmvc</artifactId>
  8. </exclusion>
  9. <exclusion>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-tomcat</artifactId>
  12. </exclusion>
  13. </exclusions>
  14. </dependency>

三.配置bootstrap.yml

  1. spring:
  2. application:
  3. name: gateway
  4. profiles:
  5. active: dev
  6. cloud:
  7. nacos:
  8. config:
  9. server-addr: 192.168.91.1:8848
  10. file-extension: yml
  11. discovery:
  12. server-addr: 192.168.91.1:8848
  13. gateway:
  14. httpclient:
  15. connect-timeout: 3000
  16. response-timeout: 10s
  17. routes:
  18. - id: test
  19. uri: lb://gateway
  20. predicates:
  21. - Path=/345/123
  22. filters:
  23. - StripPrefix=1

各字段含义如下:

id:我们自定义的路由 ID,保持唯一

uri:目标服务地址,lb为自定义nacos中的服务

predicates:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。

filters:过滤器,有多种过滤条件,其中StripPrefix为跳过路径数。例如:访问localhost:8090/test/api/home,当StripPrefix=2时,/test/api被跳过,直接访问localhost:8090/home

上面这段配置的意思是,配置了一个 id 为test的URI代理规则,路由的规则为:

当访问地址http://localhost:80890/345/123时,

会路由到上游地址http://localhost:80890/123。

四.建立controller测试路径

  1. package com.example.springcloudgateway.controller;
  2. import org.springframework.web.bind.annotation.CrossOrigin;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestMethod;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. @CrossOrigin
  8. public class gatecontroller {
  9. @RequestMapping(value = "/123",method = RequestMethod.GET)
  10. public String tsetGate() {
  11. return "welcome to my house!";
  12. }
  13. }

浏览器中输入localhost:8090/345/123

 当输入localhost:8090/123时,由于网关没有断言(predicates)设置路由规则为真,即可直接访问

 完毕!后期还会更更多关于网关的知识点,特推荐一篇关于SpringCloud gateway的博文:SpringCloud gateway (史上最全) - 疯狂创客圈 - 博客园 (cnblogs.com)

希望多多交流指正!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/301521
推荐阅读
相关标签
  

闽ICP备14008679号