关于dubbox的核心原理和细节会另开一篇文章来介绍,这篇文章主要是从配置和应用层面来介绍dubbox 2.8.4的使用。
1、准备工作
-下载zookeeper
官方地址:http://apache.fayea.com/zookeeper/zookeeper-3.4.6/
下载版本:zookeeper-3.4.6.tar.gz
-下载JDK
下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html
下载版本:JDK1.7.X版本,32位或64位看操作系统
JDK的安装就跳过了,直接百度一大堆,一定记得设置PATH,classpath,JAVA_HOME环境变量
2、解压和安装zookeeper
zookeeper的目录我是放置在/usr/zookeeper-3.4.6/这个位置,所以:
tar -zxvf ./zookeeper-3.4.6.tar.gz
mv ./zookeeper-3.4.6 /usr/zookeeper-3.4.6/
同样的,设置全局环境变量:
export PATH=/usr/zookeeper-3.4.6/bin:$PATH
好的,安装完成了,是不是简单。接下来,我们讲解一下zookeeper的主配置文件。zookeeper的主配置文件所在的地址是:${您的zookeeper安装位置}/conf/zoo.cfg
但是,解压后的zookeeper并没有这个配置文件,有一个名叫zoo_sample.cfg,所以,我们复制一个zoo.cfg文件:
cp /usr/zookeeper-3.4.6/conf/zoo_sample.cfg /usr/zookeeper-3.4.6/conf/zoo.cfg
最后,使用zkServer.sh start命令,启动zookeeper:
[root@vm2 ~]# zkServer.sh start
JMX enabled by default
Using config: /usr/zookeeper-3.4.6/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
1、准备工作
下载dubbox的源代码
下载地址:https://github.com/dangdangdotcom/dubbox
2、编译源码(此处默认大家会用maven了)
运行maven命令:mvn clean install -Dmaven.test.skip=true
将dubbox的源码打包发布到本地仓库,目的是为了获取:dubbo-admin-2.8.4.war和dubbo-monitor.war
懒人下载:http://download.csdn.net/detail/u011282930/9477514
1、部署dubbo-admin管理端,将dubbo-admin-2.8.4.war放置到准备好的tomcat目录
2、修改WEB-INF下dubbo.properties
主要是修改zookeeper的地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
3、运行tomcat
输入地址:http://localhost:8080/dubbo-admin-2.8.4
默认登录账户和密码均为:root
4、登录后首页
到此为止,dubbox的后台管理端就配置成功了
懒人下载:http://download.csdn.net/detail/u011282930/9477515
我们对于dubbo的监控用的是改装后的(页面非常好看)
下载dubbox-monitor的源代码
下载地址:http://git.oschina.net/handu/dubbo-monitor
Dubbo-Monitor配置介绍
1、创建数据库 首先创建名称为monitor数据库,编码格式UTF-8。然后将项目sql文件夹下面的create.sql导入到数据库,生成dubbo_invoke表代表成功导入。
下载地址:http://download.csdn.net/detail/u011282930/9650381
2、编辑项目中application.properties,配置如下:
####Dubbo Settings
dubbo.application.name=dubbo-monitor
dubbo.application.owner=handu.com
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.port=6060
####Database Settings
db.url=jdbc:mysql://:/monitor?prepStmtCacheSize=517&cachePrepStmts=true&autoReconnect=true&characterEncoding=utf-8
db.username=root
db.password=root
db.maxActive=500
####System Manager
manager.username=admin
manager.password=admin
3、打包运行项目 执行maven命令:mvn clean package target文件夹下生成的dubbo-monitor.war即为项目部署文件,将其放置到对应服务器目录下,启动服务器即可。例如:tomcat的webapps文件夹下。
4、访问项目 启动web服务器后,访问地址:http://IP:[port]/dubbo-moniotor,采用配置文件中manager.username和manager.password设置值进行登录。
5、透过dubbox服务管理端,我们会发现多了一个monitor的服务
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.8.4version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubbo-rpc-restartifactId>
<version>2.8.4version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubbo-rpc-webserviceartifactId>
<version>2.8.4version>
dependency>
<dependency>
<groupId>com.101tecgroupId>
<artifactId>zkclientartifactId>
<version>0.2version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.3.6version>
<exclusions>
<exclusion>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.16version>
dependency>
1、定义一个Service接口和相关实现类
定义一个接口,IUserService.java
package service;
public interface IUserService {
public void sayHello();
}
实现这个接口,UserService.java
package service;
public class UserService implements IUserService {
@Override
public void sayHello() {
System.err.println("HELLO WORLD!");
}
}
2、配置这个接口的xml文件,dubbo-provider.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application owner="whb" name="hello-world-app" />
<dubbo:registry address="zookeeper://172.17.84.84:2181"/>
<dubbo:protocol name ="dubbo" port="20880" />
<dubbo:monitor address="127.0.0.1:7070" />
<dubbo:service protocol="dubbo" timeout="2000" connections="100" interface ="service.IUserService" ref="userService" />
<bean id="userService"
class="service.UserService" />
beans>
3、运行发布我们的接口,LuncherProvider.java
package demo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class LuncherProvider {
public static void main(String[] args) throws InterruptedException {
LuncherProvider luncher = new LuncherProvider();
luncher.start();
Thread.sleep(1000 * 60 * 10);
}
void start() {
String configLocation = "classpath*:/dubbo-provider.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(
configLocation);
String[] names = context.getBeanDefinitionNames();
System.out.print("Beans:");
for (String string : names)
System.out.print(string + ",");
System.out.println();
}
}
控制台输出如下内容,为成功
通过dubbox管理端来查看我们的服务,通过下图,我们可以看到我们的服务已经注册到zookeeper了
如何调用我们发布的服务呢?
1、定义一个一模一样的接口,IUserService.java
package service;
public interface IUserService {
public void sayHello();
}
2、配置这个接口调用信息,
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="consumer-of-helloworld-app" />
<dubbo:registry protocol="zookeeper" address="172.17.84.84:2181" />
<dubbo:reference id="userService" interface="service.IUserService" />
beans>
3、测试调用服务的代码,LuncherConsumer.java
package demo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.IUserService;
public class LuncherConsumer {
public static void main(String[] args) throws InterruptedException{
LuncherConsumer luncher=new LuncherConsumer();
String configLocation="classpath*:/dubbo-consumer.xml";
ApplicationContext context =new ClassPathXmlApplicationContext(configLocation);
IUserService us=(IUserService) context.getBean("userService");
String[] names=context.getBeanDefinitionNames();
us.sayHello();
}
}
至此,dubbox的配置和使用就成功了!