An Example of Load Balancing with Zuul and Eureka – 用Zuul和Eureka进行负载平衡的例子

最后修改: 2018年 1月 24日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this article, we’ll look at how load balancing works with Zuul and Eureka.

在这篇文章中,我们将看看负载均衡如何在Zuul和Eureka中工作。

We’ll route requests to a REST Service discovered by Spring Cloud Eureka through Zuul Proxy.

我们将通过Zuul代理将请求路由到由Spring Cloud Eureka发现的REST服务

2. Initial Setup

2.初始设置

We need to set up Eureka server/client as shown in the article Spring Cloud Netflix-Eureka.

我们需要设置Eureka服务器/客户端,如文章Spring Cloud Netflix-Eureka中所示。

3. Configuring Zuul

3.配置Zuul

Zuul, among many other things, fetches from Eureka service locations and does server-side load balancing.

Zuul,在许多其他方面,从Eureka服务地点取货,并做服务器端的负载平衡。

3.1. Maven Configuration

3.1.Maven配置

First, we’ll add Zuul Server and Eureka dependency to our pom.xml:

首先,我们将把Zuul服务器Eureka依赖性添加到我们的pom.xml:中。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

3.2. Communication With Eureka

3.2.与尤里卡的沟通

Secondly, we’ll add the necessary properties in Zuul’s application.properties file:

其次,我们将在Zuul的application.properties文件中添加必要的属性。

server.port=8762
spring.application.name=zuul-server
eureka.instance.preferIpAddress=true
eureka.client.registerWithEureka=true
eureka.client.fetchRegistry=true
eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://localhost:8761/eureka}

Here we’re telling Zuul to register itself as a service in Eureka and to run on port 8762.

这里我们要告诉Zuul把自己注册为Eureka的一个服务,并在8762端口运行。

Next, we’ll implement the main class with @EnableZuulProxy and @EnableDiscoveryClient. @EnableZuulProxy indicates this as Zuul Server and @EnableDiscoveryClient indicates this as Eureka Client:

接下来,我们将用@EnableZuulProxy和@EnableDiscoveryClient来实现主类。@EnableZuulProxy表示这是Zuul服务器,@EnableDiscoveryClient表示这是Eureka客户端。

@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class ZuulConfig {
    public static void main(String[] args) {
        SpringApplication.run(ZuulConfig.class, args);
    }
}

We’ll point our browser to http://localhost:8762/routes. This should show up all the routes available for Zuul that are discovered by Eureka:

我们将把浏览器指向http://localhost:8762/routes。这应该会显示出Eureka发现的所有可用于Zuul的路线

{"/spring-cloud-eureka-client/**":"spring-cloud-eureka-client"}

Now, we’ll communicate with Eureka client using Zuul Proxy route obtained. Pointing our browser to http://localhost:8762/spring-cloud-eureka-client/greeting should generate the response something like:

现在,我们将使用获得的Zuul代理路由与Eureka客户端通信。将我们的浏览器指向http://localhost:8762/spring-cloud-eureka-client/greeting应该会产生类似的响应。

Hello from 'SPRING-CLOUD-EUREKA-CLIENT with Port Number 8081'!

4. Load Balancing with Zuul

4.用Zuul进行负载平衡

When Zuul receives a request, it picks up one of the physical locations available and forwards requests to the actual service instance. The whole process of caching the location of the service instances and forwarding the request to the actual location is provided out of the box with no additional configurations needed.

当Zuul收到一个请求时,它会选择一个可用的物理位置,并将请求转发给实际的服务实例。缓存服务实例的位置并将请求转发给实际位置的整个过程是开箱即用的,不需要额外的配置。

Here, we can see how Zuul is encapsulating three different instances of the same service:

在这里,我们可以看到Zuul是如何封装同一服务的三个不同实例的。

Zuul 1

Internally, Zuul uses Netflix Ribbon to look up for all instances of the service from the service discovery (Eureka Server).

在内部,Zuul使用Netflix Ribbon从服务发现(Eureka服务器)查找所有的服务实例。

Let’s observe this behavior when multiple instances are brought up.

让我们观察一下多个实例被提出来时的这种行为。

4.1. Registering Multiple Instances

4.1.注册多个实例

We’ll start by running two instances (8081 and 8082 ports).

我们将首先运行两个实例(8081和8082端口)。

Once all the instances are up, we can observe in logs that physical locations of the instances are registered in DynamicServerListLoadBalancer and the route is mapped to Zuul Controller which takes care of forwarding requests to the actual instance:

一旦所有的实例建立起来,我们可以在日志中观察到实例的物理位置被注册在DynamicServerListLoadBalancer中,并且路由被映射到Zuul Controller,它负责将请求转发给实际的实例。

Mapped URL path [/spring-cloud-eureka-client/**] onto handler of type [class org.springframework.cloud.netflix.zuul.web.ZuulController]
Client:spring-cloud-eureka-client instantiated a LoadBalancer:
  DynamicServerListLoadBalancer:{NFLoadBalancer:name=spring-cloud-eureka-client,
  current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
Using serverListUpdater PollingServerListUpdater
DynamicServerListLoadBalancer for client spring-cloud-eureka-client initialized: 
  DynamicServerListLoadBalancer:{NFLoadBalancer:name=spring-cloud-eureka-client,
  current list of Servers=[0.0.0.0:8081, 0.0.0.0:8082],
  Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone;	Instance count:2;	
  Active connections count: 0;	Circuit breaker tripped count: 0;	
  Active connections per server: 0.0;]},
  Server stats: 
    [[Server:0.0.0.0:8080;	Zone:defaultZone;......],
    [Server:0.0.0.0:8081;	Zone:defaultZone; ......],

Note: logs were formatted for better readability.

注:为了提高可读性,对日志进行了格式化。

4.2. Load-Balancing Example

4.2.负载平衡实例

Let’s navigate our browser to http://localhost:8762/spring-cloud-eureka-client/greeting a few times.

让我们把浏览器导航到http://localhost:8762/spring-cloud-eureka-client/greeting,多浏览几次。

Each time, we should see a slightly different result:

每一次,我们都应该看到一个稍微不同的结果。

Hello from 'SPRING-CLOUD-EUREKA-CLIENT with Port Number 8081'!
Hello from 'SPRING-CLOUD-EUREKA-CLIENT with Port Number 8082'!
Hello from 'SPRING-CLOUD-EUREKA-CLIENT with Port Number 8081'!

Each request received by Zuul is forwarded to a different instance in a round robin fashion.

Zuul收到的每个请求都会以轮回方式转发到不同的实例。

If we start another instance and register it in Eureka, Zuul will register it automatically and start forwarding requests to it:

如果我们启动另一个实例并在Eureka中注册,Zuul会自动注册并开始向它转发请求。

Hello from 'SPRING-CLOUD-EUREKA-CLIENT with Port Number 8083'!

We can also change Zuul’s load-balancing strategy to any other Netflix Ribbon strategy – more about this can be found in our Ribbon article.

我们还可以将Zuul的负载平衡策略改为任何其他Netflix Ribbon策略–关于这一点,可以在我们的Ribbon 文章中找到。

5. Conclusion

5.结论

As we’ve seen, Zuul provides a single URL for all the instances of the Rest Service and does load balancing to forward the requests to one of the instances in round robin fashion.

正如我们所看到的,Zuul为Rest Service的所有实例提供了一个单一的URL,并做了负载平衡,以循环方式将请求转发给其中一个实例。

As always, the complete code for this article can be found over on GitHub.

像往常一样,本文的完整代码可以在GitHub上找到over