dubbo简单集成spring

使用maven构建 core-inteface(接口定义),core(服务提供实现),comsumer(服务消费)

1 ,建立core- interface公共api



2 建立服务提供者项目,core  实现interface中接口



pom.xml中引入dubbo ,和  core-interface  使用zookeeper,还需要引入zk的jar

	<dependency>
		    <groupId>org.apache.zookeeper</groupId>
		    <artifactId>zookeeper</artifactId>
		    <version>3.4.9</version>
		</dependency>
  		
  		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>
 	<dependency>
    	<groupId>com.cn.web</groupId>
    	<artifactId>core-interface</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    </dependency>




dubbo-provider.xml 配置 ,( 相关更详细参数配置说明 http://dubbo.io/User+Guide-zh.htm#UserGuide-zh-%3Cdubbo%3Aservice%2F%3E

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans          
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd         
    http://code.alibabatech.com/schema/dubbo          
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
    	<!-- 使用Annotation自动注册Bean,解决事物失效问题:在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解。  -->
	<!-- <context:component-scan base-package="com.cn.core">base-package 如果多个,用“,”分隔
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan> -->
    
    

    <!--  当前应用信息配置  -->
    <dubbo:application name="dubbo-core-service" />
    
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <!-- <dubbo:registry address="multicast://224.1.1.1:1234" /> -->

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://192.168.1.21:2181" />

    <!-- 用dubbo协议在20880端口暴露服务   访问日志路径-->
    <dubbo:protocol name="dubbo" port="20880" accesslog="D:/bar.log"/>
    
     <!-- 用rmi协议在20881端口暴露服务 -->     
    <dubbo:protocol name="rmi" port="20881" /> 
    
    <!-- 声明需要暴露的服务接口 --><!-- 相关更详细参数配置说明  http://dubbo.io/User+Guide-zh.htm#UserGuide-zh-%3Cdubbo%3Aservice%2F%3E-->
    <dubbo:service interface="com.cn.service.UserService"
        ref="userService" protocol="dubbo" delay="-1" timeout="6000" retries="0"/>

    <!-- 和本地bean一样实现服务 也可直接在UserServiceImpl上@Service("userService")--> 
    <!-- <bean id="userService" class="com.cn.core.service.impl.UserServiceImpl" /> -->
</beans>  

接着启动core,提供服务,启动方式 有基本常见一下三种:

1,服务容器是一个standalone的启动程序,因为后台服务不需要Tomcat或JBoss等Web容器的功能,如果硬要用Web容器去加载服务提供方,增加复杂性,也浪费资源。

web.xml中加载 dubbo-provider.xml配置 或者引入到其他applicationContext.xml中,反正能加载到容器即可

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:/dubbo-provider.xml</param-value>
	</context-param>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>


 
2,服务容器只是一个简单的Main方法,并加载一个简单的Spring容器,用于暴露服务。 

package com.cn.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CoreLauncher {
	
	private static Logger logger =  LoggerFactory.getLogger(CoreLauncher.class);

    /**
     * @param args
     */
    public static void main(String[] args) {
        logger.info("开始启动asset");

		// 通过自定义main函数
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				"classpath:dubbo-provider.xml");
		context.start();
		synchronized (CoreLauncher.class) {
            while (true) {
                try {
                	CoreLauncher.class.wait();
                } catch (Throwable e) {
                }
            }
        }	
    }
}
java 应用项目可以打包成一个 jar ,指定一个拥有main函数的CoreLauncher作为你这个 jar 包的程序入口。

tips:eclipse打成可执行jar方式,制定项目鼠标右键-->exoport-->java-->Runable JAR file-->launcher configuration选择项目中的包含main函数的CoreLauncher类,选择export destination路径并设置名字core.jar ,library handling可选择是否包含其他依赖包,这里测试用,所以选择包含其他依赖包,package required lib.........-->Finish

执行jar 命令, Java -jar core.jar运行。


3,服务容器的加载内容可以扩展,内置了spring, jetty, log4j等加载,可通过Container扩展点进行扩展

在classpath目录下配置 dubbo.properties 相关配置

dubbo.container=log4j,spring  
dubbo.application.name=ore-service  
dubbo.application.owner=core_s
dubbo.registry.address=zookeeper://127.0.0.1:2181  
dubbo.protocol.name=dubbo  
dubbo.protocol.port=56432  
dubbo.service.loadbalance=roundrobin  
dubbo.spring.config=classpath:dubbo-provider.xml  
dubbo.log4j.level=DEBUG  
dubbo.log4j.level=INFO  

package com.cn.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CoreLauncher {
	
	private static Logger logger =  LoggerFactory.getLogger(CoreLauncher.class);

    /**
     * @param args
     */
    public static void main(String[] args) {
        logger.info("开始启动asset");

	//第三种  在classpath目录下配置 dubbo.properties 相关配置
	com.alibaba.dubbo.container.Main.main(args);
		
		
    }

}

java应用项目可以打包成一个jar,指定一个拥有main函数的CoreLauncher作为你这个jar包的程序入口。



3 建立项目 consumer 消费服务 




同样引入 core-interface和 dubbo ,zk的jar,注意下dubbo的jar

<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>dubbo</artifactId>
		<version>2.5.3</version>
		<exclusions>
			<exclusion>
				<artifactId>spring</artifactId>
				<groupId>org.springframework</groupId>
			</exclusion>
		</exclusions>
	</dependency>

配置 dubbo-consumer.xml,详细配置说明,参见:http://dubbo.io/User+Guide-zh.htm#UserGuide-zh-%3Cdubbo%3Areference%2F%3E

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans          
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd          
    http://code.alibabatech.com/schema/dubbo          
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">  
    
    <!-- 消费方应用名 -->  
    <dubbo:application name="consumer-app"/> 
     
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->  
   <!--  <dubbo:registry address="multicast://224.1.1.1:1234" />  --> 
    <dubbo:registry address="zookeeper://192.168.1.21:2181" />  
    
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService   check 启动是否检查服务可用 false不检查-->  
    <dubbo:reference id="userService" interface="com.cn.service.UserService" check="false">
    	<!-- <dubbo:method name="selectUsers" timeout="3000" retries="2000"></dubbo:method> -->
    </dubbo:reference>
     
</beans>  

启动consumer服务,访问userController中的getTest1测试 













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奔跑的窝窝牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值