Skip to content

Service Gateway with Zuul

Wuyi Chen edited this page Jul 3, 2019 · 23 revisions

Overview

For building up a service gateway with Zuul, there are several topics:


Enable Zuul server

build.gradle

dependencies {
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-zuul', version: '1.4.4.RELEASE'
}

application.yml

server:
  port: 5555                                         # Port for Zuul server

ZuulServerApplication.java

@SpringBootApplication
@EnableZuulProxy                                  // Enables the service to be a Zuul server
public class ZuulServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZuulServerApplication.class, args);
    }
}

Integrate Zuul server with Eureka server

You can integrate the Zuul server with the Eureka server so that the Zuul server can use the Eureka server as the lookup reference of downstream services.

application.yml

eureka:                                              # Let Zuul server talk to Eureka server
  instance:
    preferIpAddress: true
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
        defaultZone: http://localhost:8761/eureka/

In this case, before starting the Zuul server, please start the Eureka server first.


Map Routes to Downstream Services through Eureka server

After integrating the Zuul server with the Eureka server, the Zuul server can look up all the registered services of the Eureka server and use Eureka service ID (spring.application.name) to route incoming calls to any registered service automatically.

The pattern for calling a registered service through the Zuul server's single URL:

http://localhost:5555/{Service_ID}/{Rest_Of_Path_For_Service}

For example, if you want to call the licensing service for the method of getting licenses by organization ID, there are 2 ways to call:

Call Directly: http://localhost:8080/v1/organizations/e254f8c-c442-4ebe-a82a-e2fc1d1ff78a/licenses/
Call By Zuul:  http://localhost:5555/licensingservice/v1/organizations/e254f8c-c442-4ebe-a82a-e2fc1d1ff78a/licenses/

Configure Zuul server

You can do several things by changing the configuration of the Zuul server:

Customize routes to service

You can customize the route to a service. For example

application.yml

zuul:
  routes:
    licensingservice: /abcd/**

After this change, you can reach the licensing service by the new route:

http://localhost:5555/abcd/**

Customize routes to URL endpoint

You can customize the route to any URL endpoint. For example

application.yml

zuul:
  routes:
    licensestatic:
      path: /abcd/**
      url: http://localhost:8080/         // this is the URL for the licensing service, but it can be any URL for other cases

After this change, you can reach the licensing service by the route:

http://localhost:5555/abcd/**

Customize routes to a group of URL endpoints

You can manually configure Zuul to disable Ribbon integration with Eureka and then list the individual service instances that Ribbon will load balance against.

application.yml

zuul:
  routes:
    licensestatic:
      path: /licensestatic/**
      serviceId: licensestatic                          # Defines a service ID that will be used to look up the service in Ribbon

ribbon:
  eureka:
    enabled: false                                      # Disables Eureka support in Ribbon

licensestatic:
  ribbon:
    listOfServers: http://licenseservice-static1:8081,  # List of servers used to route the request to
                   http://licenseservice-static1:8082,
                   http://licenseservice-static1:8083,
                   http://licenseservice-static1:8084

After this change, the service call to the endpoint /licensestatic/** will be routed into one of those 4 instances.

Exclude the auto-generated routes between the Eureka service ID and the registered service

You can exclude a certain route by Eureka service ID.

application.yml

zuul:
  ignored-services: 'organizationservice'

You can exclude all Eureka-based routes. application.yml

zuul:
  ignored-services: '*'

Add prefix to the route to a service

You can add a prefix to a route to a service. This is a way to differentiate API routes vs. content routes. So in this case, you can prefix all the API routes with a type of label such as /api.

application.yml

zuul:
  prefix: /api

After this change, you can reach the licensing service by the route:

http://localhost:5555/api/licensingservice/**
Clone this wiki locally