23 changed files with 250 additions and 117 deletions
@ -0,0 +1,65 @@
|
||||
package net.sopod.soim.entry.http.config; |
||||
|
||||
import com.alibaba.nacos.api.NacosFactory; |
||||
import com.alibaba.nacos.api.exception.NacosException; |
||||
import com.alibaba.nacos.api.naming.NamingService; |
||||
import com.alibaba.nacos.api.naming.listener.Event; |
||||
import com.alibaba.nacos.api.naming.listener.NamingEvent; |
||||
import com.alibaba.nacos.api.naming.pojo.Instance; |
||||
import net.sopod.soim.common.constant.AppConstant; |
||||
import org.apache.dubbo.common.URL; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.boot.context.event.ApplicationReadyEvent; |
||||
import org.springframework.context.ApplicationListener; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.core.Ordered; |
||||
|
||||
import java.util.List; |
||||
import java.util.Properties; |
||||
|
||||
/** |
||||
* ApplicationOnReady |
||||
* |
||||
* @author tmy |
||||
* @date 2022-06-09 11:16 |
||||
*/ |
||||
@Configuration |
||||
public class ApplicationOnReady implements ApplicationListener<ApplicationReadyEvent>, Ordered { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ApplicationOnReady.class); |
||||
|
||||
private final String serverAddress; |
||||
|
||||
public ApplicationOnReady(@Value("${dubbo.registry.address}") String serverAddress) { |
||||
this.serverAddress = URL.valueOf(serverAddress).getAddress(); |
||||
} |
||||
|
||||
@Override |
||||
public void onApplicationEvent(ApplicationReadyEvent readyEvent) { |
||||
// NamingServer 获取 im-entry 实例,连接上,
|
||||
Properties properties = new Properties(); |
||||
properties.put("serverAddr", serverAddress); |
||||
|
||||
NamingService namingService = null; |
||||
try { |
||||
namingService = NacosFactory.createNamingService(properties); |
||||
List<Instance> allInstances = namingService.getAllInstances(AppConstant.APP_IM_ENTRY_NAME); |
||||
logger.info("im-entry instance...: {}", allInstances); |
||||
namingService.subscribe(AppConstant.APP_IM_ENTRY_NAME, (Event event) -> { |
||||
NamingEvent ne = (NamingEvent) event; |
||||
logger.info("namingEvent: {}", ne); |
||||
}); |
||||
|
||||
} catch (NacosException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public int getOrder() { |
||||
return Ordered.HIGHEST_PRECEDENCE; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,47 @@
|
||||
package net.sopod.soim.entry.config; |
||||
|
||||
import org.springframework.context.ApplicationContext; |
||||
|
||||
/** |
||||
* ContextHandler |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-28 15:06 |
||||
*/ |
||||
public class ImEntryAppContext { |
||||
|
||||
private static ApplicationContext applicationContext; |
||||
|
||||
private static String appAddr; |
||||
|
||||
private static String appHost; |
||||
|
||||
private static int appPort; |
||||
|
||||
public static void setContext(ApplicationContext applicationContext) { |
||||
ImEntryAppContext.applicationContext = applicationContext; |
||||
} |
||||
|
||||
public static <T> T getBean(Class<T> beanType) { |
||||
return applicationContext.getBean(beanType); |
||||
} |
||||
|
||||
public static void setAppServiceAddr(String host, int port) { |
||||
ImEntryAppContext.appAddr = host + ":" + port; |
||||
ImEntryAppContext.appHost = host; |
||||
ImEntryAppContext.appPort = port; |
||||
} |
||||
|
||||
public static String getEntryAppAddr() { |
||||
return appAddr; |
||||
} |
||||
|
||||
public static String getAppHost() { |
||||
return appHost; |
||||
} |
||||
|
||||
public static int getAppPort() { |
||||
return appPort; |
||||
} |
||||
|
||||
} |
||||
@ -1,33 +0,0 @@
|
||||
package net.sopod.soim.entry.config; |
||||
|
||||
import org.springframework.context.ApplicationContext; |
||||
|
||||
/** |
||||
* ContextHandler |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-28 15:06 |
||||
*/ |
||||
public class ImEntryAppContextHolder { |
||||
|
||||
private static ApplicationContext applicationContext; |
||||
|
||||
private static String dubboAppServiceAddr; |
||||
|
||||
public static void setContext(ApplicationContext applicationContext) { |
||||
ImEntryAppContextHolder.applicationContext = applicationContext; |
||||
} |
||||
|
||||
public static <T> T getBean(Class<T> beanType) { |
||||
return applicationContext.getBean(beanType); |
||||
} |
||||
|
||||
public static void setDubboAppServiceAddr(String dubboAppServiceAddr) { |
||||
ImEntryAppContextHolder.dubboAppServiceAddr = dubboAppServiceAddr; |
||||
} |
||||
|
||||
public static String getDubboAppServiceAddr() { |
||||
return dubboAppServiceAddr; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,52 @@
|
||||
package net.sopod.soim.entry.config; |
||||
|
||||
import net.sopod.soim.common.constant.AppConstant; |
||||
import net.sopod.soim.entry.registry.ProtoMessageHandlerRegistry; |
||||
import net.sopod.soim.entry.server.EntryServer; |
||||
import net.sopod.soim.entry.worker.WorkerGroup; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.boot.context.event.ApplicationReadyEvent; |
||||
import org.springframework.context.ApplicationListener; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.core.Ordered; |
||||
|
||||
/** |
||||
* ImEntryAppOnReady |
||||
* |
||||
* @author tmy |
||||
* @date 2022-06-09 10:45 |
||||
*/ |
||||
@Configuration |
||||
public class ImEntryAppOnReady implements ApplicationListener<ApplicationReadyEvent>, Ordered { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ImEntryAppOnReady.class); |
||||
|
||||
@Override |
||||
public void onApplicationEvent(ApplicationReadyEvent event) { |
||||
ImEntryAppContext.setContext(event.getApplicationContext()); |
||||
// 注册 protobuf 消息 handler
|
||||
ProtoMessageHandlerRegistry.registerHandlerWithApplicationContext(event.getApplicationContext()); |
||||
|
||||
// 启动 entry-server
|
||||
EntryServerConfig config = ImEntryAppContext.getBean(EntryServerConfig.class); |
||||
|
||||
EntryServer entryServer = new EntryServer( |
||||
config.getName(), |
||||
// 使用dubbo 服务端口偏移量
|
||||
ImEntryAppContext.getAppPort() + AppConstant.IM_ENTRY_SERVER_OFFSET); |
||||
entryServer.startServer(err -> { |
||||
logger.error("EntryServer 启动失败:", err); |
||||
}); |
||||
Runtime.getRuntime().addShutdownHook(new Thread(entryServer::shutdown)); |
||||
|
||||
// 启动消息消费队列组
|
||||
WorkerGroup.init(config.getWorkerSize()); |
||||
} |
||||
|
||||
@Override |
||||
public int getOrder() { |
||||
return Ordered.HIGHEST_PRECEDENCE; |
||||
} |
||||
|
||||
} |
||||
@ -1,26 +0,0 @@
|
||||
package net.sopod.soim.entry.config; |
||||
|
||||
import net.sopod.soim.entry.registry.ProtoMessageHandlerRegistry; |
||||
import org.springframework.beans.BeansException; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.context.ApplicationContextAware; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
/** |
||||
* ApplicationContextInitialed |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-10 22:20 |
||||
*/ |
||||
@Configuration |
||||
public class SpringApplicationContextInitialed implements ApplicationContextAware { |
||||
|
||||
@Override |
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
||||
ImEntryAppContextHolder.setContext(applicationContext); |
||||
|
||||
// 注册 protobuf 消息 handler
|
||||
ProtoMessageHandlerRegistry.registerHandlerWithApplicationContext(applicationContext); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue