Spring cloud realizes service registration and discovery

  Service registration and discovery are very important for microservice systems. With service discovery and registration, you don't need to change the configuration file of service calls all day long, you only need to use the service identifier to access the service.

  This article belongs to the fourth of the "Learning Spring Cloud Series in 7 Days", focusing on service registration and discovery. The projects involved in this article are:

  The principle of the service registry manager is shown in the following figure:

        

  All servers and clients accessing the service need to connect to the registry manager (eureka server). The service will automatically register itself to the eureka server when it starts. Each service has a name, and this name will be registered with the eureka server. The party using the service only needs to use the name plus the method name to call the service.

  The service registration and discovery of Spring cloud is not only limited to eureka, but also supports Zookeeper and Consul. The default is eureka. Spring encapsulates eureka, making it very simple and easy to use. It only needs to add a line of code to traditional applications. This line of code is an annotation. We implement the service registration and discovery functions as follows.

  1) It is preferred to establish a eureka server

  Creating a spring cloud eureka server is similar to the previous configuration file server. You only need to create an empty maven project, introduce the relevant starter of spring boot, and then create a nearly empty execution class. The project is as follows:

 

  In the EurekaServer class we add the following code:

  @SpringBootApplication

  @EnableEurekaServer

  public class EurekaServer {

    public static void main(String[] args) {

    SpringApplication.run(EurekaServer.class, args);

      }

  }

  You can see that you only need to use the @EnableEurekaServer annotation to turn the application into an Eureka server. This is because spring boot encapsulates the Eureka Server, allowing you to embed it into the application and use it directly. As for the real EurekaServer, it is an open source project of Netflix and can also be downloaded and used separately.

  Use the following configuration in the application.properties configuration file:

  server.port=8761

  eureka.instance.hostname=localhost

  eureka.client.registerWithEureka=false

  eureka.client.fetchRegistry=false

  eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

  其中server.port配置eureka服务器端口号。Eureka的配置属性都在开源项目spring-cloud-netflix-master中定义(spring boot连文档都没有,只能看源码了),在这个项目中有两个类EurekaInstanceConfigBean 和EurekaClientConfigBean,分别含有eureka.instance和eureka.client相关属性的解释和定义。从中可以看到,registerWithEureka表示是否注册自身到eureka服务器,因为当前这个应用就是eureka服务器,没必要注册自身,所以这里是false。fetchRegistry表示是否从eureka服务器获取注册信息,同上,这里不需要。defaultZone就比较重要了,是设置eureka服务器所在的地址,查询服务和注册服务都需要依赖这个地址。

  做完这些后当然,还要改一下pom文件,增加eureka-server的starter即可:

  <dependency>

                            <groupId>org.springframework.cloud</groupId>

                            <artifactId>spring-cloud-starter-eureka-server</artifactId>

  </dependency>

  如此eureka服务器就完成了,在命令行启动就可以了。

  2)让服务使用eureka服务器

  让服务使用eureka服务器,只需添加@EnableDiscoveryClient注解就可以了。回到我们在上篇文章中实现的cloud-simple-service微服务应用。在main方法所在的Application类中,添加@EnableDiscoveryClient注解。然后在配置文件中添加:

  eureka.client.serviceUrl.defaultZone=http\://localhost\:8761/eureka/

  spring.application.name=cloud-simple-service

  其中defaultZone是指定eureka服务器的地址,无论是注册还是发现服务都需要这个地址。application.name是指定进行服务注册时该服务的名称。这个名称就是后面调用服务时的服务标识符(这是服务发现的功能,我们在后面章节具体介绍)。当然,pom文件也需要增加:

  <dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-eureka</artifactId>

  </dependency>

  如此以来该服务启动后会自动注册到eureka服务器。如果在该服务中还需要调用别的服务,那么直接使用那个服务的服务名称加方法名构成的url即可,具体我们将在下章结合ui端的应用具体介绍。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326448240&siteId=291194637