欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > 微服务重启优化kafka+EurekaNotificationServerListUpdater

微服务重启优化kafka+EurekaNotificationServerListUpdater

2024/10/24 9:25:11 来源:https://blog.csdn.net/lswandt/article/details/140516763  浏览:    关键词:微服务重启优化kafka+EurekaNotificationServerListUpdater

由于遇到服务重启导致的业务中断等异常,所以计划通过kafka+eureka实现服务下线通知,来尽可能规避这类问题。
如果可以升级spring,则可以考虑nacos等更为方便的方案;

程序优化:
1.默认启用的为 PollingServerListUpdater,所以需要手动启用EurekaNotificationServerListUpdater

@Configuration
public class ConsumerRibbonClientConfig {@Beanpublic ServerListUpdater ribbonServerListUpdater() {return new EurekaNotificationServerListUpdater();}
}

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


@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


@Component
@KafkaListener(topics = GracefulShutdownConfigConstant.KAFKA_TOPIC)
@Slf4j
public class ServiceDowntimeListener {@AutowiredEurekaRefreshUpdater eurekaRefreshUpdater;@KafkaHandlerpublic void onMessage(@Payload String message, Acknowledgment acknowledgment) {log.info("服务关机-接收到其他服务关机信息,message:{}", JSON.toJSONString(message));eurekaRefreshUpdater.refresh();acknowledgment.acknowledge();}
}

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

@Slf4j
@Component
public class GracefulShutdown {@Value("${server.graceful.shutdown.seconds:30}")private Integer serverGracefulShutdownSeconds;@AutowiredEurekaClient eurekaClient;@Value("${spring.application.name}")private String serviceName;@Autowiredprivate KafkaTemplate<Object, String> kafkaTemplate;@PreDestroypublic 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通知的时候,根据服务的部署情况,利用分区+多条通知,来变相实现全广播。

./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来变相实现多服务通知呢?

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com