dubbo本地服务化实现(dubbo三)

一、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函数)并运行。

  &保持一致运行。

原文地址:https://www.cnblogs.com/zeussbook/p/10498935.html

时间: 2024-07-30 13:54:07

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

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

前言 本周主题:加班工作.本周内忙于CRUD不能自拔,基本每天都是九点半下班,下周上线,明天还要加班推进进度.今天是休息日,于是重拾起了dubbo,打算近期深入了解一下其使用和原理.之所以说是重拾,是因为去年自学过一次,但那次主要是针对源码的流程,在实战上欠缺,且对其理解未深入到架构层次,只能说是基本理解.现在的我跟去年比起来,对技术的理解上有了一些提升,经验也更丰富,故本次目标是做深入研究,且看能从中吸收多少要义. 今天先记录一下dubbo本地服务的简易搭建流程. 一.环境准备 本次搭建用zo

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

DUBBO的介绍部分我这里就不介绍了,大家可参考官方文档. DUBBO的注册中心安装 DUBBO的注册中心支持好几种,公司用到zookeeper注册中心,所以我这边只说明zookeeper注册中心如何安装. 安装zookeeper注册中心首先得下载zookeeper.大家可到zookeeper的官网http://zookeeper.apache.org/releases.html上去下载. 我下载了zookeeper-3.4.5.tar.gz版本的包.接下来把zookeeper-3.4.5.ta

dubbo源码之四——dubbo服务发布

dubbo版本:2.5.4 服务发布是服务提供方向注册中心注册服务过程,以便服务消费者从注册中心查阅并调用服务. 服务发布方在spring的配置文件中配置如下: <bean id="demoService"class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" /> 上面是在spring中配置的服务的具体实现,是spring中的一个普通的bean. <dubbo:serviceinterfac

dubbo系列二、dubbo+zookeeper+dubboadmin分布式服务框架搭建(windows平台)

一.zookeeper配置中心安装 1.下载安装包,zookeeper-3.4.6.tar.gz 2.解压安装包,修改配置文件 参考zookeeper-3.4.6/conf/zoo_sample.cfg文件,同步录下建立zoo.cfg,配置如下: # The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can tak

dubbo系列四、dubbo启动过程源码解析

一.代码准备 1.示例代码 参考dubbo系列二.dubbo+zookeeper+dubboadmin分布式服务框架搭建(windows平台) 2.简单了解下spring自定义标签 https://www.jianshu.com/p/16b72c10fca8 Spring自定义标签总共可以分为以下几个步骤 定义Bean 标签解析生成接收配置的POJO. 定义schema文件,定义自定义标签的attr属性 定义解析类parser,遇到自定义标签如何解析. 定义命名空间处理类namespaceSup

Dubbo之快速搭建dubbo应用框架demo

一.安装zookeeper 此处省略 二.安装dubbo-admin 可以去https://github.com/alibaba/dubbo/releases下载源码用Eclipse将dubbo-admin项目打成War包或网络下载War包 将dubbo-admin.war包放到Tomcat的Webapps目录下,并启动Tomcat,然后访问http://localhost/dubbo-admin/ 登录用户密码可以从WEB-INF下的dubbo.properties文件中看到, root账号默

dubbo入门学习(五)-----dubbo的高可用

zookeeper宕机与dubbo直连 现象 zookeeper注册中心宕机,还可以消费dubbo暴露的服务. 原因 健壮性 l 监控中心宕掉不影响使用,只是丢失部分采样数据 l 数据库宕掉后,注册中心仍能通过缓存提供服务列表查询,但不能注册新服务 l 注册中心对等集群,任意一台宕掉后,将自动切换到另一台 l 注册中心全部宕掉后,服务提供者和服务消费者仍能通过本地缓存通讯 l 服务提供者无状态,任意一台宕掉后,不影响使用 l 服务提供者全部宕掉后,服务消费者应用将无法使用,并无限次重连等待服务提

dubbo本地搭建实例

项目文件下载地址:http://download.csdn.net/detail/aqsunkai/9552711 概述 Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案. 其核心部分包含 远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及"请求-响应"模式的信息交换方式. 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持. 自

Dubbo源码分析(三):Dubbo之服务端(Service)

如上图所示的Dubbo的暴露服务的过程,不难看出它也和消费者端很像,也需要一个像reference的对象来维护service关联的所有对象及其属性,这里的reference就是provider.由于ServiceBean实现了  InitializingBean接口,所有在Spring实例化这个bean后会调用接口方法afterPropertiesSet: public void afterPropertiesSet() throws Exception { //如果没有配置provider