dubbo SpringContainer
Spring启动类容器
SPI service provider interfaces 服务提供借口
Singleton 单例
ThreadSafe 线程安全
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.alibaba.dubbo.container.spring; import com.alibaba.dubbo.common.logger.Logger; import com.alibaba.dubbo.common.logger.LoggerFactory; import com.alibaba.dubbo.common.utils.ConfigUtils; import com.alibaba.dubbo.container.Container; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * SpringContainer. (SPI, Singleton, ThreadSafe) */ public class SpringContainer implements Container { public static final String SPRING_CONFIG = "dubbo.spring.config";//首先加载配置文件位置 通过dubbo配置文件配置或者java启动命令-D配置 找不到则加载 DEFAULT_SPRING_CONFIG public static final String DEFAULT_SPRING_CONFIG = "classpath*:META-INF/spring/*.xml";//spring相关配置文件默认存放位置 private static final Logger logger = LoggerFactory.getLogger(SpringContainer.class); 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); } } }
如果直接使用SpringContainer 则需要把相应的配置文件放到相应的位置,SpringContainer使用common log,
一般 通过实现Container接口,自定义加载配置文件,slf4j接口使用日志。可以按照SpringContainer的方式实现start stop方法,start方法一般添加shutdownhook
package org.multitest.dubbo; import com.alibaba.dubbo.container.Container; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; /** * DubboServer */ public class DubboServer implements Container { private static final Logger logger = LoggerFactory.getLogger(DubboServer.class); private ClassPathXmlApplicationContext context; @Override public void start(){ context = new ClassPathXmlApplicationContext(new String[] {"server.xml"}); context.start(); context.registerShutdownHook(); logger.info("service start success"); } @Override public void stop() { try { if (context != null) { context.stop(); context.close(); context = null; } logger.info("service stop success"); } catch (Throwable e) { logger.error(e.getMessage(), e); } } private static volatile boolean running = true; public static void main(String[] args) { try{ Container container = new DubboServer(); logger.info("Use container type(" + Arrays.toString(args) + ") to run dubbo serivce."); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { try { container.stop(); logger.info("Dubbo " + container.getClass().getSimpleName() + " stopped!"); } catch (Throwable t) { logger.error(t.getMessage(), t); } synchronized (DubboServer.class) { running = false; DubboServer.class.notify(); } } }); 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) { logger.error(e.getMessage(), e); System.out.println(e); System.exit(1); } synchronized (DubboServer.class) { while (running) { try { DubboServer.class.wait(); } catch (Throwable e) { } } } } }