1.需求
构建Dubbo应用
2.介绍
1.Dubbo是Alibaba开源的分布式服务框架,RPC框架
2.Dubbo采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。
3.举个栗子
pom文件
<properties> <dubbo.version>2.6.1</dubbo.version> </properties>
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> </dependency>
dubbo-api模块,提供接口HelloService
package com.dubbo; public interface HelloService { String sayHello(String name); }
dubbo-provider模块,提供服务提供方
实现了HelloService接口,提供服务的实现
package com.dubbo; public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello World "+name; } }
配置文件 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-producer" /> <!-- 使用zookeeperz作为注册中心暴露服务地址 --> <!--<dubbo:registry address="zookeeper://127.0.0.1:2181" />--> <dubbo:registry address="multicast://224.5.6.7:1234?unicast=false" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.dubbo.HelloService" ref="helloService" /> <!-- 和本地bean一样实现服务 --> <bean id="helloService" class="com.dubbo.HelloServiceImpl" /> </beans>
服务启动类
package com.dubbo; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class Provider { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"provider.xml"}); context.start(); System.out.println("Dubbo provider start..."); try { System.in.read(); // 按任意键退出 } catch (IOException e) { e.printStackTrace(); } } }
dubbo-consumer模块,提供服务的消费方
配置文件consumer.xml
扫描二维码关注公众号,回复:
180033 查看本文章
<?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-consumer" /> <!-- 使用zookeeper注册中心暴露发现服务地址 --> <!--<dubbo:registry address="zookeeper://127.0.0.1:2181" />--> <dubbo:registry address="multicast://224.5.6.7:1234?unicast=false" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference interface="com.dubbo.HelloService" id="helloService" /> </beans>
消费服务
package com.dubbo; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Consumer { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"consumer.xml"}); context.start(); // 获取远程服务代理 HelloService demoService = (HelloService) context.getBean("helloService"); //执行远程方法 String hello = demoService.sayHello("yang"); System.out.println(hello); } }