(四十) springcloud分布式商城之跟我学习SpringCloud-Gateway整合Eureka路由转发

SpringCloud关注者 2021-04-08 09:35:17
Spring Cloud Eureka Zuul yml


本节我们先创建一个 Gateway 项目,然后实现了一个最简单的转发功能,并进行 Eureka 路由的整合。

创建 Gateway 项目

创建一个 Spring Boot 的 Maven 项目,增加 Spring Cloud Gateway 的依赖,代码如下所示。

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath />
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>

启动类就按 Spring Boot 的方式即可,无须添加额外的注解。代码如下所示。

@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

路由转发示例

下面来实现一个最简单的转发功能——基于 Path 的匹配转发功能。

Gateway 的路由配置对 yml 文件支持比较好,我们在 resources 下建一个 application.yml 的文件,内容如下:

server:
port: 2001
spring:
cloud:
gateway:
routes:
- id: path_route
uri: http://c.biancheng.net
predicates:
- Path=/spring_cloud

当你访问 http://localhost:2001/spring_cloud 的时候就会转发到 http://c.biancheng.net/spring_cloud。

如果我们要支持多级 Path,配置方式跟 Zuul 中一样,在后面加上两个*号即可,比如:

- id: path_route2
uri: http://c.biancheng.net
predicates:
- Path=/spring_cloud/**

这样一来,上面的配置就可以支持多级 Path,比如访问 http://localhost:2001/spring_cloud/view/1 的时候就会转发到 http://c.biancheng.net/spring_cloud/view/1。

整合 Eureka 路由

添加 Eureka Client 的依赖,代码如下所示。

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

配置基于 Eureka 的路由:

- id: user-service
uri: lb://user-service
predicates:
- Path=/user-service/**

uri 以lb://开头(lb 代表从注册中心获取服务),后面接的就是你需要转发到的服务名称,这个服务名称必须跟 Eureka 中的对应,否则会找不到服务,错误代码如下:

org.springframework.cloud.gateway.support.NotFoundException: Unable to find instance for user-service1

整合 Eureka 的默认路由

Zuul 默认会为所有服务都进行转发操作,我们只需要在访问路径上指定要访问的服务即可,通过这种方式就不用为每个服务都去配置转发规则,当新加了服务的时候,不用去配置路由规则和重启网关。

在 Spring Cloud Gateway 中当然也有这样的功能,通过配置即可开启,配置如下:

spring:
cloud:
gateway:
discovery:
locator:
enabled: true

开启之后我们就可以通过地址去访问服务了,格式如下:

http://网关地址/服务名称(大写)/**
http://localhost:2001/USER-SERVICE/user/get?id=1

这个大写的名称还是有很大的影响,如果我们从 Zuul 升级到 Spring Cloud Gateway 的话意味着请求地址有改变,或者重新配置每个服务的路由地址,通过源码笔者发现可以做到兼容处理,再增加一个配置即可:

spring:
cloud:
gateway:
discovery:
locator:
lowerCaseServiceId: true

配置完成之后我们就可以通过小写的服务名称进行访问了,如下所示:

http://网关地址/服务名称(小写)/**
http://localhost:2001/user-service/user/get?id=1

注意:开启小写服务名称后大写的服务名称就不能使用,两者只能选其一。

配置源码在 org.springframework.cloud.gateway.discovery.DiscoveryLocatorProperties 类中,代码所示。

@ConfigurationProperties("spring.cloud.gateway.discovery.locator")
public class DiscoveryLocatorProperties {
/**
* 服务名称小写配置, 默认为false
*
*/
private boolean lowerCaseServiceId = false;
}

推荐电子商务源码

