服务注册与发现

服务注册与发现

服务注册与发现

Service Registration and Discovery

通过此教程,你将构建并消费Netflix Eureka服务中心。

This guide walks you through the process of standing up and consuming the Netflix Eureka service registry.

将要做什么

What you’ll build

将设置一个Netflix Eureka服务中心并建立一个客户端,用来向中心注册自己与解析自己。一个服务中心是非常有用的,因为它可以启用客户端负载均衡并在不适用DNS的情况下解耦服务提供者与消费者。

You’ll setup a Netflix Eureka service registry and then build a client that both registers itself with the registry and uses it to resolve its own host. A service registry is useful because it enables client-side load-balancing and decouples service providers from consumers without the need for DNS.

需要些什么

What you’ll need

如何完成此教程

How to complete this guide

与大多数Spring的入门教程相似,你可以从头开始并完成每一步,或者你也可以跳过你熟悉的设置步骤。无论怎样,你最终应当以有效代码结束。

Like most Spring Getting Started guides, you can start from scratch and complete each step, or you can bypass basic setup steps that are already familiar to you. Either way, you end up with working code.

如果从头开始,可参考使用Gradle构建

To start from scratch, move on to Build with Gradle.

如果要跳过基本步骤,按如下步骤:

To skip the basics, do the following:

  • 下载并解压此教程的源码仓库,或者使用Git进行clone:git clone https://github.com/spring-guides/gs-service-registration-and-discovery.git
  • 进入gs-service-registration-and-discovery/initial目录
  • 跳至建立一个Eureka服务中心.
  • Download and unzip the source repository for this guide, or clone it using Git: git clone https://github.com/spring-guides/gs-service-registration-and-discovery.git
  • cd into gs-service-registration-and-discovery/initial
  • Jump ahead to Stand up a Eureka Service Registry.

当你完成时,你可以与gs-service-registration-and-discovery/complete中的代码对比检查。

When you’re finished, you can check your results against the code in gs-service-registration-and-discovery/complete.

建立一个Eureka服务中心

Stand up a Eureka Service Registry

首先我们需要一个Eureka服务中心。可以使用Spring Cloud的@EnableEurekaServer来建立一个其他应用可以访问的中心。这是一个普通的Spring Boot应用,添加了一个注解来开启服务中心。

You’ll first need a Eureka Service registry. You can use Spring Cloud’s @EnableEurekaServer to stand up a registry that other applications can talk to. This is a regular Spring Boot application with one annotation added to enable the service registry.

eureka-service/src/main/java/hello/EurekaServiceApplication.java

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServiceApplication {

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

当注册中心启动时,会有堆栈日志,表示没有连接到注册中心的其他实例。在生产环境,可能需要更多的注册中心实例。但对于我们比较简单的需求,可以屏蔽相关的日志。

When the registry starts up it will complain, with a stacktrace, that there are no replica nodes for the registry to connect to. In a production environment, you will want more than one instance of the registry. For our simple purposes, however, it sufficies to disable the relevant logging.

默认注册中心也会注册自己,所以也需要屏蔽。

By default, the registry will also attempt to register itself, so you’ll need to disable that, as well.

在本地时,比较好的做法是将注册中心使用一个单独的端口。

It’s a good convention to put this registry on a separate port when using it locally.

eureka-service/src/main/resources/application.properties添加一些属性来控制这些需求。

Add some properties to your eureka-service/src/main/resources/application.properties to handle all of these requirements.

eureka-service/src/main/resources/application.properties

server.port=8761

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF

连接到注册中心

Talking to the Registry

我们已经建好了服务注册中心,现在需要构建一个客户端用来向注册中心注册自己,并使用Spring Cloud DiscoveryClient抽象来访问注册中心获取自己的主机与端口。@EnableDiscoveryClient触发了Netflix Eureka DiscoveryClient实现。也有其他的服务注册的实现,例如Hashicorp的ConsulApache Zookeeper

Now that we’ve stood up a service registry, let’s stand up a client that both registers itself with the registry and uses the Spring Cloud DiscoveryClient abstraction to interrogate the registry for it’s own host and port. The @EnableDiscoveryClient activates the Netflix Eureka DiscoveryClient implementation. There are other implementations for other service registries like Hashicorp’s Consul or Apache Zookeeper.

eureka-client/src/main/java/hello/EurekaClientApplication.java

package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {

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

@RestController
class ServiceInstanceRestController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/service-instances/{applicationName}")
    public List<ServiceInstance> serviceInstancesByApplicationName(
            @PathVariable String applicationName) {
        return this.discoveryClient.getInstances(applicationName);
    }
}

无论选择哪一种实现,你都可以看见eureka-client是以spring.application.name属性值为名称进行注册的。此属性在Spring Cloud中用途非常广泛,经常用于服务配置的最前期。此属性用于服务引导,按惯例是在eureka-client/src/main/resources/bootstrap.properties中并在src/main/resources/application.properties之前被找到。

Whatever implementation you choose, you’ll soon see the eureka-client registered under whatever name you specify in the spring.application.name property. This property is used a lot in Spring Cloud, often in the earliest phases of a service’s configuration. This property is used in service bootstrap and so by convention lives in eureka-client/src/main/resources/bootstrap.properties where it’s found before src/main/resources/application.properties.

eureka-client/src/main/resources/bootstrap.properties

spring.application.name=a-bootiful-client

eureka-client定义了一个Spring MVC REST端点,ServiceInstanceRestController,返回所有在http://localhost:8080/service-instances/a-bootiful-client注册的ServiceInstance实例的一个枚举。查看构建一个RESTful Web服务来学习更多关于使用Spring MVC和Spring Boot构建REST服务。

The eureka-client defines a Spring MVC REST endpoint, ServiceInstanceRestController, that returns an enumeration of all the ServiceInstance instances registered in the registry at http://localhost:8080/service-instances/a-bootiful-client. Consult the Building a RESTful Web Service guide to learn more about building REST services with Spring MVC and Spring Boot.

测试此应用

Test the application

先启动eureka-service,等完成后再启动eureka-clienteureka-client大约需要一分钟来向注册中心注册自己,并从注册中心中刷新自己的注册列表。所有的阈值都可配置。在浏览器中通过http://localhost:8080/service-instances/a-bootiful-client访问eureka-client。然后你可以看见表示响应的eureka-client中的ServiceInstance

Test the end-to-end result by starting the eureka-service first and then, once loaded, starting the eureka-client. The eureka-client will take about a minute to register itself in the registry and to refresh its own list of registered instances from the registry. All of these thresholds are configurable. Visit the eureka-client in the browser, http://localhost:8080/service-instances/a-bootiful-client. There, you should see the ServiceInstance for the eureka-client reflected in the response.

总结

Summary

恭喜!你完成了用Spring创建Netflix Eureka服务注册中心并用于一个客户端的注册。

Congratulations! You’ve just used Spring to stand up a Netflix Eureka service registry and to use that registry in a client application.

翻译部分版权归YahaCode团队所有。仅供学习研究之用,任何组织或个人不得私自以此用于任何形式的商业目的

发表评论