dubbo实现示例
创建MAVEN项目
项目结构:
在项目pom.xml中添加依赖
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
<!-- 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>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
主要分三大模块:
dubbo-api : 存放公共接口;
dubbo-consumer : 调用远程服务;
dubbo-provider : 提供远程服务。
第一步创建dubbo-api的MAVEN项目(有独立的pom.xml,用来打包供提供者消费者使用)。
在项目中定义服务接口:该接口需单独打包,在服务提供方和消费方共享。
package com.alibaba.dubbo.demo; import java.util.List; public interface DemoService {
List<String> getPermissions(Long id);
}
第二步创建dubbo-provider的MAVEN项目(有独立的pom.xml,用来打包供消费者使用)。
实现公共接口,此实现对消费者隐藏:
在pom.xml中添加依赖
<dependencies>
<dependency>
<groupId>DubboDemo</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
创建配置文件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-admin 或 dubbo-monitor 会显示这个名字,方便辨识
owner 是负责人 在dubbo-admin 中可以看的 organization 组织
-->
<dubbo:application name="demotest-provider" owner="programmer" organization="dubbox"/>
<!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!--使用 dubbo 协议实现定义好的 api.PermissionService 接口-->
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" protocol="dubbo" />
<!--具体实现该接口的 bean-->
<bean id="demoService" class="com.alibaba.dubbo.demo.impl.DemoServiceImpl"/>
</beans>
实现接口
package com.alibaba.dubbo.demo.impl; import com.alibaba.dubbo.demo.DemoService; import java.util.ArrayList;
import java.util.List; public class DemoServiceImpl implements DemoService {
public List<String> getPermissions(Long id) {
List<String> demo = new ArrayList<String>();
demo.add(String.format("Permission_%d", id - 1));
demo.add(String.format("Permission_%d", id));
demo.add(String.format("Permission_%d", id + 1));
return demo;
}
}
写一个main方法启动服务
package com.alibaba.dubbo.demo.impl; 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("provider.xml");
System.out.println(context.getDisplayName() + ": here");
context.start();
System.out.println("服务已经启动...");
System.in.read();
}
}
第三步创建dubbo-consumer的MAVEN项目(可以有多个consumer,但是需要配置好)。
调用所需要的远程服务:
<artifactId>dubbo-consumer</artifactId>
<dependencies>
<dependency>
<groupId>DubboDemo</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
创建配置文件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="demotest-consumer" owner="programmer" organization="dubbox"/>
<!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--使用 dubbo 协议调用定义好的 api.PermissionService 接口-->
<dubbo:reference id="permissionService" interface="com.alibaba.dubbo.demo.DemoService"/>
</beans>
启动Consumer,调用远程服务:
package com.alibaba.dubbo.consumer; import com.alibaba.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Consumer {
public static void main(String[] args) {
//测试常规服务
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("consumer.xml");
context.start();
System.out.println("consumer start");
DemoService demoService = context.getBean(DemoService.class);
System.out.println("consumer");
System.out.println(demoService.getPermissions(1L));
}
}
使用dubbo Main方法启动服务
Pom.xml中配置
<build>
<finalName>dubbo-provider</finalName>
<resources>
<resource>
<targetPath>${project.build.directory}/classes</targetPath>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<!-- 结合com.alibaba.dubbo.container.Main -->
<resource>
<targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>spring-mybatis.xml</include>
</includes>
</resource>
</resources> <pluginManagement>
<plugins>
<!-- 解决Maven插件在Eclipse内执行了一系列的生命周期引起冲突 -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<versionRange>[2.0,)</versionRange>
<goals>
<goal>copy-dependencies</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>target/classes/</classesDirectory>
<archive>
<manifest>
<mainClass>com.alibaba.dubbo.container.Main</mainClass>
<!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<type>jar</type>
<includeTypes>jar</includeTypes>
<!--<useUniqueVersions>false</useUniqueVersions>-->
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
2使用maven 打成jar包
或者使用命令
使用java -jar 启动服务
dubbo实现示例的更多相关文章
- dubbo简单示例
dubbo简单示例 2019-09-06 1 Zookeeper注册中心的搭建(windows单机) 下载zookeeper压缩包并解压到 D:\zookeeper\apache-zookeeper- ...
- SpringMVC与Zookeeper、Dubbo使用示例
Dubbo整合Zookeeper和spring示例程序 1.Dubbo架构 本篇文章基于dubbox,使用dubbo应该也可以正常运行. 我认为想讲清楚一个任何一个技术框架,首先熟悉架构是非常有必要的 ...
- dubbo入门示例
前提准备: 在本次实验之前,需要准备一下几个包: Spring中的aop.beans.context.core.expression以及struts中的commons-logging.javassis ...
- dubbo+zookeeper示例记录
提示:要直接看搭建例子的可以跳到 三 一.项目架构的发展 传统的mvc架构项目将整个系统功能实现全部写在一个项目中,部署在一个机器上,随着用户量的增涨,单个项目服务器无法承受暴增的用户请求时需要增加服 ...
- Dubbo+Zookeeper实现简单的远程方法调用示例
1. Dubbo介绍 示例代码:Github 1.1 RPC Remote Procedure Call:远程过程调用 1.2 Dubbo架构 Subscribe 订阅:签署:赞成 Monitor 监 ...
- dubbo模块组织方式
dubbo源码版本:2.5.4 阿里通过maven将dubbo的36个模块组织成了一个项目,各个模块结构如下: -------------------------------------------- ...
- dubbo + zookeeper 环境搭建
一.zookeeper windows部署 1.下载安装 到官网下载解压版后解压至F:\server\zookeeper-3.4.8,剩下为文件配置工作: 2.本地伪集群 1) 在F:\server\ ...
- dubbo框架初步学习
dubbo简介 Dubbo是一个分布式服务框架,以及SOA治理方案.其功能主要包括:高性能NIO通讯及多协议集成,服务动态寻址与路由,软负载均衡与容错,依赖分析与降级等. 官网:http://dubb ...
- Dubbo 源码分析 - SPI 机制
1.简介 SPI 全称为 Service Provider Interface,是 Java 提供的一种服务发现机制.SPI 的本质是将接口实现类的全限定名配置在文件中,并由服务加载器读取配置文件,加 ...
随机推荐
- json初接触
<html lang="en"> <head> <meta charset="UTF-8"> <meta name=& ...
- python中的异常处理常用方法
异常处理 什么是异常? 异常就是与正常情况不同,程序在执行过程中出现错误,导致无法执行完毕.异常其实就是代码执行过程中出错. 常见的一些异常 AttributeError 试图访问一个对象没有的属性, ...
- linux目录与文件权限的意义
现在我们已经知道了Linux系统内文件的三种身份(所有者,用户者,与其他人),知道每种身份都有三种属性(r,w,x),已经能够使用chown,chgrp,chmod去修改这些权限和属性,那么这些文件权 ...
- 初学JSON和AJAX心得透过解惑去学习
虽然复制代码再改参数,也能正常运作.但是看懂里面语法,就可以客制成适合你自己程序.例如录制Excel巨集,会有一些赘句,这时候整合就是很重要工作. [大纲] 时间分配 AJAX Markdown教学及 ...
- 1、python环境安装及软件介绍
软件: python3.0 下载地址:https://www.python.org/downloads/windows/ pycharm 下载地址: https://www.jetbrains.com ...
- Spring MVC 自动为对象注入枚举类型
原文地址:http://1358440610-qq-com.iteye.com/blog/2079048 如果一个对象里面有枚举类型的话,则Spring MVC是不能够直接进行注入的,因为它只实现了一 ...
- 转载:轻量级Config文件AppSettings节点编辑帮助类
using System.Configuration; using System.Windows.Forms; namespace Allyn.Common { public class XmlHep ...
- redis5 集群迁移方案
Redis5 集群迁移方案 一.KEY优化 1.按原来要求进行优化与大KEY分拆. 二.现Redis 集群缩容(对业务无影响) 主节点按要求合并至3个主节点. 业务配置为3主4从 删除没有槽的主节点与 ...
- [leetcode]90. Subsets II数组子集(有重)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- js函数内未声明变量
<script> function test(){ testd = "Hello"; } test(); alert(testd); </script> 当 ...