Linux环境搭建Dubbo框架服务
Linux环境下搭建Dubbo框架服务介绍
需要提前准备JDK、zookeeper、dubbo-admin的WAR包
dubbo-admin这里就不用介绍了,在我的上一篇文章中介绍过了,
安装zookeeper
1、到zookeeper官网下载,下载地址:http://zookeeper.apache.org/releases.html
2、解压执行命令:tar-zxvfzookeeper-3.4.8.tar.gzzookeeper-3.4.8
3、进入到conf目录下,拷贝zoo_sample.cfg,并重命名为zoo.cfg,执行的命令为cpzoo_sample.cfgzoo.cfg即可
4、编辑zoo.cfg,修改dataDir为
dataDir=/home/test/zookeeper/data
dataLogDir=/home/test/zookeeper/logs
启动zookeeper
1、进入zookeeper的bin目录下,执行命令./zkServer.shstart
2、在bin目录下,执行命令./zkCli.sh-server服务器IP:2181,这是测试是否启动zookeeper,若zkClient已经连接上了的话,表示已启动zookeeper
关闭zookeepr
1、进入zookeeper的bin目录下,执行命令./zkServer.shstop即可
启动tomcat下的dubbo-admin
创建dubbo-demo-provider生产者
1、创建maven项目,项目名字为dubbo-demo-provider
2、修改pom文件,添加JAR包,如下:
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<build>
<finalName>dubbo-demo-provider</finalName>
</build>
3、创建接口类IDemoService,代码如下:
packagecom.cn.dubbo.provider;
publicinterfaceIDemoService{
publicStringsayHello(Stringname);
}
4、创建接口实现类DemoServiceImpl,用于实现接口中的方法,代码如下:
packagecom.cn.dubbo.provider;
publicclassDemoServiceImplimplementsIDemoService{
publicStringsayHello(Stringname)
{
Stringresult="Hello"+name+"providersayHellomethodsuccess";
System.out.println("providersayHellomethodinvoke");
returnresult;
}
}
5、创建dubbo-demo-provider.xml配置文件,配置生产者暴露服务地址和注册中心,如下
<!--提供方应用信息,用于依赖关系-->
<dubbo:applicationname="dubbo-demo-provider"/>
<!--使用zookeeper注册中心,暴露提供者服务地址-->
<dubbo:registryaddress="zookeeper://192.168.0.29:2181"/>
<!--使用dubbo协议在8001端口暴露服务-->
<dubbo:protocolname="dubbo"port="8001"/>
<!--提供者服务实现-->
<beanid="demoService"class="com.cn.dubbo.provider.DemoServiceImpl"/>
<!--声明需要暴露的服务接口-->
<dubbo:serviceinterface="com.cn.dubbo.provider.IDemoService"ref="demoService"/>
6、创建TEST类,用于启动生产者,加载配置文件,代码如下:
packagecom.cn.dubbo.test;
importjava.io.IOException;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
publicclassProviderTest{
publicstaticvoidmain(String[]args)throwsIOException{
ClassPathXmlApplicationContextcontext=newClassPathXmlApplicationContext("classpath:dubbo-demo-provider.xml");
context.start();
System.in.read();
}
}
创建dubbo-demo-consumer消费者
1、创建maven项目,项目名字为dubbo-demo-consumer
2、修改pom文件,这里需要依赖dubbo-demo-provider,代码如下:
<dependencies>
<dependency>
<groupId>com.cn.dubbo</groupId>
<artifactId>dubbo-demo-provider</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<build>
<finalName>dubbo-demo-consumer</finalName>
</build>
3、创建dubbo-demo-consumer.xml,用于配置消费者消费生产者暴露的服务,代码如下:
<!--提供方应用信息,用于依赖关系-->
<dubbo:applicationname="dubbo-demo-consumer"/>
<!--使用zookeeper注册中心,暴露提供者服务地址-->
<dubbo:registryaddress="zookeeper://192.168.0.29:2181"/>
<dubbo:referenceid="demoService"interface="com.cn.dubbo.provider.IDemoService"/>
4、创建TEST类,用于调用生产者服务,代码如下:
packagecom.cn.dubbo.test;
importjava.io.IOException;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importcom.cn.dubbo.provider.IDemoService;
publicclassConsumerTest{
publicstaticvoidmain(String[]args)throwsIOException{
ClassPathXmlApplicationContextcontext=newClassPathXmlApplicationContext("classpath:dubbo-demo-consumer.xml");
context.start();
IDemoServiceproviderImpl=(IDemoService)context.getBean("demoService");
Stringresult=providerImpl.sayHello("bb");
System.out.println(result);
System.in.read();
}
}
最后登录到dubbo-admin控制台,查看生产者和消费者,生产者和消费者是否发布成功
注:这里的启动步骤说明下:
1、先启动zookeeper
2、启动dubbo-admin
3、启动生产者,也就是dubbo-demo-provider
4、启动消费者,也就是dubbo-demo-consumer
上述就简单的介绍如何在Linux环境下搭建dubbo服务框架,安装和启动、部署zookeeper的希望对大家有帮助