您的位置:首页 > 其它

阿里dubbo框架使用系列:服务提供者和消费者的创建和使用

2016-05-10 09:15 591 查看
新建一个maven工程



创建一个服务接口

package com.pcx.dubbo_facade;

public interface DemoService {
String sayHello(String name);
}


运行 clean install打包dubbo-facade



接下来创建 dubbo-provider 工程



在pom.xml里面引用刚才的服务接口的jar包

<span style="white-space:pre">		</span><dependency>
<groupId>com.pcx</groupId>
<span style="white-space:pre">	</span><artifactId>dubbo-facade</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>


编写服务实现类

package com.pcx.dubbo_prodiver;

import org.springframework.stereotype.Service;

import com.pcx.dubbo_facade.DemoService;

@Service("demoService")
public class DemoServiceImpl implements DemoService {

public String sayHello(String name) {
return "Hello " + name;
}

}


在resource目录下编写两个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="dubbo-demo-provider" />

<!--zk注册中心的地址-->
<dubbo:registry protocol="zookeeper" address="192.168.1.10:2181" />

<!-- 用dubbo协议在21000端口暴露服务 -->
<dubbo:protocol name="dubbo" port="21000" />

<!-- 配置服务接口 -->
<dubbo:service interface="com.pcx.dubbo_facade.DemoService" ref="demoService" />

</beans>


spring-context.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd" default-autowire="byName" default-lazy-init="false">

<!-- 采用注释的方式配置bean -->
<context:annotation-config />

<!-- 配置要扫描的包的路径 -->
<context:component-scan base-package="com.pcx" />

<import resource="dubbo-provider.xml" />
</beans>
在src/test/java路径下编写测试类启动dubbo服务

package com.pcx.dubbo_prodiver;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DubboProvider {
private static final Log log = LogFactory.getLog(DubboProvider.class);

public static void main(String[] args) {
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
context.start();
} catch (Exception e) {
log.error("== DubboProvider context start error:",e);
}
synchronized (DubboProvider.class) {
while (true) {
try {
DubboProvider.class.wait();
} catch (InterruptedException e) {
log.error("== synchronized error:",e);
}
}
}
}
}


运行这个测试类,我们可以在dubbo控制台看到我们暴露的服务



创建一个新工程名为dobbo-consumer



在pom.xml下依赖我们的服务接口的jar包

<span style="white-space:pre"> </span><dependency>
<groupId>com.pcx</groupId>
<artifactId>dubbo-facade</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

在src/main/resource/目录下新增两个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="dubbo-demo-consumer" />

<!-- 填写zk注册中心的地址 -->
<dubbo:registry protocol="zookeeper" address="192.168.1.10:2181" />

<!-- 引用服务提供接口的路径 -->
<dubbo:reference interface="com.pcx.dubbo_facade.DemoService" id="demoService" check="false" />

</beans>


spring-context.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd" >

<import resource="dubbo-consumer.xml" />

</beans>


编写调用服务的测试类在src/test/java

package com.pcx.dubbo_consumer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.pcx.dubbo_facade.DemoService;

public class Consumer {
private static final Log log = LogFactory.getLog(Consumer.class);

public static void main(String[] args) throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
context.start();
DemoService demoService = (DemoService)context.getBean("demoService");
String hello = demoService.sayHello("world");
log.info("打印"+hello);

Thread.sleep(100000);

}

}
运行测试类



调用成功

此时可以在dubbo控制台看到相关信息

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: