微服务重启优化kafka+EurekaNotificationServerListUpdater

由于遇到服务重启导致的业务中断等异常,所以计划通过kafka+eureka实现服务下线通知,来尽可能规避这类问题。

如果可以升级spring,则可以考虑nacos等更为方便的方案;

程序优化:

1.默认启用的为 PollingServerListUpdater,所以需要手动启用EurekaNotificationServerListUpdater

java 复制代码
@Configuration
public class ConsumerRibbonClientConfig {
    @Bean
    public ServerListUpdater ribbonServerListUpdater() {
        return new EurekaNotificationServerListUpdater();
    }
}

2.需要触发PollingServerListUpdater中的更新,则需要先触发DiscoveryClient中的refreshRegistry

java 复制代码
@Slf4j
@Component
public class EurekaRefreshUpdater {


    public void refresh() {
        try {
            log.info("EurekaRefreshUpdater-begin");
            Method method = DiscoveryClient.class.getDeclaredMethod("refreshRegistry");
            method.setAccessible(true);
            method.invoke(SpringUtil.getBean(DiscoveryClient.class));

            log.info("EurekaRefreshUpdater-end");
        } catch (Exception e) {
            log.error("EurekaRefreshUpdater"+e.getMessage(), e);
            e.printStackTrace();
        }
    }

3.服务关机listener

java 复制代码
@Component
@KafkaListener(topics = GracefulShutdownConfigConstant.KAFKA_TOPIC)
@Slf4j
public class ServiceDowntimeListener {

    @Autowired
    EurekaRefreshUpdater eurekaRefreshUpdater;
    @KafkaHandler
    public void onMessage(@Payload String message, Acknowledgment acknowledgment) {
        log.info("服务关机-接收到其他服务关机信息,message:{}", JSON.toJSONString(message));
        eurekaRefreshUpdater.refresh();
        acknowledgment.acknowledge();

    }
}

4.自己关机发送消息通知

java 复制代码
@Slf4j
@Component
public class GracefulShutdown {

    @Value("${server.graceful.shutdown.seconds:30}")
    private Integer serverGracefulShutdownSeconds;

    @Autowired
    EurekaClient eurekaClient;

    @Value("${spring.application.name}")
    private String serviceName;

    @Autowired
    private KafkaTemplate<Object, String> kafkaTemplate;


    @PreDestroy
    public void gracefulShutdown() throws InterruptedException {
        log.info("gracefulShutdown wait {} seconds -- begin", serverGracefulShutdownSeconds);
        eurekaClient.shutdown();
        new Thread(() -> {
            kafkaTemplate.send(GracefulShutdownConfigConstant.KAFKA_TOPIC,1,serviceName);
            kafkaTemplate.send(GracefulShutdownConfigConstant.KAFKA_TOPIC,0,serviceName);
        }).start();
        Thread.sleep(serverGracefulShutdownSeconds * 1000);
        log.info("gracefulShutdown shutdown");
    }
}

脚本优化

在服务启动脚本中,要注意不可使用kill -9 结束服务进程,需要使用kill -15 让服务有一定的存活时间。来处理完成已有的请求。

问题

1.kafka通过group分组,如果同一组则只能收到一条信息。如果同一服务部署两个节点,则不能很好的都通知到位,所以在创建kafka通知的时候,根据服务的部署情况,利用分区+多条通知,来变相实现全广播。

shell 复制代码
./kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 3 --partitions 2 --topic shutdown_service

2.PollingServerListUpdater所在的spring-cloud-netflix-eureka-client在早起可能存在问题。具体详见:
EurekaNotificationServerListUpdater启用后出现 Connection refused (Connection refused)

ps:

需要注意下程序版本以及kafka版本,防止某些方法不适用。

如果高版本kafka 是否可以通过指定不同的groupid来变相实现多服务通知呢?

相关推荐
lhrimperial34 分钟前
企业智能知识库助手落地实践:从RAG到Multi-Agent
java·spring cloud·微服务·系统架构·知识图谱
雨中飘荡的记忆1 小时前
Kafka入门:从零开始掌握消息队列
kafka
indexsunny3 小时前
互联网大厂Java面试实战:Spring Boot与微服务在电商场景的应用解析
java·spring boot·redis·微服务·kafka·gradle·maven
小辉笔记5 小时前
kafka原理总结
分布式·kafka
小北方城市网5 小时前
微服务接口设计实战指南:高可用、易维护的接口设计原则与规范
java·大数据·运维·python·微服务·fastapi·数据库架构
魂之木5 小时前
Nacos服务器端部署方案
微服务·nacos·服务端部署
努力搬砖的咸鱼5 小时前
用 Docker 部署你的第一个微服务
docker·微服务·云原生·容器
面汤放盐5 小时前
软件架构指南 Software Architecture Guide
java·微服务·devops
一条咸鱼_SaltyFish6 小时前
Spring Cloud Gateway鉴权空指针惊魂:HandlerMethod为null的深度排查
java·开发语言·人工智能·微服务·云原生·架构
一条咸鱼_SaltyFish1 天前
[Day15] 若依框架二次开发改造记录:定制化之旅 contract-security-ruoyi
java·大数据·经验分享·分布式·微服务·架构·ai编程