版权声明
本文为[SpringCloud关注者]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/3613013/blog/5011687

  1. How to realize encryption and decryption of spring boot interface parameters gracefully?
  2. A new way to play in spring 5! This kind of URL request makes me see better!
  3. Can parameters in spring MVC be passed like this? It's up!
  4. Hand in hand to teach you how to develop mybatis plug-ins
  5. Fine spring boot + thymeleaf, there are so many fun details!
  6. Spring boot logs all kinds of posture, it's time to clear!
  7. Web 3.0踏浪而来,分布式存储举足轻重|时空云邀请您参加Web3.0中国峰会暨分布式存储行业大会
  8. spring-aop 进不了切面方法的解决办法
  9. Web 3.0 is coming, distributed storage is very important | spatiotemporal cloud invites you to attend Web3.0 China Summit and distributed storage industry conference
  10. The solution of spring AOP can't enter the section method
  11. Linux中如何启用root用户
  12. How to enable root in Linux
  13. 踩坑 MySQL 索引,看看你真的会用吗?
  14. Hive优化之配置参数的优化(一)
  15. Step on the MySQL index to see if you really know how to use it?
  16. Optimization of configuration parameters for hive optimization (1)
  17. Linux入门教程资料分享
  18. Introduction to Linux
  19. 外部连接mysql docker容器异常
  20. Exception of external connection MySQL docker container
  21. Zookeeper分布式锁?
  22. Zookeeper distributed lock?
  23. 嵌入式Linux_Framebuffer_03点阵显示ASCII字符串
  24. 嵌入式Linux_Framebuffer_02字符编码
  25. Embedded Linux_ Framebuffer_ 03 dot matrix display ascii string
  26. Embedded Linux_ Framebuffer_ 02 character encoding
  27. Looking forward to new product launch of Xiaomi in spring CNMO takes you to see 11 new products in advance
  28. An inventory of the commonly used garbage collectors in Java
  29. Why is it so easy to get started with HBase?
  30. Implementation of PRC framework based on netty
  31. 2021 Java back end engineer must know knowledge - (Dubbo, distributed RPC framework)
  32. 关于spring advisor和元数据 同时来管理事务的问题
  33. How to manage transactions with spring advisor and metadata at the same time
  34. 使用Playwright对Java API实现自动视觉测试 - applitools
  35. Using playwright to implement automatic visual testing for Java API - applitools
  36. Dubbo和Spring cloud、Istio对比图
  37. Comparison of Dubbo with spring cloud and istio
  38. HttpServletRequest、通过request获得请求头、请求体等、解决中文乱码等问题
  39. Mybatis学习笔记-一对一,一对多,多对多关联查询
  40. Mybatis学习笔记-基本概念与操作
  41. HttpServletRequest, obtaining request header and request body through request, solving Chinese garbled code and other problems
  42. Mybatis learning notes - one to one, one to many, many to many association query
  43. Mybatis learning notes - basic concepts and operation
  44. Spring Cloud 升级之路 - 2020.0.x - 3. Undertow 的 accesslog 配置
  45. Spring cloud upgrade road - 2020.0. X - 3. Accesslog configuration of undertow
  46. 被Java培训机构坑骗后,我在这里找回了自信
  47. After being cheated by java training institutions, I found my confidence here
  48. Linux下安装Mysql出现的常见问题以及解决办法
  49. Common problems and solutions of installing MySQL under Linux
  50. java并发编程JUC第十二篇:AtomicInteger原子整型
  51. Java Concurrent Programming JUC Chapter 12: atomicinteger atomic integer
  52. 面经手册 · 第29篇《Spring IOC 特性有哪些,不会读不懂源码!》
  53. Chapter 29 "what are the features of spring IOC? I can't understand the source code! 》
  54. 浅析linux容器--Docker
  55. Analysis of Linux container -- docker
  56. 换种方法学操作系统,轻松入门Linux内核
  57. 浅析linux容器--Docker
  58. Another way to learn operating system, easy access to Linux kernel
  59. Analysis of Linux container -- docker
  60. 手摸手教你阅读和调试大型开源项目 ZooKeeper