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. Dubbo实践(二)架构

    架构 节点角色说明 节点 角色说明 Provider 暴露服务的服务提供方 Consumer 调用远程服务的服务消费方 Registry 服务注册与发现的注册中心 Monitor 统计服务的调用次数和 ...

  2. 404 Note Found队-Alpha3

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:家伟 组员8:政演 组员9:鸿杰 组员10:刘一好 组员11:何宇恒 展示组内最 ...

  3. LeetCode35.搜索插入位置 JavaScript

    给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1,3,5,6], 5 输 ...

  4. 并发编程(三)------并发类容器Copy-On-Write容器

    Copy-On-Write简称COW,是一种用于程序设计中的优化策略.JDK里的COW容器有两种: CopyOnWriteArrayList CopyOnWriteArraySet CopyOnWri ...

  5. Android 心跳呼吸动画

    废话少说,看东西 一个很简单的心跳呼吸的动画,几行代码搞定: 代码: private ImageView ivHart; //图片 AlphaAnimation alphaAnimation = nu ...

  6. vue面试题!!!

    由于公司需要,需要把项目拆分,前端使用vue框架.最近面试vue总结的试题 1:mvvm框架是什么?它和其他框架的区别是什么? mvvm 全称model view viewModel,model数据模 ...

  7. HBase 文件读写过程描述

    HBase 数据读写过程描述 我们熟悉的在 Hadoop 使用的文件格式有许多种,例如: Avro:用于 HDFS 数据序序列化与 Parquet:常见于 Hive 数据文件保存在 HDFS中 HFi ...

  8. PHpStorm + phpstudy 断点调试配置

    步骤: phpstudy     其他菜单 ->打开配置文件 ->打开 php.ini ->找到 XDebug ->XDebug修改成下面文字 [XDebug] :红色  (1 ...

  9. 数据采集与分析的那些事——从数据埋点到AB测试

    作者:网易有数郑栋. 一.为什么企业需要一套完善的用户行为埋点和分析平台 产品初创期间,需要分析天使用户的行为来改进产品,甚至从用户行为中得到新的思路或发现来调整产品方向:产品成长过程,通过对用户行为 ...

  10. MySQL数据库实验:任务二 表数据的插入、修改及删除

    目录 任务二 表数据的插入.修改及删除 一.利用界面工具插入数据 二.数据更新 (一)利用MySQL命令行窗口更新数据 (二)利用Navicat for MySQL客户端工具更新数据 三.数据库的备份 ...