一、dubbo服务化架构包含的内容

  对于传统工程而言,分层的依据是按照包来区分。由于在相同的工程中,所以服务的提供和调用可以方便的实现。

但是对于分布式架构而言,服务的提供者负责服务具体的实现和接口规范,服务的消费者只关心接口规范即可。但是

无论是服务的提供者还是服务的消费者都会涉及到诸如公共工具类、接口、DO、VO、等公共代码,因此一个简单的

dubbo服务架构模式如下:

服务提供者:提供服务接口的实现,发布服务地址,提供服务

服务消费者:获取服务地址,使用服务接口调用服务,处理服务调用结果

公共项目: 包含公共配置:DO(和数据库同步,用于持久化对象)、VO(传输数据)、工具包、接口等

依赖关系:依赖项目就像依赖jar包一样

二、创建公共项目工程

  创建公共项目工程,普通的maven项目,提供utils、DO、接口等代码

三、服务提供者实现

  普通的Maven工程(依赖Dubbo),提供服务实现、服务启动功能。

  ①:创建项目并导入dubbo.jar

  pom.xml

  <dependencies>

<dependency>

      <groupId>cn.itsource.dubbo.core</groupId>

      <artifactId>dubbo-demo-core</artifactId>

      <version>0.0.1-SNAPSHOT</version>

    </dependency>

<!--以上的导入就是公共部分。-->

<dependency>

      <groupId>com.alibaba</groupId>

      <artifactId>dubbo</artifactId>

      <version>2.8.4a</version>

    </dependency>

    <dependency>

      <groupId>com.101tec</groupId>

      <artifactId>zkclient</artifactId>

      <version>0.9</version>

    </dependency>

    <dependency>

      <groupId>org.apache.zookeeper</groupId>

      <artifactId>zookeeper</artifactId>

      <version>3.4.9</version>

      <exclusions>

        <exclusion>

          <groupId>log4j</groupId>

          <artifactId>log4j</artifactId>

        </exclusion>

      </exclusions>

    </dependency>

    <dependency>

      <groupId>log4j</groupId>

      <artifactId>log4j</artifactId>

      <version>1.2.16</version>

    </dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

</dependencies>

  ②:实现本地服务类,不需要标记远程服务

  在调用过程中存在数据传输,因此需要转成二进制,因此服务对象需要实现序列化,也就是实现Serializable接口

  ③:以spring配置文件的方式来注册服务

  dubbo-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-2.5.xsd

    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<dubbo:application name="dubbo-test-provider" owner="sampson"></dubbo:application>

<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>

<!-- 局域网广播注册中心 -->

<dubbo:registry address="multicast://239.5.6.7:1234" />

<!-- 配置式发布 -->

<bean id="userService" class="cn.itsource.dubbo.provider.service.UserServiceImpl"></bean>

<dubbo:service interface="cn.itsource.dubbo.core.service.IUserService" ref="userService">              </dubbo:service>

  <!-- 注解式发布 -->

<!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 -->

<dubbo:annotation package="cn.itsource.dubbo.provider.service" />

  </beans>

  ④:启动spring,并且加载配置文件,才能发布服务

   启动服务监听

  String configLocation = "classpath*:/dubbo-provider.xml";

  ApplicationContext context = new ClassPathXmlApplicationContext(configLocation);

  System.out.println("dubbo-server服务正在监听,按任意键退出");

  System.in.read();//作用是从键盘读出一个字符,返回unicode编码(数字)此处用来终止程序结束,因为不输出就不会结束

四、服务消费者实现

  创建服务消费者项目:普通的Maven工程(依赖Dubbo),完成服务调用功能。

  ①:创建项目并导入dubbo.jar

   pom.xml

  <dependencies>

<dependency>

<groupId>cn.itsource.dubbo.core</groupId>

<artifactId>dubbo-demo-core</artifactId>

<version>0.0.1-SNAPSHOT</version>

</dependency>

<dependency>

      <groupId>com.alibaba</groupId>

      <artifactId>dubbo</artifactId>

      <version>2.8.4a</version>

    </dependency>

    <dependency>

      <groupId>com.101tec</groupId>

      <artifactId>zkclient</artifactId>

      <version>0.9</version>

    </dependency>

    <dependency>

      <groupId>org.apache.zookeeper</groupId>

      <artifactId>zookeeper</artifactId>

      <version>3.4.9</version>

      <exclusions>

        <exclusion>

          <groupId>log4j</groupId>

          <artifactId>log4j</artifactId>

        </exclusion>

      </exclusions>

    </dependency>

    <dependency>

      <groupId>log4j</groupId>

      <artifactId>log4j</artifactId>

      <version>1.2.16</version>

    </dependency>

    <dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>4.1.2.RELEASE</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

</dependencies>

  ②:通过配置文件对接口的配置获取本地代理对象的代码

  dubbo-consumer.xml

  <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

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-2.5.xsd

    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

    http://www.springframework.org/schema/context 

    http://www.springframework.org/schema/context/spring-context.xsd">

