Skip to content

tzzs/gatewayEurekaDemo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

具体实现可参考代码

本文档中简单描述了如何使用 Spring Cloud Gateway 组件自动路由到 Eureka 注册中心上

以下为本次Demo中主要涉及的微服务模块:

  1. 一个Gateway网关:实现前端路由的自动转发
  2. 一个Eureka注册中心:实现多服务的注册
  3. 两个接口提供模块:实现对应的接口功能,并通过Eureka Client 自动注册到 Eureka Server
graph LR

Gateway --> EurekaServer
EurekaServer --> Service1
EurekaServer --> Service2

Eureka Server 模块

此模块中仅提供单一的注册中心功能,配置如下:

spring:
  application:
    name: eurekaserver
server:
  port: 9092

eureka:
  client:
    service-url:
      defaultZone: http://localhost:${server.port}/eureka/
    fetch-registry: false
    register-with-eureka: false

Service 模块

在此微服务模块中,仅使用 Spring BootEureka Client实现对应接口的创建与注册。 ​

在两个 Service模块中分别提供一个接口,返回一个字符串 This is service1.This is service2.。 两个模块启动后可通过访问 http://localhost:9093/http://localhost:9094/测试是否正常启动。 ​

# application.yml  service1
server:
  port: 9093

eureka:
  client:
    service-url:
      defaultZone: http://localhost:9092/eureka
spring:
  application:
    name: service1

Spring Cloud Gateway 模块

参考文档

通过网关的路由功能,可以自动实现路由的转发功能,同时 Spring Cloud Gateway可以直接从 Eureka Server上获取注册模块,可以直接使用 lb://${application_name} (下面指定 uri 时会用到)完成对应多服务的转发。 ​

以下为Spring Cloud Gateway的部分配置: ​

predicates为路由的匹配配置: 即符合 predicates 下的条件才会路由到 uri 的地址。Path为路由匹配模式,路由前缀符合 /api/service 时才会满足条件(eg: http://localhost:9091/api/service2/get ). ​

filters 为路由的过滤器配置: 下面主要涉及两个配置, PreFixPath的配置的作用是,当请求路由满足 predicates 的条件时,路由转发时会在转发的路由前增加 /add路径,即请求路由为 http://localhost:9091/api/service2 转发路由为 http://localhost:9094/add/api/service2RewritePath的作用是满足条件时进行路由的重写,/api/service2/?(?<segment>.*), /$\{segment} 此正则的意思是把匹配路径中的 /api/service2去掉,即请求路由为 http://localhost:9091/api/service2/ 时,转发路由为 http://localhost:9094/

spring:
  application:
    name: gateway
  cloud:
    gateway:
      routes:
        - id: service
          uri: lb://SERVICE1
          predicates:
            - Path=/test/,/api/service1
          filters:
            - RewritePath=/api/service1/?(?<segment>.*), /$\{segment}

        - id: service2
          uri: http://localhost:9094
          predicates:
            - Path=/api/service2
          filters:
            #            - PrefixPath=/add
            - RewritePath=/api/service2/?(?<segment>.*), /$\{segment}
#      discovery:
#        locator:
#          enabled: true
server:
  port: 9091

eureka:
  client:
    service-url:
      defaultZone: http://localhost:9092/eureka/

参考文档

  1. Spring Cloud Gateway 配置

https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gateway-request-predicates-factories https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gatewayfilter-factories

About

This is a demo for how to use Spring Cloud Gateway and Eureka in project.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages