Assistant Engineer
Assistant Engineer
  • UID627
  • Fans2
  • Follows0
  • Posts55

Leveraging Alibaba Cloud Container Service to develop Spring Cloud Microservice Application (3) 

More Posted time:Jan 5, 2017 15:31 PM
Summary: This article throws light on how to leverage Spring Cloud for Zuul to support service intelligent routing and balancing of outgoing loads using Alibaba Cloud Server Load Balancer (SLB).

Use Zuul to Build a Simple API Gateway
Carrying out a specific function on the mobile client may require calling various background services. However, if all calls are made on the mobile client, the sheer number of calls may cause delay in the communication time. Additionally, due to the limited mobile battery capacity, it is generally recommended to not make too many calls to background services.
One practical approach is to converge all related services and create one new service. This new service acts as an entry point. Here, only one call needs to be made to the frontend application instead of multiple calls, as earlier. Moreover, service calls can be processed in the new service to ease the load on frontend processing. Related codes are located in foobar.
Spring Cloud Zuul can also build an API Gateway using foobar and other applications that need to provide outgoing services. URL patterns and other information can be declared about outgoing services through the configuration files for Zuul: commons/gateway.
Zuul is a multi-instance service and requires load balancing. A Server Load Balancer (SLB) can be defined on Alibaba Cloud to manage outgoing services and balance loads for Zuul. The template declaration for related deployment is included in docker-compose.yml.
Introduce Zuul Dependency in build.gradle
dependencies {

### Annotation:
 add ```@EnableZuulProxy``` to the primary class, and declare that the application relies on Zuul for service routing; add the ```@EnableDiscoveryClient``` annotation to indicate that a service discovery client is embedded in the application. Eureka is introduced in ```build.gradle```, so the client can automatically discover services through Eureka.
public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);

Define Access to Services in application.yml
Application.yml defines that gateway listens to the fixed port 8080, while zuul.routes.foobar.path defines that all traffic to the current host /acs/*** will be directed to foobar services.
Zuul can discover all foobar services through Eureka, whose configuration is same.
  port: 8080


      path: /acs/**

Use Alibaba Cloud Server Load Balancer for Load Balancing

The goal to balance the server load can be achieved by using Alibaba Cloud Server Load Balancer. Alibaba Cloud Container Service offers a tag extension for the Docker Compose template and declares that the service performs load balancing through Server Load Balancer. For a more specific approach, refer to the ```docker-compose.yml``` document:
    image: xxxx
      aliyun.scale: "2"
      aliyun.lb.port_8080: http://${slbname}:8080

Alibaba Cloud Container Service will resolve all tags starting with aliyun and perform different operations based on the tag definition. Aliyun.scale indicates that two instances are activated.
```8080``` the last section of the URL denotes the outbound port number of Server Load Balancer, the same as above. Refer to the official Help Documentation of Container Service, which details how to use Docker Compose for tag extension at [Alibaba Cloud Container Service Help Documentation]

This article was based on how to leverage Spring Cloud for Zuul to support service intelligent routing and balancing of outgoing loads using Alibaba Cloud Server Load Balancer (SLB).