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

5 篇文章 0 订阅
3 篇文章 0 订阅

新建一个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控制台看到相关信息

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值