• SpringCloudEureka高可用


    首先先在C:\Windows\System32\drivers\etc\host文件中添加如下Host

    127.0.0.1 eurekaB
    127.0.0.1 eurekaA
    

     

    先建springcloudParent.java的项目

    pom.xml文件如下

    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.example</groupId>
        <artifactId>springcloudParent</artifactId>
        <packaging>pom</packaging>
        <version>1.0-SNAPSHOT</version>
        <modules>
        </modules>
    
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
    </project>
    

     在建一个eurekademo的maven module

    pom.xml文件如下:

    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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.zk</groupId>
        <artifactId>eurekademo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>eurekademo</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
            <spring-cloud.version>Hoxton.SR9</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>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </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>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>${spring-boot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>2.3.7.RELEASE</version>
                    <configuration>
                        <mainClass>com.zk.eurekademo.EurekademoApplication</mainClass>
                    </configuration>
                    <executions>
                        <execution>
                            <id>repackage</id>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    EurekademoApplication.java
    package com.zk.eurekademo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekademoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekademoApplication.class, args);
        }
    
    }
    

     application.yml

    spring:
      application:  # 应用名字,eureka 会根据它作为服务id
        name: eureka
    server:  # 服务端口
      port: 8761
    eureka:
      instance:
        hostname: eurekaA
      client:
        register-with-eureka: false  # 不向eureka server 注册自己
        fetch-registry: false  # 不向eureka server 获取服务列表
        service-url:
          defaultZone: http://eurekaB:8762/eureka/
    

     复制一份eurekademo为eurekademo2 

    仅更改application.yml

    spring:
      application:  # 应用名字,eureka 会根据它作为服务id
        name: eureka
    server:  # 服务端口
      port: 8762
    eureka:
      instance:
        hostname: eurekaB
      client:
        register-with-eureka: false  # 不向eureka server 注册自己
        fetch-registry: false  # 不向eureka server 获取服务列表
        service-url:
          defaultZone: http://eurekaA:8761/eureka/
    

    启动时勾选Allow parallel run

     两个springboot application启动

    访问http://localhost:8761/

  • 相关阅读:
    C# OCR 文字识别[初级]
    首次试用 NeoSwiff ,用C#开发FLASH版的多国语言翻译
    星期天上午买了个nokia 的NGage QD手机
    高兴,今天总算鸟枪换炮了~
    3年前的小程序:破解需要delphi IDE 环境的vcl 控件
    基于Ajax的五子棋演示
    学习API HOOK,编写了一个winsock 的封包抓取程序,可免费使用;
    我得小猫爱摔交
    老是不中,算了算“双色球”和“3D”,全买到底要多少¥¥。。(C 代码)
    总算忙完了,大家'晚秋快乐'!
  • 原文地址:https://www.cnblogs.com/longlyseul/p/16667702.html
Copyright © 2020-2023  润新知