SpringCloud Study - (二) Eureka 集群

Eureka 是SpringCloud 重要组件之一,为整个微服务提供注册中心。

目标:

     搭建三台 Eureka 模块集群服务。

步骤:

1.创建springboot modle 项目 EureKaServer1,EurekaServer2,EureKaServer3。

2.修改pom文件,加入Eureka的相关依赖。由于spring cloud 发展迅速各个版本兼容管理容易冲突,我们用统一的组件管理版本配置管理。

<?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.study</groupId>
    <artifactId>EurekaServer1</artifactId>
    <version>1.0-SNAPSHOT</version>

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

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <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-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>

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


</project>

3.配置Application.peroperties 属性文件,Eureka server 三个模块的端口分别为 9001,9002,9003.同时需要加入Eureka的相关配置.

EurekaServer1 ->Application.properties:

spring.application.name=spring-cloud-eureka
server.port=9001
#表示是否将自己注册到Eureka Server,默认为true。
eureka.client.register-with-eureka=true
#表示是否从Eureka Server获取注册信息,默认为true。
#在集群中从其他Eureka Server获取注册信息
eureka.client.fetch-registry=true

eureka.instance.hostname=eureka1
#默认地址为http://localhost:8761/eureka
#把本Eureka注册到其他Eureka的URL地址
eureka.client.serviceUrl.defaultZone=http://eureka2:9002/eureka,http://eureka3:9003/eureka
##关闭自我保护模式
eureka.server.enable-self-preservation=false
#清理不可用服务的时间,默认为60*1000
eureka.server.eviction-interval-timer-in-ms=4000

EurekaServer2 ->Application.properties

spring.application.name=spring-cloud-eureka
server.port=9002
#表示是否将自己注册到Eureka Server,默认为true。
eureka.client.register-with-eureka=true
#表示是否从Eureka Server获取注册信息,默认为true。
eureka.client.fetch-registry=true

eureka.instance.hostname=eureka2
#默认地址为http://localhost:8761/eureka
#把本Eureka注册到其他Eureka的URL地址
eureka.client.serviceUrl.defaultZone=http://eureka1:9001/eureka,http://eureka3:9003/eureka
##关闭自我保护模式
eureka.server.enable-self-preservation=false
#清理不可用服务的时间,默认为60*1000
eureka.server.eviction-interval-timer-in-ms=4000

EurekaServer3 ->Application.properties

spring.application.name=spring-cloud-eureka
server.port=9003
#表示是否将自己注册到Eureka Server,默认为true。
eureka.client.register-with-eureka=true
#表示是否从Eureka Server获取注册信息,默认为true。
eureka.client.fetch-registry=true

eureka.instance.hostname=eureka3
#默认地址为http://localhost:8761/eureka
#把本Eureka注册到其他Eureka的URL地址
eureka.client.serviceUrl.defaultZone=http://eureka1:9001/eureka,http://eureka2:9002/eureka
##关闭自我保护模式
eureka.server.enable-self-preservation=false
#清理不可用服务的时间,默认为60*1000
eureka.server.eviction-interval-timer-in-ms=4000

 

4. 在host 文件中配置域名解析 .

host 文件位置:C:\Windows\System32\drivers\etc

在host 文件中增加以下内容。

127.0.0.1       eureka1
127.0.0.1       eureka2
127.0.0.1       eureka3

5.在启动入口类加上Eureka启动注解。

6. 启动 EurekaServer1 ,EurekaServer2,EurekaServer3 ,并在浏览器中访问 Eureka Server 模块。

总结: 在Eureka 服务模块搭建中遇到的坑。

坑1 :服务启动后而且服务单独访问都正常,但是服务出现在了 unavailable-replicas,经过搜资料排查是因为参数设置问题,需要把参数

register-with-eureka 设置为true。
eureka.client.register-with-eureka=true

坑2 : 在浏览器访问中不需要加后缀 \eureka,但是在application.properties中必须加后缀\eureka.

 

application.properties

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值