Dubbo 的简单实用
来源:互联网 发布:java mysql信息管理 编辑:程序博客网 时间:2024/06/06 02:04
- 使用Dubbo 的安装一个注册中心来注册服务,一般选用的是 zookeeper 。zookeeper的安装并不是很复杂。
- Dubbo采用全spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。可以直接像使用本地已经实现的接口一样,通过Spring Bean 容器里面直接获取。这也是Dubbo最大的好处之一。
3.结构工程
这是一个简单的工程结构:
consumer 消费者 ,消费服务,是使用服务的对象。一个消费者可以消费多个服务。
provider 服务提供者 ,提供实现了的接口。一个服务者也可以像多个消费者提供服务。
interface 公共接口(消费者和服务提供者共用)
(一)Interface
普通 Java 模块,没有引用其他相关的包
定义了一个接口,给 consumer ,provider使用 ,没有实现。
实现在 provider 里面。使用在 consumer 里面。
public interface IUserService { boolean login(String username, String password);}
(二) Provider
主要功能是,实现接口IUserService ,并注册到注册中心。
pom.xml
<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"> <parent> <artifactId>DubboDemo</artifactId> <groupId>com.linving.DubboDemo</groupId> <version>1.0-SNAPSHOT</version> </parent> <version>1.0-SNAPSHOT</version> <modelVersion>4.0.0</modelVersion> <groupId>com.linving.DubboDemo</groupId> <artifactId>provider</artifactId> <packaging>jar</packaging> <name>provider</name> <url>http://maven.apache.org</url> <!-- **************************** Properties 配置 **************************** --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.version>3.8.1</junit.version> <springframework.version>4.1.6.RELEASE</springframework.version> <commonsLogging.version>1.2</commonsLogging.version> </properties> <!-- **************************** /Properties 配置 **************************** --> <dependencies> <!-- **************************** Spring 依赖 **************************** --> <!-- 添加Spring-core包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${springframework.version}</version> </dependency> <!-- 添加spring-tx包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${springframework.version}</version> </dependency> <!-- Spring ORM 相关--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${springframework.version}</version> </dependency> <!-- 添加spring-jdbc包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${springframework.version}</version> </dependency> <!--添加spring-web包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${springframework.version}</version> </dependency> <!-- 添加spring-context包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>${commonsLogging.version}</version> </dependency> <!--添加aspectjweaver包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.5</version> </dependency> <!-- **************************** /Spring 依赖 **************************** --> <!-- **************************** Dubbo 依赖 **************************** --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.3.3</version> <exclusions> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> <exclusion> <groupId>com.sun.jdmk</groupId> <artifactId>jmxtools</artifactId> </exclusion> <exclusion> <groupId>javax.jms</groupId> <artifactId>jms</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!-- **************************** /Dubbo 依赖 **************************** --> <!-- **************************** interface 依赖 **************************** --> <dependency> <groupId>com.linving.DubboDemo</groupId> <artifactId>interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- **************************** /interface 依赖 **************************** --> </dependencies></project>
类:
UserServiceImpl 是接口的实现。
SpringBeanFactory 是 spring bean加载类,用来启动spring容器。
provider 一般不用Tomcat或者其他容器中启动。有个启动类即可。
在看看 服务是这么注册到 注册中心的。
applicationContext.xml 平常spring 加载的一些配置,没有什么特殊的。
spring-provider.xml 负责 注册到注册中心并注册服务。
<?xml version="1.0" encoding="UTF-8"?><!-- 添加 DUBBO SCHEMA --><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="dubbodemo-provider"/> <!-- 连接到哪个本地注册中心 --> <dubbo:registry id="dubbodemo" address="zookeeper://localhost:2181"/> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="28080"/> <!-- 声明需要暴露的服务接口 --> <dubbo:service timeout="3000" interface="com.linving.service.IUserService" ref="userService"/> <!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 --> <!--<dubbo:annotation package="com.linving.service.impl" />--></beans>
就这样就可以 发布一个服务了。 记得打开注册中心。
Consumer
服务消费者
pom.xml
<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"> <parent> <artifactId>DubboDemo</artifactId> <groupId>com.linving.DubboDemo</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.linving.DubboDemo</groupId> <artifactId>consumer</artifactId> <packaging>jar</packaging> <name>consumer</name> <url>http://maven.apache.org</url> <!-- **************************** Properties 配置 **************************** --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.version>3.8.1</junit.version> <springframework.version>4.1.6.RELEASE</springframework.version> <commonsLogging.version>1.2</commonsLogging.version> </properties> <!-- **************************** /Properties 配置 **************************** --> <dependencies> <!-- **************************** Spring 依赖 **************************** --> <!-- 添加Spring-core包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${springframework.version}</version> </dependency> <!-- 添加spring-tx包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${springframework.version}</version> </dependency> <!-- Spring ORM 相关--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${springframework.version}</version> </dependency> <!-- 添加spring-jdbc包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${springframework.version}</version> </dependency> <!--添加spring-web包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${springframework.version}</version> </dependency> <!-- 添加spring-context包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>${commonsLogging.version}</version> </dependency> <!--添加aspectjweaver包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.5</version> </dependency> <!-- **************************** /Spring 依赖 **************************** --> <!-- **************************** Dubbo 依赖 **************************** --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.3.3</version> <exclusions> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> <exclusion> <groupId>com.sun.jdmk</groupId> <artifactId>jmxtools</artifactId> </exclusion> <exclusion> <groupId>javax.jms</groupId> <artifactId>jms</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!-- **************************** /Dubbo 依赖 **************************** --> <!-- **************************** interface 依赖 **************************** --> <dependency> <groupId>com.linving.DubboDemo</groupId> <artifactId>interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- **************************** /interface 依赖 **************************** --> </dependencies></project>
Consumer 服消费者:实际运用一般是一个WebApp ,在容器中启动。这里没有写成一个web 项目。
看看是怎么获取注册中心的信息的:
spring-consumer.xml
<?xml version="1.0" encoding="UTF-8"?><!-- 添加 DUBBO SCHEMA --><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="dubbodemo-consumer"/> <!-- 连接到哪个注册中心(连接到本机的2181端口zookeeper) --> <dubbo:registry address="zookeeper://localhost:2181"/> <!-- 消费方用什么协议获取服务(用dubbo协议在20880端口暴露服务) --> <dubbo:protocol port="28080"/> <!-- 调用哪些服务 --> <dubbo:reference id="userService" interface="com.linving.service.IUserService"/> <!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 --> <!--<dubbo:annotation package="com.linving" />--></beans>
UserServiceConsumer.java
@Componentpublic class UserServiceConsumer { private static Logger logger = LoggerFactory.getLogger(UserServiceConsumer.class); public static void main(String args[]) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //调用远程服务 IUserService userService = (IUserService) ctx.getBean("userService"); logger.info("执行结果:" + userService.login("hello", "hello")); }}可像调用本地 实现一样调用远程服务了。项目代码地址: https://git.oschina.net/linving/DubboDemo.git
1 0
- Dubbo 的简单实用
- dubbo的简单体验
- 简单的使用dubbo
- dubbo的简单使用
- Dubbo 简单的入门【Maven+SpringMVC+Dubbo 】
- Dubbo最简单的实例
- 体验dubbo的简单方法
- 简单实用的bugfree
- 简单实用的 .vimrc
- selenium的简单实用
- 简单实用的makefile
- ArrayList的简单实用
- 简单实用的分享
- wxTimer的简单实用
- GPUImage的简单实用
- UISearchBar的简单实用
- fastJson的简单实用
- SuperToast的简单实用
- HDU 2845 Beans
- lua使用优化建议
- Docker学习笔记七:Docker网络
- JavaSe总结-22- 输入输出流(3)
- 信息论基础知识
- Dubbo 的简单实用
- mysql查询优化
- 本地Navicat连不上Linux虚拟机MySQL数据库问题
- MyEclipse开启Mybatis-config.xml配置文件智能提示
- JavaSe总结-23- 多线程(1)
- XML简介
- 96. Unique Binary Search Trees -Medium
- CSS样式隐藏图片作为背景图片上方的文字内容
- 在OD中手工修复IAT重定向