您的位置:首页 > 其它

dubbo框架搭建

2016-12-05 20:22 387 查看
一. 在Linux上安装Zookeeper与dubbo-admin管理页

http://doc.okbase.net/congcong68/archive/112508.html

dubbo官网 http://dubbo.io/

二.在工程中使用dubbo

我使用的是当当网的dubbox, https://github.com/dangdangdotcom/dubbox, 下载源码后安装在maven本地仓库。同时dubbo-admin的工程也要安装dubbox的,不然项目中会不断报错。

1. maven中引用dubbo相关的jar

<zkclient_version>0.1</zkclient_version>
<zookeeper_version>3.4.6</zookeeper_version>
<dubbo_version>3.0.1</dubbo_version>

<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient_version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper_version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo_version}</version>
</dependency>


2. 提供者中与spring集成
提供者中使用dubbo-container提供微服务,通过maven打包,把项目打成 tar包的形势进行linux下的部署
1. 只需要把spring的配置文件放在src\main\resources\spring 路径下,系统可以自动加载所有的spring配置文件
2.  \src\main\assembly\conf\dubbo.propertie 路径下进行相关dubbo属性的配置, 下面是dubbo.properties文件


dubbo.container=log4j,spring
dubbo.application.name=out-collection-service
dubbo.application.owner=
#dubbo.registry.address=multicast://224.5.6.7:1234
#注册中心地址
dubbo.registry.address=zookeeper://10.0.4.81:2181
#dubbo.registry.address=redis://127.0.0.1:6379
#dubbo.registry.address=dubbo://127.0.0.1:9090
#注册中心缓存文件,缓存提供者的路径
dubbo.registry.file=/home/dubbo/registry/out-collection-service.properties
#通过注册中心发现监控中心服务
dubbo.monitor.protocol=registry
#协议名
dubbo.protocol.name=dubbo
#服务的端口
dubbo.protocol.port=20850
#负载均衡的策略
dubbo.service.loadbalance=roundrobin


3. spring目录下的dubbo提供者配置 dubbo-provider.xml, 这里面的配置的优先级要高于dubbo.properties文件


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
<dubbo:application name="out-collection-service" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://10.0.4.81:2181" protocol="zookeeper" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" serialization="kryo" port="20880" />
<!-- 声明需要暴露的服务接口 registry="N/A" -->
<dubbo:service interface="com.xx.XXService" ref="XXService"  />
</beans>


启动dubbo服务, eclipse下只需要在main方法下调用如下代码即可

public class TestContainer {
public static void main(String[] args) {
com.alibaba.dubbo.container.Main.main(args);
}
}


打包dubbo服务,使用mavn-assembly-plugin插件进行打包

在src/main/assembly路径下加入assembly.xml文件如下

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> <id>assembly</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/dubbo/META-INF/assembly/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
</fileSet>
<fileSet>
<directory>src/main/assembly/conf</directory>
<outputDirectory>conf</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>conf</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>


pom.xml文件加入如下配置

<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<outputDirectory>${project.build.directory}/dubbo</outputDirectory>
<includes>META-INF/assembly/**</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 打包排除配置文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</excludes>
</configuration>
</plugin>
</plugins>


使用mvn package命令就可以把工程打成tar包,解压后文件结构如下图

配置文件都会被打到conf下, 服务的所有代码都会以jar包放在lib下,运行bin下边的starh.bat即可直接启动服务。

消费者中与spring集成

在消费者中,还是使用传统web项目的形式。要使用提供者的服务只需要两步。

1.把提供者对应的jar包引入到工程中, 当然同时也要引入dubbo对应的jar

2. 加入spring配置 consumer.xml, 在applicationContext.xml引入consumer.xml.

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 
<!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
<dubbo:application name="XXX" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://10.0.4.81:2181" file="/home/deploy/appservers/registry/xx.properties"/>
<dubbo:protocol name="dubbo" serialization="kryo" optimizer="com.touna.collection.SerializationOptimizerImpl" />
<!-- 声明需要暴露的服务接口 registry="N/A" -->
<dubbo:reference interface="com.xx.XXService" id="
"/>

<!-- 全局设置 -->
<dubbo:consumer timeout="600000" check="false" />
</beans>


3, 在本地,与调本地服务一样注入service.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dubbo