0
votes

I have below props in zuul gateway

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

Problem: How can i access my services : I want the end url to access my services to be like

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

But this is not working. instead i have to call

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

Note I don't want to change the RequestMapping at the services.

2
This example is helpful to you. Zuul proxy example - Ted Kim

2 Answers

0
votes

As you mentioned that you have common methods across services:

Try adjust your routing path as:

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

then map each different service per method:

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

I will show you gateway example.

1. Create your service project (user-service)

create application.properties file

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

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

2. Setting up Zuul project (Gateway-service)

1.@EnableZuulproxy to tell Spring Boot that this is a Zuul proxy

@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class GatewayServiceApplication {

2.create an application.properties file

# =======================================
# 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 website ( localhost:8761 )

enter image description here