Spring Cloud 之服务注册中心高可用

服务注册中心高可用

服务注册中心 eureka-server 高可用实施

版本

Spring Boot 版本

# Spring Boot 版本: 

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/>
    </parent>

Spring Cloud 版本


# Spring Cloud 版本: 

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

主机

修改hosts 模拟两台主机

C:WindowsSystem32driversetchosts

# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
127.0.0.1 localhost
127.0.0.1 www.galsang.org
127.0.0.1 www.galsang.io

服务注册中心

一台机器模拟出的两台主机规划如下:

项目名称 spring.application.name eureka.client.serviceUrl.defaultZone eureka.instance.hostname 端口
spring-cloud-server spring-cloud-server-discovery-eureka-ha http://www.galsang.io:8999/eureka/ www.galsang.org 9000
spring-cloud-server-available spring-cloud-server-discovery-eureka-ha http://www.galsang.org:9000/eureka/ www.galsang.io 8999

spring-cloud-server

pom.xml


<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.galsang.cloud</groupId>
    <artifactId>spring-cloud-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-cloud-server</name>
    <description>注册中心</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

application-pro.yml


## 生产环境 启用服务注册中心高可用
spring:
  profiles: pro
  cloud:
    inetutils:
      preferred-networks:
        - 192.168
server:
  port: 9000
  context-path: /
# 服务治理中心实例
eureka:
  instance:
    hostname: www.galsang.org
    leaseRenewalIntervalInSeconds: 1
    leaseExpirationDurationInSeconds: 2
# 是否作为服务进行注册,在此仅作为服务治理中心,不作为服务进行注册
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl.defaultZone: http://www.galsang.io:8999/eureka/
# 关闭自我保护
  server:
    enableSelfPreservation: false

spring-cloud-server-available

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.galsang.cloud</groupId>
    <artifactId>spring-cloud-server-available</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-cloud-server-available</name>
    <description>注册中心</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

application-pro.yml


## 生产环境 启用服务注册中心高可用
spring:
  profiles: pro
  cloud:
    inetutils:
      preferred-networks:
        - 192.168
server:
  port: 8999
  context-path: /
# 服务治理中心实例
eureka:
  instance:
    hostname: www.galsang.io
    leaseRenewalIntervalInSeconds: 1
    leaseExpirationDurationInSeconds: 2
# 是否作为服务进行注册,在此仅作为服务治理中心,不作为服务进行注册
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl.defaultZone: http://www.galsang.org:9000/eureka/
# 关闭自我保护
  server:
    enableSelfPreservation: false

启动项目

浏览器打开
http://www.galsang.org:9000

http://www.galsang.io:8999

这里写图片描述

说明 服务注册中心 eureka-server 高可用已经成功

源码请移步: https://gitee.com/vimx86/SpringCloud-Learning

参考资料

原文地址:https://www.cnblogs.com/ljmatlight/p/9060777.html