<dubbo:application name="dubbo-test-consumer"></dubbo:application>

<!-- 局域网广播注册中心 -->

<dubbo:registry address="multicast://239.5.6.7:1234" />

<!-- 配置式调用服务 -->

<!-- <dubbo:reference id="helloService" interface="cn.itsource.dubbo.core.service.IHelloService">      </dubbo:reference> -->

<!-- 注解式调用服务 -->

<!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 -->

<dubbo:annotation package="cn.itsource.dubbo.consumer" />

  </beans>

  ③:给接口产生了本地代理对象,并且把它纳入spring管理

  ④:直接注入代理对象,调用方法完成远程调用

    JUnit4调用dubbo服务测试类

    import org.junit.Test;

    import org.junit.runner.RunWith;

    import org.springframework.test.context.ContextConfiguration;

    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

    import com.alibaba.dubbo.config.annotation.Reference;

    importcn.itsource.dubbo.core.service.IHelloService;

    @RunWith(SpringJUnit4ClassRunner.class)

    @ContextConfiguration({"classpath*:/dubbo-consumer.xml"})

    publicclass DubboServiceTest {

    @Reference

    private IHelloService helloService;

    @Test

    publicvoid testHello(){

    String sayHi = helloService.sayHi("老宋");

    System.out.println(sayHi);

}

}

五、直连模式

  当消费者和服务者在同一台电脑或者服务器,可以采取直连模式。一般用于本地测试,部署在同一个

容器将失去微服务架构的优势。

  与之相对的check(检查模式)。例如:A服务启动会依赖于B服务,如果B服务没有启动的情况下,去

启动A服务。如果check为false启动时不会报错,如果check为true,启动就会报错。

  check:启动检查依赖关系

  服务提供者:

  修改注册中心为:N/A模式(不注册)

<dubbo:registry address="N/A" check="false"/>

check,A服务的启动会依赖于B服务。如果B服务没有启动的情况下,去启动A服务。如果check为false,启动时不会报错,调用时才报错。如果check为true,启动时就报错。调试时用false,上线的时为true。

服务消费者:

  修改注册中心为:N/A模式(不注册)

<dubbo:registry address="N/A" check="false"/>

配置本地调用地址映射:

然后在${user.home}/dubbo-resolve.properties文件中配置对应服务调用的本地地址

${user.home} 一般代表:C:\Users\{你当前登录名}

dubbo-resolve.properties示例

cn.itsource.dubbo.core.service.IUserService=dubbo://localhost:20880

cn.itsource.dubbo.core.service.IHelloService=dubbo://localhost:20880

.....其它服务配置

六、dubbo服务打包

  ①.   Dubbo服务提供者的运行方式有三种

通过一个配置文件初始化一个Spring容器。dubbo就会解析配置文件完成对应服务注册。换句话说就是要启动一个Spring。

1、使用Servlet容器(不用)-ContextLoadListener

利用Tomcat、Jetty等WEB容器启动Dubbo服务。

缺点:增加管理配置的复杂性,不必要地使用http端口,浪费内存资源

2、Java的Main方法/Test方法中(不建议,本地调试可以用)

基于Spring框架,写一个Java类并提供Main方法启动。 new ApplicationContext

缺点:无法使用Dubbo的一些高级特性,服务的管理需要自己额外提供实现

3、Dubbo框架Main方法(项目上线使用)

Dubbo框架本身提供了服务运行支持方法,基于com.alibaba.dubbo.container.Main

简单高效地运行服务,该类其实就是一个带Main函数的类,该main函数会根据特定路径的配置文件创建一个Spring的容器。

很好地支持Dubbo服务的发布、关停(ShutdownHook)

  ②:Maven编译打包

  <groupId>cn.itsource.service</groupId>

<artifactId>service-user</artifactId>

<version>${service-user.version}</version>

<packaging>jar</packaging>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

<!-- 打包jar详细配置 -->

<build>

<!-- jar包名字 -->

<finalName>service-user</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

官方文档:dubbo会自动在classes/META-INF/spring下去加载spring的配置文件

因此打包时需要将spring配置文件复制到该目录

-->

<resource>

<targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>

<directory>src/main/resources</directory>

<filtering>true</filtering>

<includes>

<include>spring-dubbo-provider.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>

</plugins>

</pluginManagement>

<plugins>

<!-- 打包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>

</build>

<dependencies>

</dependencies>

  ③:dubbo服务jar包运行

  Cmd窗口:

  1、定位到jar包所在目录

  2、输入命令并回车执行:java -jar xxxxx.jar &

  dubbo服务打包是部署dubbo服务的必要方式之一。当执行java -jar时,会自动寻找程序入口(main函数)并运行。

  &保持一致运行。

