SpringCloud(eurka server集群 eureka client集群)

  • SpringBoot 2.3.5.RELEASE
  • SpringCloud Hoxton.SR6

1 - 父项目maven聚合

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
    </parent>
    <properties>
        <spring.cloud-version>Hoxton.SR6</spring.cloud-version>
    </properties>
    <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>

2 - eureka server 集群开发

1 - 8761 开发
pom

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

application.yml

server:
  port: 8761
spring:
  application:
    name: EUREKASERVER
eureka:
  client:
    fetch-registry: false
    register-with-eureka: false
    service-url:
      defaultZone: http://127.0.0.1:8762/eureka/,http://127.0.0.1:8763/eureka/

入口类

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication8761 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication8761.class, args);
    }
}

2 - 8762 开发

pom

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

application.yml

server:
  port: 8762
spring:
  application:
    name: EUREKASERVER
eureka:
  client:
    fetch-registry: false
    register-with-eureka: false
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka/,http://127.0.0.1:8763/eureka/

入口类

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication8762 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication8762.class, args);
    }
}

3 - 8763 开发

pom

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

application.yml

server:
  port: 8763
spring:
  application:
    name: EUREKASERVER
eureka:
  client:
    fetch-registry: false
    register-with-eureka: false
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka/,http://127.0.0.1:8762/eureka/

入口类

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication8763 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication8763.class, args);
    }
}

2 - eureka client 开发

1 - 8080 开发

pom

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

application.yml


spring:
  application:
    name: EUREKACLIENTPRODUCT
server:
  port: 8080

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka/,http://127.0.0.1:8762/eureka,http://127.0.0.1:8763/eureka

入口类

@SpringBootApplication
@EnableEurekaClient
public class EurekaClient8080 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClient8080.class, args);
    }
}

2 - 8081 开发
pom

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

application.yml

spring:
  application:
    name: EUREKACLIENTPRODUCT
server:
  port: 8081

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka/,http://127.0.0.1:8762/eureka,http://127.0.0.1:8763/eureka

入口类

@SpringBootApplication
@EnableEurekaClient
public class EurekaClient8081 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClient8081.class, args);
    }
}

4 - 注意问题

我不知道是我的版本的问题还是机器的问题 填写localhost的时候无法完成集群
把localhost改成127.0.0.1就成功了

5 - 成功展示

先将server端全部开启,再开启client端
SpringCloud(eurka server集群 eureka client集群)

上一篇:解决springboot前后端跨域问题


下一篇:HM-SpringCloud微服务系列4.3【SpringAMQP】