码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-03-09 01:20:09      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:resource   框架   广播   微服务   targe   ihe   toc   client   数据库同步   

一、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服务。如果checkfalse,启动时不会报错,调用时才报错。如果checktrue,启动时就报错。调试时用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

利用TomcatJettyWEB容器启动Dubbo服务。

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

2、JavaMain方法/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三)

标签:resource   框架   广播   微服务   targe   ihe   toc   client   数据库同步   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!