com.fizzgate.service_registry.RegistryCenterService Maven / Gradle / Ivy
/*
* Copyright (C) 2020 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package com.fizzgate.service_registry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
import org.springframework.stereotype.Service;
import com.fizzgate.config.AggregateRedisConfig;
import com.fizzgate.config.SystemConfig;
import com.fizzgate.util.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* @author hongqiaowei
*/
@Service
public class RegistryCenterService implements ApplicationListener {
private static final Logger LOGGER = LoggerFactory.getLogger(RegistryCenterService.class);
private Map registryCenterMap = new HashMap<>();
@Resource
private ReactiveWebServerApplicationContext applicationContext;
@Resource(name = AggregateRedisConfig.AGGREGATE_REACTIVE_REDIS_TEMPLATE)
private ReactiveStringRedisTemplate rt;
@Resource
private SystemConfig systemConfig;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
Result> result = initRegistryCenter();
if (result.code == Result.FAIL) {
throw new RuntimeException(result.msg, result.t);
}
result = lsnRegistryCenterChange();
if (result.code == Result.FAIL) {
throw new RuntimeException(result.msg, result.t);
}
}
public Result> initRegistryCenter() {
Result> result = Result.succ();
Flux> registryCenterEntries = rt.opsForHash().entries("fizz_registry");
registryCenterEntries.collectList()
.defaultIfEmpty(Collections.emptyList())
.flatMap(
es -> {
if (!es.isEmpty()) {
String json = null;
try {
for (Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy