The container module of dubbo source code research

   The dubbo-container module is the first module in the dubbo startup sequence. The dubbo-container module is a container module, and the related configuration of the dobbo-config module is read through the dubbo-container module.
  
/**
 * Container. (SPI, Singleton, ThreadSafe)
 *
 * @author william.liangf
 */
@SPI("spring")
public interface Container {
    
    /**
     * start.
     */
    void start();
    
    /**
     * stop.
     */
    void stop();

}

  The container interface is very simple, with only two methods, start and stop. Note that both methods are void and do not throw checked exceptions.
Careful children's shoes may also find the above notes, spi, Singleton, ThreadSafe. Explain that the interface is based on the spi mechanism, singleton, and thread-safe.
  The main implementation of the Container interface is

  JavaConfigContainer, which is based on spring's javaconfig, and javaconfig is the main configuration mode after spring 4. If you use spring boot's children's shoes to integrate with dubbo and use zero configuration, you can consider this class.
  SpringContainer mainly specifies the default configuration file path, and starts the spring container through ClassPathXmlApplicationContext.
**
 * SpringContainer. (SPI, Singleton, ThreadSafe)
 *
 * @author william.liangf
 */
public class SpringContainer implements Container {

    private static final Logger logger = LoggerFactory.getLogger(SpringContainer.class);

    public static final String SPRING_CONFIG = "dubbo.spring.config";
    
    public static final String DEFAULT_SPRING_CONFIG = "classpath*:META-INF/spring/*.xml";

    static ClassPathXmlApplicationContext context;
    
    public static ClassPathXmlApplicationContext getContext() {
		return context;
	}

	public void start() {
        String configPath = ConfigUtils.getProperty(SPRING_CONFIG);
        if (configPath == null || configPath.length() == 0) {
            configPath = DEFAULT_SPRING_CONFIG;
        }
        context = new ClassPathXmlApplicationContext(configPath.split("[,\\s]+"));
        context.start();
    }

    public void stop() {
        try {
            if (context != null) {
                context.stop();
                context.close();
                context = null;
            }
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
        }
    }

}


  Several other implementations are similar, focusing on com.alibaba.dubbo.container.Main. This is the main function entry that comes with dubbo.
public static void main(String[] args) {
        try {
            if (args == null || args.length == 0) {
                String config = ConfigUtils.getProperty(CONTAINER_KEY, loader.getDefaultExtensionName());
                args = Constants.COMMA_SPLIT_PATTERN.split(config);
            }
            
            final List<Container> containers = new ArrayList<Container>();
            for (int i = 0; i < args.length; i ++) {
                containers.add(loader.getExtension(args[i]));
            }
            logger.info("Use container type(" + Arrays.toString(args) + ") to run dubbo serivce.");
            
            if ("true".equals(System.getProperty(SHUTDOWN_HOOK_KEY))) {
	            Runtime.getRuntime().addShutdownHook(new Thread() {
	                public void run() {
	                    for (Container container : containers) {
	                        try {
	                            container.stop();
	                            logger.info("Dubbo " + container.getClass().getSimpleName() + " stopped!");
	                        } catch (Throwable t) {
	                            logger.error(t.getMessage(), t);
	                        }
	                        synchronized (Main.class) {
	                            running = false;
	                            Main.class.notify();
	                        }
	                    }
	                }
	            });
            }
            
            for (Container container : containers) {
                container.start();
                logger.info("Dubbo " + container.getClass().getSimpleName() + " started!");
            }
            System.out.println(new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]").format(new Date()) + " Dubbo service server started!");
        } catch (RuntimeException e) {
            e.printStackTrace ();
            logger.error(e.getMessage(), e);
            System.exit(1);
        }
        synchronized (Main.class) {
            while (running) {
                try {
                    Main.class.wait();
                } catch (Throwable e) {
                }
            }
        }
    }


  This method implements graceful shutdown by hooking Runtime.getRuntime().addShutdownHook, and also supports loading extension points through startup parameters, containers.add(loader.getExtension(args[i])). But there is no CommandLineRunner interface similar to spring boot, which performs some initialization actions after the container is started. It is recommended to use dubbo's children's shoes if there is no special requirement, you can use this class as the program startup class.

 


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326560761&siteId=291194637