Spring Cloud (a) Eureka Server- monomer and Cluster Setup

Introduce a .Enreka

Eureka is a service discovery frame Netflix development, is itself a REST-based services, the intermediate layer is mainly used for positioning operation AWS service domain to achieve load balancing and failover intermediate layer service purposes. SpringCloud integrate it in their subprojects spring-cloud-netflix in order to achieve SpringCloud service discovery.

Eureka consists of two components: Eureka Server and Eureka Client.

Eureka Server service registration services, after each node starts, it will be registered in Eureka Server, so EurekaServer in the service registry will store all information available information service node, service node can visually see in the interface .

Java Eureka Client is a client for interacting with the simplified Eureka Server, the client is also a built-in polling (round-robin) load load balancing algorithm.

After the application starts, will be sent to Eureka Server heartbeat, the default period is 30 seconds, if Eureka Server does not receive a node in a multiple heartbeat heartbeat period, Eureka Server service from the registry will put the service node remove (default 90 seconds).

Between Eureka Server synchronization is accomplished by way of copying data, Eureka also provides client-side caching mechanism, even if all of Eureka Server will hang up, the client still can use the information in the cache API consumption of other services. In summary, Eureka heartbeat checking, client caching mechanisms to ensure high system availability, flexibility and scalability.

The figure briefly describes the basic architecture of Eureka, consisting of three characters:

1、Eureka Server

  • Provide service registry and discovery

2、Service Provider

  • Service provider

  • The self-service registration to Eureka, so that the service consumer to find

3、Service Consumer

  • Consumer Services

  • Obtain a list of registered services from Eureka, thereby Consumer Services

II. Monomer building

Add depend in 1.pom

SpringCloud and SpringBoot version correspondence is as follows:

Add dependence:

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/>
    </parent>    
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
    </properties>
​
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
​
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2.Application startup class annotate

@EnableEurekaServer: This comment is related to the activation of automatic configuration Eureka Servier class org.springframework.cloud.netflix.eureka.server.EurekaServerAutoConfiguration

@SpringBootApplication
@EnableEurekaServer
public class Study01Application {
​
    public static void main(String[] args) {
        SpringApplication.run(Study01Application.class, args);
    }
}

3. Profiles

spring.application.name=spring-cloud-eureka
​
server.port=8000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
​
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
  • eureka.client.register-with-eureka : Indicates whether to register itself to Eureka Server, the default is true.

  • eureka.client.fetch-registry : Indicates whether to obtain registration information from Eureka Server, the default is true.

  • eureka.client.serviceUrl.defaultZone: Set interaction with Eureka Server address, tracking and registration services will need to rely on this address. Default HTTP: // localhost: 8761 / Eureka ; a plurality of addresses may be used, separated.

After starting the project, visit: HTTP: // localhost: 8000 / , you can see the following page

III. Cluster Setup

Sign up such a critical service center, if it is a single point, then experience a failure is devastating. In a distributed system, the service registry is the most important basic part, we should be ready to provide services in the state. In order to maintain its availability, use the cluster is a good solution. Eureka to achieve the deployment of highly available by registering with each other, so we only need to Eureke Server configuration other available serviceUrl can achieve high availability deployments.

1. The two-node registration

(1) create application-peer1.properties, as a configuration peer1 service center, and serviceUrl point peer2

spring.application.name=spring-cloud-eureka
server.port=8000
eureka.instance.hostname=peer1
​
eureka.client.serviceUrl.defaultZone=http://peer2:8001/eureka/

(2) create application-peer2.properties, as a configuration peer2 service center, and serviceUrl point peer1

spring.application.name=spring-cloud-eureka
server.port=8001
eureka.instance.hostname=peer2
​
eureka.client.serviceUrl.defaultZone=http://peer2:8000/eureka/

(3)启动服务加载不同配置文件

依次启动完成后,浏览器输入:http://localhost:8000/ 效果图如下:

根据图可以看出peer1的注册中心DS Replicas已经有了peer2的相关配置信息,并且出现在available-replicas中。我们手动停止peer2来观察,发现peer2就会移动到unavailable-replicas一栏中,表示peer2不可用。

2.多节点注册

在生产中我们可能需要三台或者大于三台的注册中心来保证服务的稳定性,配置的原理其实都一样,将注册中心分别指向其它的注册中心。这里只介绍三台集群的配置情况,其实和双节点的注册中心类似,每台注册中心分别又指向其它两个节点即可,使用application.yml来配置。

---
spring:
  application:
    name: spring-cloud-eureka
  profiles: peer1
server:
  port: 8000
eureka:
  instance:
    hostname: peer1
  client:
    serviceUrl:
      defaultZone: http://peer2:8001/eureka/,http://peer3:8002/eureka/
---
spring:
  application:
    name: spring-cloud-eureka
  profiles: peer2
server:
  port: 8001
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://peer1:8000/eureka/,http://peer3:8002/eureka/
---
spring:
  application:
    name: spring-cloud-eureka
  profiles: peer3
server:
  port: 8002
eureka:
  instance:
    hostname: peer3
  client:
    serviceUrl:
      defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/

分别以peer1、peer2、peer3的配置参数启动eureka注册中心。

依次启动完成后,浏览器输入:http://localhost:8000/ 效果图如下:

可以在peer1中看到了peer2、peer3的相关信息。至此eureka集群也已经完成了。

Guess you like

Origin blog.csdn.net/fy_java1995/article/details/94128673