美文网首页
本机搭建spring cloub的高可用Eureka

本机搭建spring cloub的高可用Eureka

作者: KyGb | 来源:发表于2019-07-14 11:58 被阅读0次

    参考书籍:《Spring Cloud微服务实战》

    服务端配置


    总节点
        server.port=4444
        spring.application.name = peer
        eureka.client.serviceUrl.defaultZone = http://peer2:1112/eureka/,http://peer1:1111/eureka/
    

    节点一

    spring.application.name=peer 
    spring.profiles.active=peer1
    server.port=1111
    eureka.instance.hostname=peer1
    #eureka.client.register-with-eureka=true  默认为true
    #eureka.client.fetch-registry = true
    eureka.client.serviceUrl.defaultZone=http://peer2:1112/eureka/
    

    节点二

      spring.application.name=peer 
        
        spring.profiles.active=peer2
        server.port=1112
        eureka.instance.hostname=peer2
        #eureka.client.register-with-eureka=true
        #eureka.client.fetch-registry = true
        eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/
    

    配置host文件:

    127.0.0.1 peer1
    127.0.0.1 peer2
    

    打包jar
    进入jar路径启动命令

    java -jar learn-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
    java -jar learn-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
    java -jar learn-0.0.1-SNAPSHOT.jar 
    
    • 访问

    http://localhost:1111/


    http://localhost:1112/

    http://localhost:4444/

    pom
             <parent>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-parent</artifactId>
                 <version>1.3.7.RELEASE</version>
                <relativePath/> <!-- lookup parent from repository -->
             </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
    
        <dependencies>
    
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>1.5.4.RELEASE</version>
            </dependency>
    
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <!--spring cloub-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka-server</artifactId>
                <version>1.4.6.RELEASE</version>
            </dependency>
    
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Brixton.SR5</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
    • 最后注意问题:出现available-replicas为空

    原因:节点一与节点二下面的服务中心的配置名称保持一致,微服务名称大于服务
    spring.application.name=peer

    相关文章

      网友评论

          本文标题:本机搭建spring cloub的高可用Eureka

          本文链接:https://www.haomeiwen.com/subject/lkebkctx.html