深圳设计网站公司,互联网服务平台投诉中心,青岛专业做网站的公司有哪些,网站服务器租用技巧由于遇到服务重启导致的业务中断等异常#xff0c;所以计划通过kafkaeureka实现服务下线通知#xff0c;来尽可能规避这类问题。 如果可以升级spring#xff0c;则可以考虑nacos等更为方便的方案#xff1b; 程序优化#xff1a; 1.默认启用的为 PollingServerListUpdater… 由于遇到服务重启导致的业务中断等异常所以计划通过kafkaeureka实现服务下线通知来尽可能规避这类问题。 如果可以升级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(EurekaRefreshUpdatere.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 KafkaTemplateObject, 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_service2.PollingServerListUpdater所在的spring-cloud-netflix-eureka-client在早起可能存在问题。具体详见 EurekaNotificationServerListUpdater启用后出现 Connection refused (Connection refused)
ps 需要注意下程序版本以及kafka版本防止某些方法不适用。 如果高版本kafka 是否可以通过指定不同的groupid来变相实现多服务通知呢