-
Notifications
You must be signed in to change notification settings - Fork 60
Service Gateway with Zuul
For building up a service gateway with Zuul, there are several topics:
- Enable Zuul server
- Integrate Zuul server with Eureka server
- Map Routes to Downstream Services through Eureka server
- Configure Zuul server
dependencies {
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-zuul', version: '1.4.4.RELEASE'
}
server:
port: 5555 # Port for Zuul server
@SpringBootApplication
@EnableZuulProxy // Enables the service to be a Zuul server
public class ZuulServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulServerApplication.class, args);
}
}
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.
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.
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/
You can do several things by changing the configuration of the Zuul server:
- Customize routes to service
- Customize routes to URL endpoint
- Customize routes to a group of URL endpoints
- Exclude the auto-generated routes between the Eureka service ID and the registered service
- Add prefix to the route to a service
You can customize the route to a service. For example
zuul:
routes:
licensingservice: /abcd/**
After this change, you can reach the licensing service by the new route:
http://localhost:5555/abcd/**
You can customize the route to any URL endpoint. For example
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/**
You can manually configure Zuul to disable Ribbon integration with Eureka and then list the individual service instances that Ribbon will load balance against.
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.
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: '*'
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/**
- Overview
- Getting Started
-
Technical Essentials
- Autowired
- SpringData JPA
- Configuration File Auto-loading
- Configuration Encryption
- Service Discovery with Eureka
- Resiliency Patterns with Hystrix
- Configure Hystrix
- Service Gateway with Zuul
- Zuul Filters
- Protect Service with Spring Security and OAuth2
- Use JWT as Access Token
- Store Clients and Users' Credentials to DB
- Integrate with Message Queue (Kafka)
- Integrate with Redis
- Tune Logging
- Log Aggregation
- Send Trace to Zipkin
- Build Runnable Jar
- Core Application Logic
- Components