dubbo源码 学习笔记(六)

来源:互联网 发布:网络教育专科学校 编辑:程序博客网 时间:2024/05/18 03:52

使用注解来进行暴露服务和引用服务


生产者

public class ProviderAnnotation {public static void main(String[] args) throws IOException {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "META-INF/spring/dubbo-demo-provider.xml" });context.start();System.in.read(); // 按任意键退出}}

<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"       xmlns:context="http://www.springframework.org/schema/context"       xmlns="http://www.springframework.org/schema/beans"       xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd       http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context-2.5.xsd   http://code.alibabatech.com/schema/dubbo   http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><!-- spring 注解扫描 --><context:component-scan base-package="com.wy.demo.dubbo."/><!-- dubbo 注解扫描 --><dubbo:annotation package="com.wy.demo.dubbo.service" />       <!-- 提供方应用信息,用于计算依赖关系 -->    <dubbo:application name="demo-provider"/>    <!-- 使用multicast广播注册中心暴露服务地址 -->    <dubbo:registry address="multicast://224.5.6.7:1234"/>    <!-- 用dubbo协议在20880端口暴露服务 -->    <dubbo:protocol name="dubbo" port="20880"/></beans>

@Service()  //dubbo的service注解public class HelloServiceImpl implements HelloService {int i = 0;@Overridepublic int say(User hello) {System.out.println(hello.getName());return i++;}}

消费者

public class CousumerAnnotation {@ReferenceHelloService helloService;public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "META-INF/spring/dubbo-demo-consumer.xml" });context.start();CousumerAnnotation hh = (CousumerAnnotation) context.getBean("haha"); // 获取远程服务代理System.out.println(hh.helloService); // 显示调用结果}}

<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"       xmlns:context="http://www.springframework.org/schema/context"       xmlns="http://www.springframework.org/schema/beans"       xsi:schemaLocation="       http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd       http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context-2.5.xsd   http://code.alibabatech.com/schema/dubbo   http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><dubbo:annotation package="com.wy.demo.dubbo.consumer." />     <!-- 提供方应用信息,用于计算依赖关系 -->    <dubbo:application name="demo-cousumer"/>    <!-- 使用multicast广播注册中心暴露服务地址 -->    <dubbo:registry address="multicast://224.5.6.7:1234"/>     <!--  通过spring 配置bean --><bean id="haha" class="com.wy.demo.dubbo.consumer.CousumerAnnotation"></bean></beans>