simbro
simbro

Reputation: 3692

Auto-configure routes with Zuul and Eureka

Through reading various books / tutorials, it appears that it is possible to auto-configure routes in Zuul when using it in combination with Eureka service discovery. That means that I don't have to explicitly add routes to Zuul's application.properties.

So I understand this correctly? Or do I still need to add routes explicitly to Zuul in order it to work as a gateway?

I would like it to automatically create routes from the application name's that are registered with Eureka. Is this possible?

(Note: I have actually tried this, but when I go to http://localhost:8762/routes I just get an error page.)

Upvotes: 2

Views: 3921

Answers (2)

Ted Kim
Ted Kim

Reputation: 201

Sure. In most microservices implementations, internal microservices endpoints are not exposed outside. A set of public services will be exposed to the clients using an API gateway.

enter image description here

The zuul proxy internally uses the Eureka Server for service discovery.

  1. I would like it to automatically create routes from the application name's that are registered with Eureka. Is this possible?

Sure. 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

Upvotes: 6

S.K.
S.K.

Reputation: 3697

Yes. You can integrate Zuul with Eureka and configure the routes based on application names registered in Eureka. Just add the following configuration to Zuul application:

zuul:
  ignoredServices: "*"
  routes:
    a-service: /a-service/**
    b-service: /b-service/**
    c-service: /c-service/**
    d-service: /d-service/**

Upvotes: 1

Related Questions