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:\Windows\System32\drivers\etc\hosts

# 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.nameeureka.client.serviceUrl.defaultZoneeureka.instance.hostname端口
spring-cloud-serverspring-cloud-server-discovery-eureka-hahttp://www.galsang.io:8999/eureka/www.galsang.org9000
spring-cloud-server-availablespring-cloud-server-discovery-eureka-hahttp://www.galsang.org:9000/eureka/www.galsang.io8999

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值