2-15 3,461 views
定义服务接口及其实现
服务接口TestService:
1 2 3 4 5 6 7 |
package com.magicwt.service; public interface TestService { public String greet(String name); } |
服务实现TestServiceImpl:
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.magicwt.service.impl; import com.magicwt.service.TestService; public class TestServiceImpl implements TestService { @Override public String greet(String name) { return "hi, " + name; } } |
服务提供者
服务提供者TestProvider:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.magicwt; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestProvider { public static void main(String[] args) throws Exception { // 加载Spring上下文环境 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("provider.xml"); // 保持进程,等待输入 System.in.read(); } } |
服务提供者Spring上下文配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?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-3.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 配置应用名称,用于表示服务提供者和服务消费者之间的依赖关系 --> <dubbo:application name="test-provider" /> <!-- 配置Zookeeper注册中心 --> <dubbo:registry address="zookeeper://xxx.xxx.xxx.xxx:2181" /> <!-- 配置服务通信协议,采用dubbo协议 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 配置本地服务实例 --> <bean id="testService" class="com.magicwt.service.impl.TestServiceImpl"/> <!-- 通过Dubbo将本地服务暴露为远程服务 --> <dubbo:service interface="com.magicwt.service.TestService" ref="testService" /> </beans> |
服务消费者
服务消费者TestConsumer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.magicwt; import com.magicwt.service.TestService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestConsumer { public static void main(String[] args) throws Exception { // 加载Spring上下文环境 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml"); // 从Spring上下文环境获取服务实例 TestService testService = (TestService) applicationContext.getBean("testService"); int count = 0; while(true) { // 每隔2秒调用一次服务方法 System.out.println(testService.greet("admin" + ++count)); Thread.sleep(2000); } } } |
服务消费者Spring上下文配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?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-3.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 配置应用名称,用于表示服务提供者和服务消费者之间的依赖关系 --> <dubbo:application name="test-consumer" /> <!-- 配置Zookeeper注册中心 --> <dubbo:registry address="zookeeper://xxx.xxx.xxx.xxx:2181" /> <!-- 引用通过Dubbo暴露的远程服务 --> <dubbo:reference id="testService" interface="com.magicwt.service.TestService" /> </beans> |
运行
分别启动服务提供者和服务消费者,服务消费者输出如下:
从监控中心可以看出,应用“test-provider”是服务提供者,应用“test-consumer”是服务消费者,因为“test-consumer”调用“test-provider”提供的服务,所以“test-consumer”依赖于“test-provider”。
版权属于: 我爱我家
原文地址: http://magicwt.com/2016/02/15/%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e7%9a%84dubbo%e7%a4%ba%e4%be%8b/
转载时必须以链接形式注明原始出处及本声明。