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提 ...
随机推荐
- JNI由浅入深_10_JNI 综合开发
1.使用ndk-build时如果找不到某个类,可以使用下面两种方法解决: 1.1 进入src目录 D:\project3\JNIAndroid\src>set classpath=D:\proj ...
- NodeJS平台下的前后端文件共享
一.前后端文件共享的需要背景——为什么需要共享? 项目基本JS/NodeJS全端开发,有部分代码前后端都需要用得到 有一些配置是在前后端都需要用得到的 区别其他开发平台,NodeJS平台下的前后端文件 ...
- 《Python高性能编程》——列表、元组、集合、字典特性及创建过程
这里的内容仅仅是本人阅读<Python高性能编程>后总结的一些知识,用于自己更好的了解Python机制.本人现在并不从事计算密集型工作:人工智能.数据分析等.仅仅只是出于好奇而去阅读这本书 ...
- ztz11的noip模拟赛T1:愤怒的XiaoX
链接: https://www.luogu.org/problemnew/show/U47231 思路: 这道题其实就是一道双Lazy线段树裸题 因为我们知道,当k一定时,取反偶数次最后k位等于不取反 ...
- MySQL-5.5.32 配置文件优化详解
目录 MySQL-5.5.32 配置文件优化详解 一.配置文件说明 2.my-medium.cnf 3.my-large.cnf 4.my-huge.cnf 5.my-innodb-heavy-4G. ...
- mysql 生成UUID() 即 ORACLE 中的guid()函数
MYSQL 生成UUID 即 guid 函数-- 带 - 的UUIDselect UUID() -- 去掉 - 的UUIDselect replace(uuid(),'-','') 一个表的数据插入另 ...
- Windows Redis 取消保护模式C#进行访问
运行redis服务程序和客户端程序设置即可. config set protected-mode “no”
- 课时14.DTD文档声明上(掌握)
1.什么是DTD文档声明? 由于HTML有很多格版本的规范,每个版本的规范之间又又一些差异,所以为了让浏览器能够正确的编译/解析/渲染我们的网页,我们需要在HTML文件的第一行告诉浏览器,我们当前这个 ...
- laravel5.5源码笔记(一、入口应用的初始化)
laravel的项目入口文件index.php如下 define('LARAVEL_START', microtime(true)); require __DIR__.'/../vendor/auto ...
- PCB布线设计(1)
在PCB设计的时候,初学的时候对布线设计一无所知,那个时候老师布置 AT91SAM7X-开发板 作为学习例板 ,最终采用自动布线的结果如下 也并非全为自动布线,自动布线对于这种元器件稍多的很难全部 ...