Search in sources :

Example 1 with DistributedDatabaseOperateImpl

use of com.alibaba.nacos.config.server.service.repository.embedded.DistributedDatabaseOperateImpl in project nacos by alibaba.

the class BaseClusterTest method run.

private static void run(final int index, final CountDownLatch latch, final Class<?> cls) {
    Runnable runnable = () -> {
        try {
            EnvUtil.setIsStandalone(false);
            final String path = Paths.get(System.getProperty("user.home"), "/nacos-" + index + "/").toString();
            DiskUtils.deleteDirectory(path);
            System.setProperty("nacos.home", path);
            System.out.println("nacos.home is : [" + path + "]");
            Map<String, Object> properties = new HashMap<>();
            properties.put("server.port", "884" + (7 + index));
            properties.put("nacos.home", path);
            properties.put("nacos.logs.path", Paths.get(System.getProperty("user.home"), "nacos-" + index, "/logs/").toString());
            properties.put("spring.jmx.enabled", false);
            properties.put("nacos.core.snowflake.worker-id", index + 1);
            MapPropertySource propertySource = new MapPropertySource("nacos_cluster_test", properties);
            ConfigurableEnvironment environment = new StandardServletEnvironment();
            environment.getPropertySources().addFirst(propertySource);
            SpringApplication cluster = new SpringApplicationBuilder(cls).web(WebApplicationType.SERVLET).environment(environment).properties(clusterInfo).properties("embeddedStorage=true").build();
            ConfigurableApplicationContext context = cluster.run();
            DistributedDatabaseOperateImpl operate = context.getBean(DistributedDatabaseOperateImpl.class);
            CPProtocol protocol = context.getBean(CPProtocol.class);
            protocol.protocolMetaData().subscribe(operate.group(), MetadataKey.LEADER_META_DATA, o -> {
                ProtocolMetaData.ValueItem item = (ProtocolMetaData.ValueItem) o;
                System.out.println("node : 884" + (7 + index) + "-> select leader is : " + item.getData());
                if (FINISHED[index].compareAndSet(false, true)) {
                    latch.countDown();
                }
            });
            new Thread(() -> {
                try {
                    Thread.sleep(5000L);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (FINISHED[index].compareAndSet(false, true)) {
                        latch.countDown();
                    }
                }
            });
            applications.put(String.valueOf(properties.get("server.port")), context);
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            latch.countDown();
        }
    };
    runnable.run();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) NotifyCenter(com.alibaba.nacos.common.notify.NotifyCenter) HttpClientBeanHolder(com.alibaba.nacos.common.http.HttpClientBeanHolder) Subscriber(com.alibaba.nacos.common.notify.listener.Subscriber) BeforeClass(org.junit.BeforeClass) URL(java.net.URL) NacosFactory(com.alibaba.nacos.api.NacosFactory) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) DistributedDatabaseOperateImpl(com.alibaba.nacos.config.server.service.repository.embedded.DistributedDatabaseOperateImpl) ProtocolMetaData(com.alibaba.nacos.consistency.ProtocolMetaData) SpringApplication(org.springframework.boot.SpringApplication) URLClassLoader(java.net.URLClassLoader) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Map(java.util.Map) AfterClass(org.junit.AfterClass) Logger(org.slf4j.Logger) Properties(java.util.Properties) PropertyKeyConst(com.alibaba.nacos.api.PropertyKeyConst) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) RaftDbErrorEvent(com.alibaba.nacos.config.server.model.event.RaftDbErrorEvent) EnvUtil(com.alibaba.nacos.sys.env.EnvUtil) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) InetUtils(com.alibaba.nacos.sys.utils.InetUtils) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) DiskUtils(com.alibaba.nacos.sys.utils.DiskUtils) Event(com.alibaba.nacos.common.notify.Event) Paths(java.nio.file.Paths) MapPropertySource(org.springframework.core.env.MapPropertySource) WebApplicationType(org.springframework.boot.WebApplicationType) MetadataKey(com.alibaba.nacos.consistency.cp.MetadataKey) CPProtocol(com.alibaba.nacos.consistency.cp.CPProtocol) NamingService(com.alibaba.nacos.api.naming.NamingService) DistributedDatabaseOperateImpl(com.alibaba.nacos.config.server.service.repository.embedded.DistributedDatabaseOperateImpl) CPProtocol(com.alibaba.nacos.consistency.cp.CPProtocol) SpringApplication(org.springframework.boot.SpringApplication) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MapPropertySource(org.springframework.core.env.MapPropertySource) ProtocolMetaData(com.alibaba.nacos.consistency.ProtocolMetaData) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

NacosFactory (com.alibaba.nacos.api.NacosFactory)1 PropertyKeyConst (com.alibaba.nacos.api.PropertyKeyConst)1 NamingService (com.alibaba.nacos.api.naming.NamingService)1 HttpClientBeanHolder (com.alibaba.nacos.common.http.HttpClientBeanHolder)1 NacosRestTemplate (com.alibaba.nacos.common.http.client.NacosRestTemplate)1 Event (com.alibaba.nacos.common.notify.Event)1 NotifyCenter (com.alibaba.nacos.common.notify.NotifyCenter)1 Subscriber (com.alibaba.nacos.common.notify.listener.Subscriber)1 RaftDbErrorEvent (com.alibaba.nacos.config.server.model.event.RaftDbErrorEvent)1 DistributedDatabaseOperateImpl (com.alibaba.nacos.config.server.service.repository.embedded.DistributedDatabaseOperateImpl)1 ProtocolMetaData (com.alibaba.nacos.consistency.ProtocolMetaData)1 CPProtocol (com.alibaba.nacos.consistency.cp.CPProtocol)1 MetadataKey (com.alibaba.nacos.consistency.cp.MetadataKey)1 EnvUtil (com.alibaba.nacos.sys.env.EnvUtil)1 DiskUtils (com.alibaba.nacos.sys.utils.DiskUtils)1 InetUtils (com.alibaba.nacos.sys.utils.InetUtils)1 File (java.io.File)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 Paths (java.nio.file.Paths)1