Springboot Gateway Zuul

时间:2018-09-19 22:06:29

标签: spring-boot netflix-zuul

我在zuul网关中有以下道具

Application.properties

zuul.routes.product.path = /api/products/**
zuul.routes.product.service-id=dotcom-ms-products
zuul.routes.product.strip-prefix=true

zuul.routes.course.path=/api/courses/**
zuul.routes.course.service-id=dotcom-ms-course-service
zuul.routes.course.strip-prefix=true

实际的MicroService-休息控制器 @GetMapping(“ / products / v1 / getAll”) getProducts()

@GetMapping(“ / courses / v1 / getAll”) getCourses()

问题: 我该如何访问我的服务:我希望结束网址访问我的服务就像

localhost/api/products/v1/getAll
localhost/api/courses/v1/getAll

但这不起作用。相反,我必须致电

localhost/api/products/products/v1/getAll
localhost/api/courses/courses/v1/getAll

请注意,我不想在服务处更改RequestMapping。

2 个答案:

答案 0 :(得分:0)

正如您所提到的,跨服务具有通用方法:

尝试将您的路由路径调整为:

zuul.routes.product.path = /api/**
. . .
zuul.routes.course.path=/api/**

然后按方法映射每个不同的服务:

@GetMapping("/products/v1/getAll")
. . .
@GetMapping("/courses/v1/getAll")

答案 1 :(得分:0)

我将向您展示网关示例。

1。创建您的服务项目(用户服务)

创建application.properties文件

# --- Spring Config
spring:
  application:
    name: OVND-USER-SERVICE

# Eureka client
eureka:
  client:
    serviceUrl:
      defaultZone: ${EUREKA_URL:http://localhost:8761/eureka/}

2。设置Zuul项目(网关服务)

1. @ EnableZuulproxy告诉Spring Boot这是一个Zuul代理

@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class GatewayServiceApplication {

2。创建一个application.properties文件

# =======================================
# Gateway-service Server Configuration
# =======================================

# --- Spring Config
spring:
  application:
    name: gateway-service

server:
  port: ${PORT:8080}

# Eureka client
eureka:
  client:
    serviceUrl:
      defaultZone: ${EUREKA_URL:http://localhost:8761/eureka/}

zuul:
  host:
  routes:
    ## By default, all requests to user service for example will start with: "/user/"
    ## What will be sent to the user service is what comes after the path defined,
    ## So, if request is "/user/v1/user/tedkim", user service will get "/v1/user/tedkim".
    user-service:
      path: /user/**
      service-id: OVND-USER-SERVICE
    another-service:
      path: /another/**
      service-id: OVND-ANOTHER-SERVICE

Eureka网站(localhost:8761)

enter image description here