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包,如下:
<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,代码如下:
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配置文件,配置生产者暴露服务地址和注册中心,如下
<!-- 提供方应用信息,用于依赖关系 -->
<dubbo:application name="dubbo-demo-provider"/>
<!-- 使用zookeeper注册中心,暴露提供者服务地址 -->
<dubbo:registry address="zookeeper://192.168.0.29:2181"/>
<!-- 使用dubbo协议在8001端口暴露服务 -->
<dubbo:protocol name="dubbo" port="8001" />
<!-- 提供者服务实现 -->
<bean id="demoService" class="com.cn.dubbo.provider.DemoServiceImpl" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.cn.dubbo.provider.IDemoService" ref="demoService"/>
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,代码如下:
<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:application name="dubbo-demo-consumer"/>
<!-- 使用zookeeper注册中心,暴露提供者服务地址 -->
<dubbo:registry address="zookeeper://192.168.0.29:2181"/>
<dubbo:reference id="demoService" interface="com.cn.dubbo.provider.IDemoService" />
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控制台,查看生产者和消费者,生产者和消费者是否发布成功
注:这里的启动步骤说明下:
1、先启动 zookeeper
2、启动dubbo-admin
3、启动生产者,也就是dubbo-demo-provider
4、启动消费者,也就是dubbo-demo-consumer
上述就简单的介绍如何在Linux环境下搭建dubbo服务框架,安装和启动、部署zookeeper的希望对大家有帮助
需要提前准备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包,如下:
<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,代码如下:
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配置文件,配置生产者暴露服务地址和注册中心,如下
<!-- 提供方应用信息,用于依赖关系 -->
<dubbo:application name="dubbo-demo-provider"/>
<!-- 使用zookeeper注册中心,暴露提供者服务地址 -->
<dubbo:registry address="zookeeper://192.168.0.29:2181"/>
<!-- 使用dubbo协议在8001端口暴露服务 -->
<dubbo:protocol name="dubbo" port="8001" />
<!-- 提供者服务实现 -->
<bean id="demoService" class="com.cn.dubbo.provider.DemoServiceImpl" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.cn.dubbo.provider.IDemoService" ref="demoService"/>
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,代码如下:
<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:application name="dubbo-demo-consumer"/>
<!-- 使用zookeeper注册中心,暴露提供者服务地址 -->
<dubbo:registry address="zookeeper://192.168.0.29:2181"/>
<dubbo:reference id="demoService" interface="com.cn.dubbo.provider.IDemoService" />
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控制台,查看生产者和消费者,生产者和消费者是否发布成功
注:这里的启动步骤说明下:
1、先启动 zookeeper
2、启动dubbo-admin
3、启动生产者,也就是dubbo-demo-provider
4、启动消费者,也就是dubbo-demo-consumer
上述就简单的介绍如何在Linux环境下搭建dubbo服务框架,安装和启动、部署zookeeper的希望对大家有帮助