首页 技术 正文
技术 2022年11月18日
0 收藏 445 点赞 4,360 浏览 3786 个字

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) {
}
}
}
}
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,983
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,500
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,344
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,127
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,761
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,838