springboot集成zuul,-SpringBoot 3.4.5集成SpringCloud 2027-eureka、admin-service demo - empty()

📅 2026-12-01 👤 拼多多助力营销技巧 📂 拼多多助力网站平台 👁️ 476 次阅读

文章目录

前言SpringBoot 3.4.5集成SpringCloud 2024-eureka、admin-service demo1. 组件情况:2. 分布式场景中组件定位3. eureka、admin服务制作3.1. 父Pom3.2. eureka服务制作3.2.1. 服务端3.2.2. 客户端

3.3. admin服务制作3.3.1. 服务端3.3.2. 客户端

前言

如果您觉得有用的话,记得给博主点个赞,评论,收藏一键三连啊,写作不易啊^ _ ^。 而且听说点赞的人每天的运气都不会太差,实在白嫖的话,那欢迎常来啊!!!

SpringBoot 3.4.5集成SpringCloud 2024-eureka、admin-service demo

1. 组件情况:

jdk:17 SpringBoot:3.4.5 SpringCloud:2024

2. 分布式场景中组件定位

eureka:注册中心 admin:服务监控、同时也可以自定义扩展,比如当服务宕机时,可以写相关通知代码,比如邮件通知、企业微信群机器人通知等等。

注意的是这里我将admin与eureka拆分出来作为一个新的服务,客观原因是SpringCloud版本太新了,当前的admin与这个版本的eureka并没有适配,选择此SpringCloud的版本的一大原因是,我们的SpringBoot底座太高了,2024年的版本,目前其他开源组件并没有适配到这个级别的版本,因此只能使用原生SpringCloud,作为分布式架构的设计。

3. eureka、admin服务制作

3.1. 父Pom

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">

org.springframework.boot

spring-boot-starter-parent

3.4.5

4.0.0

org.example

yzy

1.0-SNAPSHOT

pom

springCloud-2024-admin-service

springCloud-2024-eureka

springCloud-2024-Gateway

springCloud-2024-service-A

springCloud-2024-service-B

yzy

yzy

UTF-8

17

2024.0.2

3.4.0

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

pom

import

eureka服务与admin服务为下图红框处:

3.2. eureka服务制作

3.2.1. 服务端

pom:

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">

4.0.0

org.example

yzy

1.0-SNAPSHOT

springCloud-2024-eureka

1.0-SNAPSHOT

springCloud-2024-eureka

UTF-8

17

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

de.codecentric

spring-boot-admin-starter-client

${spring-boot-admin.version}

org.springframework.boot

spring-boot-starter-web

org.springframework.boot 拼多多帮助力入口

spring-boot-maven-plugin

配置:

server:

port: 8761

spring:

application:

name: eureka-server

boot:

admin:

client:

url: http://127.0.0.1:8889 # Spring Boot Admin Server 的地址

instance:

name: ${spring.application.name}

prefer-ip: true # 注册时使用 IP 地址

eureka:

instance:

hostname: localhost

client:

register-with-eureka: false # 禁用Eureka客户端向Eureka服务器注册自己,因为Eureka服务器不需要注册自己

fetch-registry: true # 设置为 true,意味着这个应用程序会从 Eureka 服务器获取注册表中的服务信息。这是客户端的一部分配置,用于从 Eureka 服务器获取其他服务的注册信息

serviceUrl:

defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

server:

enable-self-preservation: false # 禁用Eureka服务器的自我保护模式。默认情况下,自我保护模式是启用的,以防止网络分区导致的不必要的实例剔除。在测试或开发环境中,可能会禁用它以简化故障排除,但在生产环境中最好保持启用状态。

logging:

level:

com.netflix: WARN

# org.springframework.security: DEBUG

management:

endpoints:

web:

exposure:

include: "*"

启动类添加:

@SpringBootApplication

// 将当前应用注册为 Eureka 客户端(可选)

@EnableDiscoveryClient

// 用于启动 Eureka 服务注册和发现服务器。

@EnableEurekaServer

public class EurekaServerApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaServerApplication.class, args);

}

}

效果:

3.2.2. 客户端

客户端需要添加pom:

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

配置:

eureka:

instance:

prefer-ip-address: true # 优先ip注册

client:

service-url:

defaultZone: http://127.0.0.1:8761/eureka # 注册中心地址

启动类添加:

@EnableDiscoveryClient

3.3. admin服务制作

3.3.1. 服务端

pom:

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">

4.0.0

org.example

yzy

1.0-SNAPSHOT

springCloud-2024-admin-service

1.0-SNAPSHOT

springCloud-2024-admin-service

UTF-8

17

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.boot

spring-boot-starter-web

de.codecentric

spring-boot-admin-starter-server

3.4.0

org.springframework.boot

spring-boot-maven-plugin

配置:

server:

port: 8889

spring:

application:

name: admin-server

boot:

admin:

discovery:

enabled: false

ui:

# context-path: /admin

security:

user:

name: admin

password: admin

roles: ADMIN # 角色

eureka:

instance:

prefer-ip-address: true # 优先ip注册

client:

service-url:

defaultZone: http://127.0.0.1:8761/eureka # 注册中心地址

logging:

level:

com.netflix: WARN

management:

endpoints:

web:

exposure:

include: "*"

注意的配置:

spring:

application:

name: admin-server

boot:

admin:

discovery:

enabled: false

作用:禁止 Server 从 Eureka/Nacos 等自动发现实例,要不然当service与eureka的实例名不同时,会出现两个实例。

宕机时自定义处理方式代码:

package org.example.config;

import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

/**

* @author 杨镇宇

* @date 2025/9/29 16:16

* @version 1.0

*/

@Configuration

public class NotifierConfig {

@Bean

public CustomNotifier customNotifier(InstanceRepository repository) {

return new CustomNotifier(repository);

}

}

package org.example.config;

import de.codecentric.boot.admin.server.domain.entities.Instance;

import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;

import de.codecentric.boot.admin.server.domain.events.InstanceEvent;

import de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent;

import de.codecentric.boot.admin.server.notify.AbstractEventNotifier;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import reactor.core.publisher.Mono;

public class CustomNotifier extends AbstractEventNotifier{

private static final Logger log = LoggerFactory.getLogger(CustomNotifier.class);

public CustomNotifier(InstanceRepository repository) {

super(repository);

}

@Override

protected MonodoNotify(InstanceEvent event, Instance instance) {

if (event instanceof InstanceStatusChangedEvent statusChangedEvent) {

String newStatus = statusChangedEvent.getStatusInfo().getStatus();

String appName = instance.getRegistration().getName();

if ("DOWN".equalsIgnoreCase(newStatus)) {

log.warn("服务 {} 宕机!", appName);

sendCustomNotification(appName, "DOWN");

}

if ("UP".equalsIgnoreCase(newStatus)) {

log.info("服务 {} 已恢复上线", appName);

sendCustomNotification(appName, "UP");

}

}

return Mono.empty();

}

private void sendCustomNotification(String serviceName, String status) {

log.info("【通知发送】服务:{} 状态:{}", serviceName, status);

// TODO: 在这里加邮件/钉钉/微信推送

}

}

启动效果: 服务宕机效果: 我这里演示的是直接关闭服务操作。

3.3.2. 客户端

pom:

de.codecentric

spring-boot-admin-starter-client

${spring-boot-admin.version}

配置:

spring:

application:

name: eureka-server

boot:

admin:

client:

url: http://127.0.0.1:8889 # Spring Boot Admin Server 的地址

instance:

name: ${spring.application.name}

prefer-ip: true # 注册时使用 IP 地址,springboot集成zuul

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