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函数)并运行。
&保持一致运行。
dubbo本地服务化实现(dubbo三)的更多相关文章
- 本地Windows环境Dubbo搭建测试
Dubbo介绍 Dubbo[]是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案. 其核心部分包含: 远程通讯: 提供对多种基于长连接的NIO框架抽象封装, ...
- Dubbo 04 服务化最佳实现流程
Dubbo 04 服务化最佳实践 分包 建议将服务接口.服务模型.服务异常等均放在 API 包中,因为服务模型和异常也是 API 的一部分,这样做也符合分包原则:重用发布等价原则(REP),共同重用原 ...
- DUBBO本地搭建及小案例
DUBBO的介绍部分我这里就不介绍了,大家可参考官方文档. DUBBO的注册中心安装 DUBBO的注册中心支持好几种,公司用到zookeeper注册中心,所以我这边只说明zookeeper注册中心如何 ...
- 【dubbo】服务提供者运行的三种方式
[dubbo]服务提供者运行的三种方式 学习了:https://blog.csdn.net/yxwb1253587469/article/details/78712451 1,使用容器: 2,使用自建 ...
- 【2020-03-21】Dubbo本地环境搭建-实现服务注册和消费
前言 本周主题:加班工作.本周内忙于CRUD不能自拔,基本每天都是九点半下班,下周上线,明天还要加班推进进度.今天是休息日,于是重拾起了dubbo,打算近期深入了解一下其使用和原理.之所以说是重拾,是 ...
- Dubbo本地存根是什么,Dubbo本地伪装又是什么?
真正的大师永远怀着一颗学徒的心 哈喽!大家好,我是小奇,一位程序员界的学徒 小奇打算以轻松幽默的对话方式来分享一些技术,如果你觉得通过小奇的文章学到了东西,那就给小奇一个赞吧 前言 书接上回,昨天打了 ...
- DUBBO本地搭建及小案例 (转)
DUBBO的介绍部分我这里就不介绍了,大家可参考官方文档. DUBBO的注册中心安装 DUBBO的注册中心支持好几种,公司用到zookeeper注册中心,所以我这边只说明zookeeper注册中心如何 ...
- dubbo本地调试直连
服务: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://ww ...
- Dubbo本地调试
dubbo 启动标志 Dubbo service server started <dubbo:reference id="transferTimingUploadHisRPCServi ...
随机推荐
- codeforces/contest/803/problem C
题目:C. Maximal GCD 题意:输入n,k.将n拆成k个数的序列,使得这k个数的gcd最大.(且序列严格递增).1 ≤ n, k ≤ 1010 . 分析:假设k个数的gcd为d,则一定有d| ...
- CentOS7 Docker私有仓库搭建及删除镜像 【转】
文章来源:centos7 Docker私有仓库搭建及删除镜像 如果不想用私有镜像库,你可以用docker的库 https://hub.docker.com 环境准备 环境:两个装有Docker 17. ...
- SQLServer之PRIMARY KEY约束
PRIMARY KEY约束添加规则 1.在表中常有一列或多列的组合,其值能唯一标识表中的每一行,这样的一列或多列成为表的主键(PrimaryKey). 2.一个表只能有一个主键,而且主键约束中的列不能 ...
- jenkins使用开始踩坑(1)
上篇文章 安装教程 :https://www.cnblogs.com/linuxchao/p/linuxchao-jenkins-setup.html 一.前戏 话说上一篇文章安装完 JDK 和 je ...
- Python面试常见的问题
So if you are looking forward to a Python Interview, here are some most probable questions to be ask ...
- 3.15 总结,初始java
- PHP奇淫技巧
https://www.jb51.net/list/list_67_1.htm PHP技巧:https://www.jb51.net/list/list_67_13.htm mysql三范式 1NF: ...
- 论PHP框架设计模式及MVC的缺陷
目前主流的PHP框架设计模式均为MVC模式,比如yii或codeigniter,均是由控制器接收页面请求,并沟通模型与视图的交互.如果我们把网站整体看作一个矩阵,把网站接收用户请求并处理看作是网站的竖 ...
- openstack搭建之-glance配置(9)
一. base节点配置 #设置数据库,创建glance数据库,并设置权限 mysql -u root -proot CREATE DATABASE glance; GRANT ALL PRIVILEG ...
- Kafka--消息队列
说明:解耦指的是客户端A和客户端B不需要同步,两者之间的通信是异步的:消息队列是可以复制备份的,因此消息队列具有冗余性和可恢复性:所谓拓展性指的是,在大型分布式系统中,消息队列是分布在集群中的:队列是 ...