dubbo SpringContainer
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) {
}
}
}
}
}
dubbo SpringContainer的更多相关文章
- Dubbo操作
admin控制台安装 1.控制台下载地址https://github.com/apache/incubator-dubbo/releases 2.找到dubbo-admin 3.修改webapp/WE ...
- dubbox 编译 和 测试
因为 dubbox 并没有发布到maven中央仓库仓库中,所以需要我们自己到官网下载,自己编译,install 到本地. 1. 首先安装git客户端工具 TortoiseGit, 然后使用它将 dub ...
- Running Dubbo On Spring Boot
Dubbo(http://dubbo.io/) 是阿里的开源的一款分布式服务框架.而Spring Boot则是Spring社区这两年致力于打造的简化Java配置的微服务框架. 利用他们各自优势,配置到 ...
- dubbo 解决Multicast java.net.SocketException: No such device
log4j:WARN No appenders could be found for logger (com.alibaba.dubbo.common.logger.LoggerFactory). l ...
- Dubbo认识
Dubbo提供了服务注册.RPC服务调用.调用均衡.服务监控和服务failover等功能 Dubbo框架中有两个重要角色:(服务)提供者和(服务)消费者,这里为了简单起见,将包含了dubbo提供者或消 ...
- Dubbo原理解析-监控
Dubbo发布代码中,自带了一个简易的监控中心实现.对于一般的小业务这个监控中心应该能够满足需求,对于那些大业务量的大公司一般都会有自己的监控中心,更加丰富的功能如常用的报警短信通知等等.这章讲解分析 ...
- Dubbo 入门之二 ——- 项目结构解析
本文主要说明点 概述 背景 需求 架构 Dubbo源代码项目结构 概述 分享 Dubbo 的项目结构 ,通过本文可以大致了解到Dubbo整个项目的结构 背景 将一个项目进行拆分, 进行分布式架构. 需 ...
- dubbo环境搭建与tomcat集成、DEMO示例、常见问题(最完整版本、带管理控制台、监控中心、zookeeper)
以windows为例,linux基本相同,开发环境一般linux,个人环境一般windows(如果不开额外vm的话). 示例以dubbo官方自带demo为例子,进行整合和稍加修改测试. 0.dubbo ...
- Dubbo启动过程(Spring方式)详解
一.使用Spring xml配置方式的启动过程 1. 解析XML,注册Bean 2. 监听Spring事件 3. 启动或关闭dubbo 二.详细过程 1. 解析XML,注册Bean 利用Spring提 ...
随机推荐
- HDU 1176 免费馅饼 (类似数字三角形的题,很经典,值得仔细理解的dp思维)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1176 免费馅饼 Time Limit: 2000/1000 MS (Java/Others) ...
- cocos2d-x开发: 完善接口范例分离模块
在上一篇文章中,我阐述了一下为什么要做分离这种工作,这篇文章没什么重点,只是将上一次没有完善的工作做完.原本我想做到像Lua-tests那样子,能够一次完全显示所有的tests,然后选择要执行的tes ...
- 如何快速找到指定端口被哪个程序占用并释放该端口(解决bindException)
首先打开打开任务管理器,选择性能模块,下方有打开资源监视器,或者直接搜索资源监视器 在资源监视器中点击侦听端口模块,即可看到正在使用网络端口的应用程序名和pid,如果被占用可以直接使用命令行关闭即可 ...
- python3 使用pyinstaller打包可执行程序
步骤 1.使用pip安装pyinstaller pip install pyinstaller 2. 使用pyinstaller打包程序 pyinstaller -F xxx.py pyinstall ...
- Linux-- 查看文件 more与其它
more 翻页查看 用法:more 文件名 nl 显示行号打印(不常用) 1.不打印空白行行号:nl -b t 文件名 类似 cat -b 文件名 2.打印所有行行号:nl -b a 文件名 类似 c ...
- java 一个数字的位数不够怎么在前面加0
import java.text.DecimalFormat; //(1).如果数字1是字符串,如下处理: String str1="1"; DecimalFormat df=ne ...
- python3 用户登录验证的小功能
用户登录验证,记录一下,还需要修改黑名单不合理 #!/usr/bin/env python3 ''' 需求:编写登录接口 1.输入用户名和密码 2.验证用户密码成功后输出欢迎消息 3.3次没有验证通过 ...
- git提交代码速度过慢解决方法(macos)
iterm2中ping下github.com和github.global.ssl.fastly.net $ ping github.com 得到以下 PING github.com (192.30.2 ...
- ruby rspec+jenkins+ci_report持续集成生成junit测试报告
1.加载ci_report gem install ci_reporter_rspec 2.给测试工程编写rakefile require 'ci/reporter/rake/rspec' requi ...
- RUBY惯用方法(转)
RUBY惯用方法 目录 迭代 ||=赋值 程序入口 预设变量和特殊记号 inject 并行赋值 *的匹配 rescue简单用法 命名参数的默认值 精细duck typing控制 获取metaclass ...