[size=large]Linux环境下搭建Dubbo框架服务介绍
需要提前准备JDK、zookeeper、dubbo-admin的WAR包
dubbo-admin这里就不用介绍了,在我的上一篇文章中介绍过了,
文章地址:http://kuailenanhaier.iteye.com/blog/2329575
安装zookeeper
1、到zookeeper官网下载,下载地址:http://zookeeper.apache.org/releases.html
2、解压执行命令:tar -zxvf zookeeper-3.4.8.tar.gz zookeeper-3.4.8
3、进入到conf目录下,拷贝 zoo_sample.cfg,并重命名为zoo.cfg,执行的命令为 cp zoo_sample.cfg zoo.cfg 即可
4、编辑 zoo.cfg,修改dataDir为
dataDir=/home/test/zookeeper/data
dataLogDir=/home/test/zookeeper/logs
启动zookeeper
1、进入zookeeper的 bin 目录下,执行命令 ./zkServer.sh start
2、在bin目录下,执行命令 ./zkCli.sh -server 服务器IP:2181,这是测试是否启动zookeeper,若zkClient已经连接上了的话,表示已启动zookeeper
关闭zookeepr
1、进入zookeeper的 bin 目录下,执行命令 ./zkServer.sh stop 即可
启动tomcat下的dubbo-admin
创建dubbo-demo-provider 生产者
1、创建maven项目,项目名字为dubbo-demo-provider
2、修改pom文件,添加JAR包,如下:
com.alibaba
dubbo
2.5.3
org.springframework
spring-core
4.3.3.RELEASE
org.springframework
spring-context
4.3.3.RELEASE
org.springframework
spring-context-support
4.3.3.RELEASE
org.springframework
spring-beans
4.3.3.RELEASE
org.apache.zookeeper
zookeeper
3.4.9
com.github.sgroschupf
zkclient
0.1
dubbo-demo-provider
3、创建接口类IDemoService,代码如下:
package com.cn.dubbo.provider;
public interface IDemoService {
public String sayHello(String name);
}
4、创建接口实现类DemoServiceImpl,用于实现接口中的方法,代码如下:
package com.cn.dubbo.provider;
public class DemoServiceImpl implements IDemoService{
public String sayHello(String name)
{
String result = "Hello "+name+" provider sayHello method success";
System.out.println("provider sayHello method invoke");
return result;
}
}
5、创建 dubbo-demo-provider.xml配置文件,配置生产者暴露服务地址和注册中心,如下
6、创建TEST类,用于启动生产者,加载配置文件,代码如下:
package com.cn.dubbo.test;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ProviderTest {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-demo-provider.xml");
context.start();
System.in.read();
}
}
创建dubbo-demo-consumer消费者
1、创建maven项目,项目名字为dubbo-demo-consumer
2、修改pom文件,这里需要依赖dubbo-demo-provider,代码如下:
com.cn.dubbo
dubbo-demo-provider
${project.parent.version}
com.alibaba
dubbo
2.5.3
org.springframework
spring-core
4.3.3.RELEASE
org.springframework
spring-context
4.3.3.RELEASE
org.springframework
spring-context-support
4.3.3.RELEASE
org.springframework
spring-beans
4.3.3.RELEASE
org.apache.zookeeper
zookeeper
3.4.9
com.github.sgroschupf
zkclient
0.1
dubbo-demo-consumer
3、创建dubbo-demo-consumer.xml,用于配置消费者消费生产者暴露的服务,代码如下:
4、创建TEST类,用于调用生产者服务,代码如下:
package com.cn.dubbo.test;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cn.dubbo.provider.IDemoService;
public class ConsumerTest {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-demo-consumer.xml");
context.start();
IDemoService providerImpl = (IDemoService)context.getBean("demoService");
String result = providerImpl.sayHello("bb");
System.out.println(result);
System.in.read();
}
}
最后登录到dubbo-admin控制台,查看生产者和消费者,生产者和消费者是否发布成功
[color=red]注:这里的启动步骤说明下:
1、先启动 zookeeper
2、启动dubbo-admin
3、启动生产者,也就是dubbo-demo-provider
4、启动消费者,也就是dubbo-demo-consumer
上述就简单的介绍如何在Linux环境下搭建dubbo服务框架,安装和启动、部署zookeeper的希望对大家有帮助[/color][/size]