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的更多相关文章

  1. Dubbo操作

    admin控制台安装 1.控制台下载地址https://github.com/apache/incubator-dubbo/releases 2.找到dubbo-admin 3.修改webapp/WE ...

  2. dubbox 编译 和 测试

    因为 dubbox 并没有发布到maven中央仓库仓库中,所以需要我们自己到官网下载,自己编译,install 到本地. 1. 首先安装git客户端工具 TortoiseGit, 然后使用它将 dub ...

  3. Running Dubbo On Spring Boot

    Dubbo(http://dubbo.io/) 是阿里的开源的一款分布式服务框架.而Spring Boot则是Spring社区这两年致力于打造的简化Java配置的微服务框架. 利用他们各自优势,配置到 ...

  4. dubbo 解决Multicast java.net.SocketException: No such device

    log4j:WARN No appenders could be found for logger (com.alibaba.dubbo.common.logger.LoggerFactory). l ...

  5. Dubbo认识

    Dubbo提供了服务注册.RPC服务调用.调用均衡.服务监控和服务failover等功能 Dubbo框架中有两个重要角色:(服务)提供者和(服务)消费者,这里为了简单起见,将包含了dubbo提供者或消 ...

  6. Dubbo原理解析-监控

    Dubbo发布代码中,自带了一个简易的监控中心实现.对于一般的小业务这个监控中心应该能够满足需求,对于那些大业务量的大公司一般都会有自己的监控中心,更加丰富的功能如常用的报警短信通知等等.这章讲解分析 ...

  7. Dubbo 入门之二 ——- 项目结构解析

    本文主要说明点 概述 背景 需求 架构 Dubbo源代码项目结构 概述 分享 Dubbo 的项目结构 ,通过本文可以大致了解到Dubbo整个项目的结构 背景 将一个项目进行拆分, 进行分布式架构. 需 ...

  8. dubbo环境搭建与tomcat集成、DEMO示例、常见问题(最完整版本、带管理控制台、监控中心、zookeeper)

    以windows为例,linux基本相同,开发环境一般linux,个人环境一般windows(如果不开额外vm的话). 示例以dubbo官方自带demo为例子,进行整合和稍加修改测试. 0.dubbo ...

  9. Dubbo启动过程(Spring方式)详解

    一.使用Spring xml配置方式的启动过程 1. 解析XML,注册Bean 2. 监听Spring事件 3. 启动或关闭dubbo 二.详细过程 1. 解析XML,注册Bean 利用Spring提 ...

随机推荐

  1. linux挂在新硬盘

    login as: rootroot@192.168.109.128's password:Last login: Fri Mar 22 14:12:08 2019 from 192.168.109. ...

  2. HDU 1165 公式推导题

    题目链接: acm.hdu.edu.cn/showproblem.php?pid=1165 Eddy's research II Time Limit: 4000/2000 MS (Java/Othe ...

  3. C# foreach遇到async和await

    一个简单的列子,需要把一个集合的数据添加到数据库中. 我先这样写了,然后报错了 public async Task<IHttpActionResult> Test([FromUri]str ...

  4. [Zedboard Linux系统移植]-从MACHINE_START開始

    改动自:http://www.cnblogs.com/lknlfy/archive/2012/05/06/2486479.html 内核的启动过程? 3)内核的启动过程? arch/arm/kerne ...

  5. git相关命令

    查看分支:git branch创建分支:git branch <name>切换分支:git checkout <name>创建+切换分支:git checkout -b < ...

  6. Oracle 表空间、段、区和块简述

    数据块(Block) 数据块Block是Oracle存储数据信息的最小单位.注意,这里说的是Oracle环境下的最小单位.Oracle也就是通过数据块来屏蔽不同操作系统存储结构的差异.无论是Windo ...

  7. mac 下安装php7.1 redis

    1.下载phpredis源文件 https://nodeload.github.com/nicolasff/phpredis/zip/master 下载后解压 2.执行命令 phpize  执行后执行 ...

  8. activemq的高级特性:集群实战

    高级特性实战需求 当消费端是多个集群,集群A又包含多个服务. 当每个集群都要接受相同的一批消息,而集群内的每个服务都去分摊消息. 解决办法一:级联 增加一个中转者.但是不是特别的优化,而且性能也不是特 ...

  9. JQuery中事件冒泡

    JQuery中事件冒泡 定义 在一个对象上触发某类事件,就会执行此事件程序,如果没有处理事件就会向这个对象的父级对象传播 直至它被处理,最顶层老大为document对象. 作用 事件冒泡允许多个操作被 ...

  10. redis具体使用

    key 命名规则:不可包含空格和\n 创建方式: set  key value values Strings (Binary-safe strings) Lists Sets Sorted sets ...