Springboot Gateway Zuul - PullRequest
       13

Springboot Gateway Zuul

0 голосов
/ 20 сентября 2018

У меня ниже реквизиты в шлюзе 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

Actual MicroService - Rest Controller @GetMapping ("/products / v1 / getAll ") getProducts ()

@ GetMapping (" / courses / v1 / getAll ") getCourses ()

Проблема: Как я могу получить доступ к своим услугам: я хочу, чтобы конецURL для доступа к моим службам будет выглядеть как

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 голосов
/ 01 октября 2018

Я покажу вам пример шлюза.

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 (Gateway-service)

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

0 голосов
/ 20 сентября 2018

Как вы упомянули, что у вас есть общие методы для разных служб:

Попробуйте настроить путь маршрутизации следующим образом:

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

, затем сопоставьте каждую отдельную службу для метода:

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