【JAVA企业级开发】详解在本机环境下构建高可用分布式Eureka server集群的配置过程,应用过程和测试监控面板变化过程

一级目录

二级目录

三级目录

一修改本机host,用于在单机情况下构建伪集群

# Copyright (c) 1993-2009 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:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
	127.0.0.1       eurekaserver1
	127.0.0.1       eurekaserver2
	127.0.0.1       eurekaserver3
	

三个host对应着三个eurekaserver Module
在这里插入图片描述

二 集群中各个eurekaserver的配置文件bootstrap.properties

1eureka-server-1

#应用名
spring.application.name=eurekaserver1
#服务端口号
server.port=9001
#eureka服务端实例URL
eureka.instance.hostname=eurekaserver1
#不向服务注册中心注册自己,如果是erueka集群的话,因为自我保护模式,就需要开启自注册了
eureka.client.register-with-eureka=true
#表示自己就是服务注册中心,职责就是维护服务,不去检索服务
eureka.client.fetch-registry=false
#服务中心地址
eureka.client.service-url.defaultZone=http://eurekaserver2:9002/eureka/,http://eurekaserver3:9003/eureka/
#调整保护机制生效的renews 和renews threshold百分比,此配置的百分比会减少 renews threshold的次数
eureka.server.renewal-percent-threshold=0.85
#安全模式访问服务中心,如果服务中心开启安全机制,则客户端的微服务注册请求和心跳机制将会被拦截
#spring.security.user.name=fengbo
#spring.security.user.password=fengbo


2eureka-server-2

#应用名
spring.application.name=eurekaserver2
#服务端口号
server.port=9002
#eureka服务端实例URL
eureka.instance.hostname=eurekaserver2
#不向服务注册中心注册自己,如果是erueka集群的话,因为自我保护模式,就需要开启自注册了
eureka.client.register-with-eureka=true
#表示自己就是服务注册中心,职责就是维护服务,不去检索服务
eureka.client.fetch-registry=false
#服务中心地址
eureka.client.service-url.defaultZone=http://eurekaserver3:9003/eureka/,http://eurekaserver1:9001/eureka/
#调整保护机制生效的renews 和renews threshold百分比,此配置的百分比会减少 renews threshold的次数
eureka.server.renewal-percent-threshold=0.85
#安全模式访问服务中心,如果服务中心开启安全机制,则客户端的微服务注册请求和心跳机制将会被拦截
#spring.security.user.name=fengbo
#spring.security.user.password=fengbo



3eureka-server-3

#应用名
spring.application.name=eurekaserver3
#服务端口号
server.port=9003
#eureka服务端实例URL
eureka.instance.hostname=eurekaserver3
#不向服务注册中心注册自己,如果是erueka集群的话,因为自我保护模式,就需要开启自注册了
eureka.client.register-with-eureka=true
#表示自己就是服务注册中心,职责就是维护服务,不去检索服务
eureka.client.fetch-registry=false
#服务中心地址
eureka.client.service-url.defaultZone=http://eurekaserver2:9002/eureka/,http://eurekaserver1:9001/eureka/
#调整保护机制生效的renews 和renews threshold百分比,此配置的百分比会减少 renews threshold的次数
eureka.server.renewal-percent-threshold=0.85
#安全模式访问服务中心,如果服务中心开启安全机制,则客户端的微服务注册请求和心跳机制将会被拦截
#spring.security.user.name=fengbo
#spring.security.user.password=fengbo



三依赖(三个server都一样)

1eureka-server-1

<?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">
    <parent>
        <artifactId>FengboSoft</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>eureka-server-1</artifactId>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!--eureka服务中心依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--cloud分布式配置中心-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!--web组件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
     <!--   &lt;!&ndash; 安全模式依赖 &ndash;&gt;
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>-->
    </dependencies>

</project>

2eureka-server-2

<?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">
    <parent>
        <artifactId>FengboSoft</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-server-2</artifactId>
        <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!--eureka服务中心依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--cloud分布式配置中心-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!--web组件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--   &lt;!&ndash; 安全模式依赖 &ndash;&gt;
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-security</artifactId>
           </dependency>-->
    </dependencies>

</project>

3eureka-server-3

<?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">
    <parent>
        <artifactId>FengboSoft</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-server-3</artifactId>
       <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!--eureka服务中心依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--cloud分布式配置中心-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!--web组件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--   &lt;!&ndash; 安全模式依赖 &ndash;&gt;
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-security</artifactId>
           </dependency>-->
    </dependencies>

</project>

四集群间通信测试

参数变化:
Renews threshold 1
Renews (last min) 6

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

五 微服务提供者和集群之间通信测试

1修改配置文件,将交互地址改写成三个集群服务器的地址

#注意不要忘了/eureka/后缀,设置与eureka-server的交互地址
eureka.client.service-url.defaultZone= http://eurekaserver1:9001/eureka/,http://eurekaserver2:9002/eureka/,http://eurekaserver3:9003/eureka/
#应用名
spring.application.name=AccountProvider
#服务器端口
server.port=8001
#配置Mybatis
mybatis.config-location=classpath:mybatisConfig.xml
mybatis.mapper-locations=classpath:mapper/*.xml
#配置数据源bean
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=shuang666
spring.datasource.url=jdbc:mysql://49.235.16.100:3306/springcloud1
spring.datasource.dbcp2.min-idle=5
spring.datasource.dbcp2.initial-size=5
spring.datasource.dbcp2.max-idle=5
spring.datasource.dbcp2.max-wait-millis=200
#配置内置日志logback
logging.config=classpath:logback.xml
#配置单文件上传最大值,#配置多文件请求上传最大值
spring.servlet.multipart.max-file-size=6MB
spring.servlet.multipart.max-request-size=66MB
#分页插件配置,#指定数据库,#分页合理化参数,默认值为false。当该参数设置为 true 时,pageNum<=0 时会查询第一页,#支持通过 Mapper 接口参数来传递分页参数,默认值false
#pagehelper.helper-dialect=mysql
#pagehelper.reasonable=true
#pagehelper.support-methods-arguments=true
#注意不要忘了/eureka/后缀,设置与eureka-server的交互地址,服务注册到集群只需要注册一个即可,因为eureka之间互相注册了
eureka.client.service-url.defaultZone= http://eurekaserver1:9001/eureka/,http://eurekaserver2:9002/eureka/,http://eurekaserver3:9003/eureka/
#eureka的健康检查机制开关
eureka.client.healthcheck.enabled=true
#自我保护机制
#eureka.server.enable-self-preservation=false
#自我保护机制关闭之后的配置,告诉服务端,如果你N秒之后还没有接受到我发送的心跳,就代表我"死"了,将我的服务踢出掉。
#eureka.instance.lease-expiration-duration-in-seconds=36
#设置每隔多少秒给服务中心发送一次心跳,证明自己还活着
#eureka.instance.lease-renewal-interval-in-seconds=6
#springcloud的配置中心开关,SpringCloud默认启动的时候是会先加载bootstrap.properties或bootstrap.yml配置文件,如果没有的话,则会远程从http://localhost:8888获取配置,然后才会加载到application.yml,properties文件。
spring.cloud.config.enabled=false

2服务注册集群测试

参数变化:
Renews threshold 3
Renews (last min) 8

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牵牛刘先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值