旅游網(wǎng)站怎么做的seo快排公司哪家好
由于遇到服務(wù)重啟導(dǎo)致的業(yè)務(wù)中斷等異常,所以計劃通過kafka+eureka實現(xiàn)服務(wù)下線通知,來盡可能規(guī)避這類問題。
如果可以升級spring,則可以考慮nacos等更為方便的方案;
程序優(yōu)化:
1.默認(rèn)啟用的為 PollingServerListUpdater,所以需要手動啟用EurekaNotificationServerListUpdater
@Configuration
public class ConsumerRibbonClientConfig {@Beanpublic ServerListUpdater ribbonServerListUpdater() {return new EurekaNotificationServerListUpdater();}
}
2.需要觸發(fā)PollingServerListUpdater中的更新,則需要先觸發(fā)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.服務(wù)關(guān)機(jī)listener
@Component
@KafkaListener(topics = GracefulShutdownConfigConstant.KAFKA_TOPIC)
@Slf4j
public class ServiceDowntimeListener {@AutowiredEurekaRefreshUpdater eurekaRefreshUpdater;@KafkaHandlerpublic void onMessage(@Payload String message, Acknowledgment acknowledgment) {log.info("服務(wù)關(guān)機(jī)-接收到其他服務(wù)關(guān)機(jī)信息,message:{}", JSON.toJSONString(message));eurekaRefreshUpdater.refresh();acknowledgment.acknowledge();}
}
4.自己關(guān)機(jī)發(fā)送消息通知
@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");}
}
腳本優(yōu)化
在服務(wù)啟動腳本中,要注意不可使用kill -9 結(jié)束服務(wù)進(jìn)程,需要使用kill -15 讓服務(wù)有一定的存活時間。來處理完成已有的請求。
問題
1.kafka通過group分組,如果同一組則只能收到一條信息。如果同一服務(wù)部署兩個節(jié)點,則不能很好的都通知到位,所以在創(chuàng)建kafka通知的時候,根據(jù)服務(wù)的部署情況,利用分區(qū)+多條通知,來變相實現(xiàn)全廣播。
./kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 3 --partitions 2 --topic shutdown_service
2.PollingServerListUpdater所在的spring-cloud-netflix-eureka-client在早起可能存在問題。具體詳見:
EurekaNotificationServerListUpdater啟用后出現(xiàn) Connection refused (Connection refused)
ps:
需要注意下程序版本以及kafka版本,防止某些方法不適用。
如果高版本kafka 是否可以通過指定不同的groupid來變相實現(xiàn)多服務(wù)通知呢?