dubbo本地服务化实现(dubbo三)的更多相关文章

  1. 本地Windows环境Dubbo搭建测试

    Dubbo介绍 Dubbo[]是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案. 其核心部分包含: 远程通讯: 提供对多种基于长连接的NIO框架抽象封装, ...

  2. Dubbo 04 服务化最佳实现流程

    Dubbo 04 服务化最佳实践 分包 建议将服务接口.服务模型.服务异常等均放在 API 包中,因为服务模型和异常也是 API 的一部分,这样做也符合分包原则:重用发布等价原则(REP),共同重用原 ...

  3. DUBBO本地搭建及小案例

    DUBBO的介绍部分我这里就不介绍了,大家可参考官方文档. DUBBO的注册中心安装 DUBBO的注册中心支持好几种,公司用到zookeeper注册中心,所以我这边只说明zookeeper注册中心如何 ...

  4. 【dubbo】服务提供者运行的三种方式

    [dubbo]服务提供者运行的三种方式 学习了:https://blog.csdn.net/yxwb1253587469/article/details/78712451 1,使用容器: 2,使用自建 ...

  5. 【2020-03-21】Dubbo本地环境搭建-实现服务注册和消费

    前言 本周主题:加班工作.本周内忙于CRUD不能自拔,基本每天都是九点半下班,下周上线,明天还要加班推进进度.今天是休息日,于是重拾起了dubbo,打算近期深入了解一下其使用和原理.之所以说是重拾,是 ...

  6. Dubbo本地存根是什么,Dubbo本地伪装又是什么?

    真正的大师永远怀着一颗学徒的心 哈喽!大家好,我是小奇,一位程序员界的学徒 小奇打算以轻松幽默的对话方式来分享一些技术,如果你觉得通过小奇的文章学到了东西,那就给小奇一个赞吧 前言 书接上回,昨天打了 ...

  7. DUBBO本地搭建及小案例 (转)

    DUBBO的介绍部分我这里就不介绍了,大家可参考官方文档. DUBBO的注册中心安装 DUBBO的注册中心支持好几种,公司用到zookeeper注册中心,所以我这边只说明zookeeper注册中心如何 ...

  8. dubbo本地调试直连

    服务: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://ww ...

  9. Dubbo本地调试

    dubbo 启动标志 Dubbo service server started <dubbo:reference id="transferTimingUploadHisRPCServi ...

随机推荐

  1. Chinese word segment based on character representation learning 论文笔记

    论文名和编号 摘要/引言 相关背景和工作 论文方法/模型 实验(数据集)及 分析(一些具体数据) 未来工作/不足 是否有源码 问题 原因 解决思路 优势 基于表示学习的中文分词 编号:1001-908 ...

  2. 5分钟了解TypeScript

    1.安装TypeScript 有两种方式安装TypeScript: Via npm 通过安装VS插件,更多可参见这里. 对于npm用户,可以直接使用下面的命令行安装: nmp install -g T ...

  3. Python 函数调用&定义函数&函数参数

    一.函数调用 在python中内置了很多函数,我们可以直接调用 .想要调用函数首先要知道函数的名称及包含的参数,还可以通过查看python官方的文档:https://docs.python.org/3 ...

  4. C#基础知识之面向对象以及面向对象的三大特性

    在C#基础知识之类和结构体中我详细记录了类.类成员.重载.重写.继承等知识总结.这里就记录一下对面向对象和面向对象三大特性的广义理解. 一.理解面向对象 类是面向对象编程的基本单元,面向对象思想其实就 ...

  5. 理解IO、NIO、 AIO

    转载:https://baijiahao.baidu.com/s?id=1586112410163034993&wfr=spider&for=pc nio 同步: 自己亲自出马持银行卡 ...

  6. 【Codeforces 1000F】One Occurrence

    题意:给一个序列,每次查询某个区间内一个只出现一次的数. 思路:线段树. 首先我们看只出现一次的本质是什么. 如果一个数\(x​\)在\((l,r)​\)中只出现了一次,那么它在其中第一次出现位置为\ ...

  7. Fabric动态增加组织【资料】

    Fabric在启动之前需要生成Orderer的创世区块和channel的配置区块.也就是说在Fabric网络启动之前我们就必须定好了有哪些Org,而当Fabric已经跑起来之后,想要增加Org却是很麻 ...

  8. 04 Django REST Framework 认证、权限和限制

    目前,我们的API对谁可以编辑或删除代码段没有任何限制.我们希望有更高级的行为,以确保: 代码片段始终与创建者相关联. 只有通过身份验证的用户可以创建片段. 只有代码片段的创建者可以更新或删除它. 未 ...

  9. CSL 的魔法

    链接 [https://ac.nowcoder.com/acm/contest/551/E] 分析 很显然就是a的第k大得和b的倒数第k大相乘. 那么我们只要让a的第k大和b的倒数第k大位置是相同的即 ...

  10. 妙解Servlet四大域对象

    pageContext pageContext作用域为page(页面执行期). request request是表示一个请求,只要发出一个请求就会创建一个request,它的作用域仅在当前请求中有效. ...