关于分布式框架DUBBO的学习:服务注册与消费(二)
文章目录
由于DUBBO是一个高性能的RPC框架,用于进行远程服务的注册消费。以一个简单案例,来阐述DUBBO框架具体应该如何使用。
1、进行DUBBO框架的安装与部署,请参阅:https://segmentfault.com/a/11...
2、项目的依赖配置
因为使用的是Maven项目管理工具,所以要构建Maven的依赖配置。开发工具我这里使用的是Ideal,当然也可以使用Eclipse;
具体配置如下:
<dependencies>
<!--Spring框架的核心依赖包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!--Apache Commons的通用工具包-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!--复杂对象的映射的工具包-->
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
</dependency>
<!--spring mvc依赖包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!--Dubbo工具包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.3</version>
<!--<exclusions>-->
<!--<exclusion>-->
<!--<groupId>org.jboss.netty</groupId>-->
<!--<artifactId>netty</artifactId>-->
<!--</exclusion>-->
<!--</exclusions>-->
</dependency>
<!--Zookeeper工具包-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.12</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--zookeeper客户端的工具包-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!--curator框架包,主要是为了让Java更为简单方便的操作Zookeeper服务器-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>
<!--日志处理工具包-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
<!--Json处理工具包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!--单元测试包->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3、编写服务开放注册接口
4、编写服务提供者
public class ProducerMain {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ctx = null;
try {
ctx = new ClassPathXmlApplicationContext("spring-dubbo-provider.xml");
ctx.start();
System.in.read();
}catch (Exception ex){
System.out.println(ex.getCause().getMessage());
}finally {
if(ctx != null){
ctx.close();
}
}
}
}
编辑配置文件:spring-dubbo-provider.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: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="hello-world-app-provider"></dubbo:application>
<dubbo:registry address="zookeeper://192.168.237.131:2181?backup=192.168.237.131:2182,192.168.237.131:2183"/>
<dubbo:monitor protocol="registry"/>
<dubbo:protocol name="dubbo" port="20881"/>
<dubbo:service interface="接口的包路径.SayHello" ref="sayHelloImpl"/>
<bean id="sayHelloImpl" class="服务提供者的包路径.SayHelloImpl"/>
</beans>
5、编写服务消费者
配置spring-dubbo-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: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="hello-world-app-consumer"></dubbo:application>
<!--<dubbo:registry address="multicast://224.5.6.7:2181"/>-->
<dubbo:registry address="zookeeper://192.168.237.131:2181?backup=192.168.237.131:2182,192.168.237.131:2183"/>
<dubbo:protocol name="dubbo" port="20881"/>
<dubbo:reference id="sayHello" interface="cn.com.dlut.lgzs.dubbo.api.SayHello"/>
</beans>
6、服务的启动与调试:
(1)服务提供者进行服务注册:
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ctx = null;
try {
ctx = new ClassPathXmlApplicationContext("spring-dubbo-provider.xml");
ctx.start();
System.in.read();
}catch (Exception ex){
System.out.println(ex.getCause().getMessage());
}finally {
if(ctx != null){
ctx.close();
}
}
}
(2)服务消费者进行服务消费:
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ctx = null;
try {
ctx = new ClassPathXmlApplicationContext("spring-dubbo-consumer.xml");
ctx.start();
SayHello sayHello = (SayHello) ctx.getBean("sayHello");
System.out.println(sayHello.sayHello("你好,我是消费者Jack,我将进行第一次消费!"));
System.in.read();
}catch (Exception ex){
System.out.println(ex.getCause().getMessage());
}finally {
if(ctx != null) {
ctx.close();
}
}
}
注:由于DUBBO服务使用依赖于Spring的容器框架,DUBBO的服务注册与消费都必须建立在容器启动以后,System.in.read();是为了保证容器处于启动状态,这样能够保证和注册中心进行会话。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论