050_Eureka 集群环境配置


复制创建Eureka服务端子模块 springcloud-eureka-7002

image.png

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">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>com.qing</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-eureka-7002</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--服务提供者使用eureka,eureka服务端使用eureka-server-->
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

    </dependencies>

</project>

配置文件 application.yml

注:只有端口和7001不同,为7002

server:
  port: 7002

# Eureka配置
eureka:
  instance:
    hostname: localhost # Eureka服务端的实例名称
  client:
    register-with-eureka: false # 表示是否向eureka注册中心注册自己,这是eureka服务端不需要注册,其他服务需要注册为true
    fetch-registry: false # 如果为false,表示这是注册中心,其他服务需要为true
    service-url: # 注册url,监控页面是:http://${eureka.instance.hostname}:${server.port}
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动类,添加开启Eureka服务端注解

package com.qing.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer // 开启Eureka服务端
public class EurekaServer_7002 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_7002.class, args);
    }
}

启动测试

image.png
image.png

复制创建Eureka服务端子模块 springcloud-eureka-7003

image.png

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">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>com.qing</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-eureka-7003</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--服务提供者使用eureka,eureka服务端使用eureka-server-->
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

    </dependencies>

</project>

配置文件 application.yml

注:只有端口和7001不同,为7002

server:
  port: 7003

# Eureka配置
eureka:
  instance:
    hostname: localhost # Eureka服务端的实例名称
  client:
    register-with-eureka: false # 表示是否向eureka注册中心注册自己,这是eureka服务端不需要注册,其他服务需要注册为true
    fetch-registry: false # 如果为false,表示这是注册中心,其他服务需要为true
    service-url: # 注册url,监控页面是:http://${eureka.instance.hostname}:${server.port}
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动类,添加开启Eureka服务端注解

package com.qing.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer // 开启Eureka服务端
public class EurekaServer_7003 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_7003.class, args);
    }
}

启动测试

image.png
image.png

模拟Eureka服务端部署在多个服务器,修改hosts文件

# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost

127.0.0.1       eureka7001.com
127.0.0.1       eureka7002.com
127.0.0.1       eureka7003.com

Eureka集群配置

springcloud-eureka-7001

application.yml 配置集群关联其他eureka服务端

server:
  port: 7001

# Eureka配置
eureka:
  instance:
    hostname: eureka7001.com # Eureka服务端的实例名称
  client:
    register-with-eureka: false # 表示是否向eureka注册中心注册自己,这是eureka服务端不需要注册,其他服务需要注册为true
    fetch-registry: false # 如果为false,表示这是注册中心,其他服务需要为true
    service-url: # 注册url,监控页面是:http://${eureka.instance.hostname}:${server.port}
      # 单机:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      # 集群(关联):
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

springcloud-eureka-7002

application.yml 配置集群关联其他eureka服务端

server:
  port: 7002

# Eureka配置
eureka:
  instance:
    hostname: eureka7002.com # Eureka服务端的实例名称
  client:
    register-with-eureka: false # 表示是否向eureka注册中心注册自己,这是eureka服务端不需要注册,其他服务需要注册为true
    fetch-registry: false # 如果为false,表示这是注册中心,其他服务需要为true
    service-url: # 注册url,监控页面是:http://${eureka.instance.hostname}:${server.port}
      # 单机:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      # 集群(关联):
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/

springcloud-eureka-7003

application.yml 配置集群关联其他eureka服务端

server:
  port: 7003

# Eureka配置
eureka:
  instance:
    hostname: eureka7003.com # Eureka服务端的实例名称
  client:
    register-with-eureka: false # 表示是否向eureka注册中心注册自己,这是eureka服务端不需要注册,其他服务需要注册为true
    fetch-registry: false # 如果为false,表示这是注册中心,其他服务需要为true
    service-url: # 注册url,监控页面是:http://${eureka.instance.hostname}:${server.port}
      # 单机:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      # 集群(关联):
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

服务提供者子模块注册到集群

springcloud-provider-dept-8001

application.yml 配置注册集群url

server:
  port: 8001

mybatis:
  type-aliases-package: com.qing.springcloud.pojo
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
# 开启驼峰
#  configuration:
#    map-underscore-to-camel-case: true

spring:
  application:
    name: springcloud-provider-dept
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456

# Eureka
eureka:
  client:
    service-url:
      # 单机:defaultZone: http://localhost:7001/eureka/ # 使用Eureka服务端配置的注册url
      # 集群:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ # 使用Eureka服务端配置的注册url
  instance:
    instance-id: springcloud-provider-dept-zdy # 修改Eureka监控页面上服务默认描述
# 监控信息 actuator-info配置
info:
  app.name: qing-springcloud
  company.name: 清风阁

集群启动测试:3个Eureka服务端和1个服务提供者

image.png
image.png
image.png

posted @   清风(学习-踏实)  阅读(78)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示