Dubbo之消费者
在写 dubbbo调用时候
<dubbo:reference 不能有空格!
项目结构:
pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.toov5</groupId> <artifactId>toov5-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>toov5-order-dubbo-service</artifactId> <dependencies> <dependency> <groupId>com.toov5</groupId> <artifactId>toov5-member-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> <version>3.2.5.Final</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </project>
业务:
package com.toov5.dubbo.consumer; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.toov5.api.member.service.MemberService; public class OderToMemberTest { public static void main(String[] args) { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("dubbo-consumer.xml"); MemberService memberService = applicationContext.getBean(MemberService.class); String resultUser = memberService.getUser(12L); System.out.println("调用服务后的返回结果"+resultUser); } }
dubbo配置
<?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-admin 或 dubbo-monitor 会显示这个名字,方便辨识 --> <dubbo:application name="demotest-consumer" /> <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper --> <dubbo:registry address="zookeeper://192.168.91.5:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!--使用 dubbo 协议实现定义好的 MemberService 接口 --> <dubbo:reference id="memberService" interface="com.toov5.api.member.service.MemberService"/> </beans>
启动:
只引入了接口 没有实现。 实现都是通过底层转换成rpc dubbo协议进行调用的