一, dubbo 服务化架构包含的内容
对于传统工程而言, 分层的依据是按照包来区分. 由于在相同的工程中, 所以服务的提供和调用可以方便的实现.
但是对于分布式架构而言, 服务的提供者负责服务具体的实现和接口规范, 服务的消费者只关心接口规范即可. 但是
无论是服务的提供者还是服务的消费者都会涉及到诸如公共工具类, 接口, DO,VO, 等公共代码, 因此一个简单的
dubbo 服务架构模式如下:
服务提供者: 提供服务接口的实现, 发布服务地址, 提供服务
服务消费者: 获取服务地址, 使用服务接口调用服务, 处理服务调用结果
公共项目: 包含公共配置: DO(和数据库同步, 用于持久化对象),VO(传输数据), 工具包, 接口等
依赖关系: 依赖项目就像依赖 jar 包一样
二, 创建公共项目工程
创建公共项目工程, 普通的 maven 项目, 提供 utils,DO, 接口等代码
三, 服务提供者实现
普通的 Maven 工程(依赖 Dubbo), 提供服务实现, 服务启动功能.
1: 创建项目并导入 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>
2: 实现本地服务类, 不需要标记远程服务
在调用过程中存在数据传输, 因此需要转成二进制, 因此服务对象需要实现序列化, 也就是实现 Serializable 接口
3: 以 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>
4: 启动 spring, 并且加载配置文件, 才能发布服务
启动服务监听
- String configLocation = "classpath*:/dubbo-provider.xml";
- ApplicationContext context = new ClassPathXmlApplicationContext(configLocation);
- System.out.println("dubbo-server 服务正在监听, 按任意键退出");
- System.in.read();// 作用是从键盘读出一个字符, 返回 unicode 编码 (数字) 此处用来终止程序结束, 因为不输出就不会结束
四, 服务消费者实现
创建服务消费者项目: 普通的 Maven 工程(依赖 Dubbo), 完成服务调用功能.
1: 创建项目并导入 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>
2: 通过配置文件对接口的配置获取本地代理对象的代码
- 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>
3: 给接口产生了本地代理对象, 并且把它纳入 spring 管理
4: 直接注入代理对象, 调用方法完成远程调用
JUnit4 调用 dubbo 服务测试类
- importorg.junit.Test;
- importorg.junit.runner.RunWith;
- importorg.springframework.test.context.ContextConfiguration;
- importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- importcom.alibaba.dubbo.config.annotation.Reference;
- importcn.itsource.dubbo.core.service.IHelloService;
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration({
- "classpath*:/dubbo-consumer.xml"
- })
- publicclassDubboServiceTest {
- @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 服务打包
1.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)
2: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>
3:dubbo 服务 jar 包运行
Cmd 窗口:
1, 定位到 jar 包所在目录
2, 输入命令并回车执行: java -jar xxxxx.jar &
dubbo 服务打包是部署 dubbo 服务的必要方式之一. 当执行 java -jar 时, 会自动寻找程序入口 (main 函数) 并运行.
& 保持一致运行.
dubbo 本地服务化实现(dubbo 三)
来源: http://www.bubuko.com/infodetail-2980996.html