Spring-Cloud learning Eureka Cluster Setup

A. Why do we need a cluster

An article explains how to set up a single node eureka, eureka This explains how to build a cluster, where the cluster or local port to perform three different eureka, because the conditions do not permit, not three computers, so we will look at it the aim is to prevent a eureka cluster point of failure cause, paralysis of the entire service issues, as a single point of failure, because a point problem, there is another point to go to the top, instead of the point of work, it also achieved high availability cluster, high performance

II. Cluster principle

Each registration point is configured with url other points, it is possible to synchronize data with other server point, when registering to a service point, the store will be synchronized to the service to other registered points, when consumers need to consume asks a specific service to the registry, the registry will provide access to the address of the service provider to provide the specific services to consumers and finally realize calling services
Here Insert Picture Description

III. Creating win configure a cluster environment

Here it is convenient to all services directly to soak in the local win10 environment had truly simulated environment to run on linux virtual machine, we can own Baidu, the principles are the same, but run on linux needs to be packaged release, I will direct the operations on the win. First run multiple services configured at the local, domain mapping, in C: \ Windows \ System32 \ hosts file in the drivers \ etc Path added:
127.0.0.1 eureka3000.com
127.0.0.1 eureka3001.com
127.0.0.1 eureka3002.com
here need to be reminded, modify the hosts file is privileged, set specific permissions reference win10 modify the configuration file

IV. Create a cluster

  • First create a directory are as follows: Create eureka3000 in the article already mentioned (refer to build Eureka single node ), create eureka3001 and eureka3001 is the same, here pasted directly at the pom files

Here Insert Picture Description

  • eureka3000 the pom file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.org.ldc</groupId>
    <artifactId>eureka3000</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka3000</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!-- 继承本项目的父工程 -->
    <parent>
        <groupId>com.org.ldc</groupId>
        <artifactId>eureka-father</artifactId>
        <version>1.0.0.RELEASE</version>
    </parent>

    <!--引入eurekaserver  依赖-->
    <dependencies>
        <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-web</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • eureka3000 of applicatiion.yml file:
server:
  port: 3000
eureka:
  server:
    enable-self-preservation: false  #关闭自我保护机制
    eviction-interval-timer-in-ms: 4000 #设置清理间隔(单位:毫秒 默认是60*1000)
  instance:
    hostname: eureka3000.com
  client:
    serviceUrl:
        defaultZone: http://eureka3001.com:3001/eureka,http://eureka3002.com:3002/eureka   #将自己当成服务注册到其他注册中心
spring:
  application:
    name: eureka-server-cluster  #设置服务名
  • eureka3000 same as the original startup class
  • eureka3001 the pom file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.org.ldc</groupId>
    <artifactId>eureka3001</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka3001</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!-- 继承本项目的父工程 -->
    <parent>
        <groupId>com.org.ldc</groupId>
        <artifactId>eureka-father</artifactId>
        <version>1.0.0.RELEASE</version>
    </parent>

    <!--引入eurekaserver  依赖-->
    <dependencies>
        <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-web</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • eureka3001 the application file:
server:
  port: 3001
eureka:
  server:
    enable-self-preservation: false  #关闭自我保护机制
    eviction-interval-timer-in-ms: 4000 #设置清理间隔(单位:毫秒 默认是60*1000)
  instance:
    hostname: eureka3001.com
  client:
    serviceUrl:
        defaultZone: http://eureka3000.com:3000/eureka,http://eureka3002.com:3002/eureka   #将自己当成服务注册到其他注册中心
spring:
  application:
    name: eureka-server-cluster  #设置服务名
  • Start class above

  • eureka3002 the pom file:

    
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.org.ldc</groupId>
    <artifactId>eureka3002</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka3002</name>
    <description>Demo project for Spring Boot</description>
    
    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <!-- 继承本项目的父工程 -->
    <parent>
        <groupId>com.org.ldc</groupId>
        <artifactId>eureka-father</artifactId>
        <version>1.0.0.RELEASE</version>
    </parent>
    
    <!--引入eurekaserver  依赖-->
    <dependencies>
        <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-web</artifactId>
        </dependency>
    </dependencies>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 - eureka3002的application文件:

Server:
Port: 3002
Eureka:
Server:
enable-Self-Preservation: false # turn off self-protection mechanism of
eviction-interval-timer-in- ms: 4000 # Set cleanup interval (Unit: ms Default is 60 * 1000)
instance:
hostname: eureka3002.com
Client:
serviceUrl:
defaultzone: http://eureka3001.com:3001/eureka,http://eureka3000.com:3000/eureka # to register themselves as service to other registries
the Spring:
the Application:
name: eureka- server-cluster # set the service name


 - 最后是工程的pom文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">;
<!--基本信息-->
<description>SpringBoot-Eureka环境搭建多模块构建示例</description>
<modelVersion>4.0.0</modelVersion>
<name>eureka-father</name>
<packaging>pom</packaging>

<!-- 项目说明:这里作为聚合工程的父工程 -->
<groupId>com.org.ldc</groupId>
<artifactId>eureka-father</artifactId>
<version>1.0.0.RELEASE</version>

<!-- 继承说明:这里继承SpringBoot提供的父工程 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
</parent>

<modules>
    <module>eureka3000</module>
    <module>eureka3001</module>
    <module>eureka3002</module>
    <module>user5000</module>
</modules>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.org.ldc</groupId>
            <artifactId>eureka3000</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.org.ldc</groupId>
            <artifactId>eureka3001</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.org.ldc</groupId>
            <artifactId>eureka3002</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.org.ldc</groupId>
            <artifactId>user5000</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</dependencyManagement>

</project>


在这里说明一下,这里的搭建教程是基于maven的聚合工程搭建的,需要有能搭建maven聚合工程的前提,这里就默认大家会搭建,所以就一顿输出了,如果还不懂的可以自行去百度。

最后测试就是启动各个注册中心,然后最后启动服务在浏览器分别输入各个注册中心的访问地址,出现如下图,说明搭建成功
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191215215951525.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMjU1MDE3,size_16,color_FFFFFF,t_70)
>更多的教程请关注非科班的科班,一起努力,加油干,路过的小伙伴帮我点个赞,谢谢大家

最后分享一波java的资源,资源包括java从入门到开发的全套视频,以及java的26个项目,资源比较大,大小大概是290g左右,链接容易失效,获取的方式是关注公众号:非科班的科班,让后回复:java项目即可获得,祝大家学习愉快

Guess you like

Origin blog.51cto.com/14481935/2460556