03 Eureka的高可用

本文档介绍了如何将EurekaServer配置为服务注册到其他EurekaServer,实现高可用的EurekaServer集群。配置包括修改`application.yml`文件,设置服务地址和端口,并通过启动参数指定不同节点的默认Zone。同时,展示了用户服务(user_service)如何注册到EurekaServer,以及启动和验证过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将Eureka Server作为一个服务注册到其它Eureka Server,这样多个Eureka Server之间就能够互相发现对方,同步服务,实现Eureka Server集群(高可用)。

Eureka服务(eureka_service)

修改application.yml配置文件

导入坐标
  • 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_parent</artifactId>
        <groupId>li.chen.com</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka_service</artifactId>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

</project>
配置文件
  • application.yml
server:
  # 端口设置 有传参使用传参值;没有使用默认10086
  port: ${port:10086}

spring:
  application:
    name: eureka_server

eureka:
  client:
    service-url:
      # eureka 服务地址,如果是集群的话;需要指定其它集群eureka地址
      # 端口设置 有传参使用传参值;没有使用默认 http://127.0.0.1:10086/eureka
      defaultZone: ${defaultZone:http://127.0.0.1:10086/eureka/}

10086端口 子节点配置
-Dport=10086 -DdefaultZone=http://127.0.0.1:10087/eureka/ (图片错误,以此为准)
在这里插入图片描述
10087端口 子节点配置
-Dport=10087 -DdefaultZone=http://127.0.0.1:10086/eureka/ (图片错误,以此为准)
在这里插入图片描述

创建启动类
package li.chen.com;

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

/**
 * @ClassName: EurekaServerApplication
 * @Description 启动引导类
 * @EnableEurekaServer 声明当前类为Eureka服务
 **/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class,args);
    }
}

测试

启动
EurekaServerApplication
启动
EurekaServerApplication10087
访问路径 http://127.0.0.1:10086/ 或 http://127.0.0.1:10086/
在这里插入图片描述

服务注册(user_service)

仅修改application.yml配置文件

配置文件
  • application.yml
server:
  port: 9090

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db2
    username: root
    password: root
  application:
    name: user_service

mybatis:
  type-aliases-package: li.chen.com.user.pojo

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka,http://127.0.0.1:10086/eureka
springboot启动类
  • UserApplication
package li.chen.com.user;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@MapperScan("li.chen.com.user.mapper")
@EnableDiscoveryClient   //开启Eureka客户端发现功能
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }
}

测试

启动UserApplication
访问 http://127.0.0.1:10086/ 或 http://127.0.0.1:10087/ 都显示user_Service服务
注意:EurekaServerApplication要一直启动
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岿然如故

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值