Dubbo开发流程

dubbo的具体介绍这里就不多说了,不了解的看官网,http://dubbo.io/ ;这里就结合自己做过的项目,给大家分享一下dubbo开发以及发布的大致流程。

项目目录结构

  1. dubbo-service-workschool为maven构建的普通java工程,作为服务接口,专门提供一些消费端要使用的服务,注意,最好还要将一些公共使用的工具类添加在此工程中。此工程在发布的时候需要单独打包成jar,这个jar在服务接口实现端和消费端要共享,也就是说,要在dubbo-provider-workschool和dubbo-provider-workschool工程中引入进来。
  2. dubbo-provider-workschool为spring+mybatis工程,提供对dubbo-service-workschool接口的实现,这里放dao层和serviceImpl(接口实现)层,另外还要提供一个特殊类,就是用于启动服务的main类。这里我注重说一下,就是最好要用dubbo自带的容器启动,不要用自己写的加载类,如下:
public class Provider {

	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "META-INF\spring\spring.xml" });
		context.start();

		System.out.println("服务端启动成功");  
		while(true){
			System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
		}
	}

}

因为我在尝试发布的时候,输入流的阻塞未起到作用,导致当所有服务注册进去之后,服务又突然消失。

推荐使用如下启动服务端:

public static void main(String[] args) throws Exception {
		com.alibaba.dubbo.container.Main.main(args);
	}
服务启动不需要笨重的第三方容器,因为dubbo自带了容器启动的实现,共有四种方式:JettyContainer,Log4jContainer,LogbackContainer,还有SpringContainer。我使用的是SpringContainer。SpringContainer源码:

/*
 * Copyright 1999-2011 Alibaba Group.
 *  
 * Licensed 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 org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.dubbo.common.extension.ExtensionLoader;
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;

/**
 * SpringContainer. (SPI, Singleton, ThreadSafe)
 * 
 * @author william.liangf
 */
public class SpringContainer implements Container {

    private static final Logger logger = LoggerFactory.getLogger(SpringContainer.class);

    public static final String SPRING_CONFIG = "dubbo.spring.config";
    
    public static final String DEFAULT_SPRING_CONFIG = "classpath*:META-INF/spring/*.xml";

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

}
这里有一点,就是如果你没有配置dubbo.property文件的话,即未指定key值dubbo.spring.config路径的话,默认初始化你工程的META-INF/spring/*.xml目录下所有文件。



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值