背景:
传统的项目是单一的,当业务不断增长,系统的功能不断增加,系统就会边的臃肿庞大难以维护,最常见的就是,修改一个小功能,整个系统得重新部署启动,对于频繁的迭代,令人抓狂。近几年,模块化的思想兴起,将庞大的系统拆分成多个功能模块,这样很大程度上解决的以上的烦恼。旧的的问题解决,但又带来新的问题,模块之间的服务调用通信怎么解决?rpc解决了这个问题,dubbo就是rpc的典型代表。
使用(基于sprng框架系列):
1.使用主流的开发框架springmvc与dubbo整合,maven管理依赖包。
2.项目结构如图:
使用maven的继承和聚合功能来管理项目的。
3.上篇文章提到,dubbo是分为消费方和生产方的,这里phoenix-organ-web 代表消费方(多个);phoenix-organ-provider代表生产方;phoenix-organ-api则是一个接口,里面包含生产方需要实现,消费方需要调用的接口和实体类,准确的说 api是需要打包发布私服的,然后消费方和生产方都需要通过坐标引用该jar包的,双方的引用api的目的不同,生产方需要具体实现这些接口的功能,消费方式主要是通过来找到需要调用的方法而已。
4.这三个子项目中除了基本的spring配置文件,还有重要的几个配置文件,这才是关键。
phoenix-organ-api:organ-dubbo-consumer.xml 内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-redis="http://www.springframework.org/schema/integration/redis"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:stream="http://www.springframework.org/schema/integration/stream"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-2.1.xsd
http://www.springframework.org/schema/integration/redis
http://www.springframework.org/schema/integration/redis/spring-integration-redis-2.1.xsd
http://www.springframework.org/schema/integration/stream
http://www.springframework.org/schema/integration/stream/spring-integration-stream-2.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
<dubbo:reference id="oneService" interface="com.test.service.OneService" version="2.0.0"/>
<dubbo:reference id="twoService" interface="com.test.service.TwoService" version="2.0.0"/>
<dubbo:reference id="threeService" interface="com.test.service.ThreeService" version="2.0.0"/>
</beans>
version:标示服务的唯一性,除此 还有端口号。
phoenix-organ-provider:organ-dubbo-provider.xml 代码如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-redis="http://www.springframework.org/schema/integration/redis"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:stream="http://www.springframework.org/schema/integration/stream"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-2.1.xsd
http://www.springframework.org/schema/integration/redis
http://www.springframework.org/schema/integration/redis/spring-integration-redis-2.1.xsd
http://www.springframework.org/schema/integration/stream
http://www.springframework.org/schema/integration/stream/spring-integration-stream-2.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
<dubbo:service interface="com.test.service.OneService" ref="oneService" version="2.0.0"/>
<dubbo:service ref="twoService" interface="com.test.service.TwoService" version="2.0.0"/>
<dubbo:service ref="threeService" interface="com.test.service.ThreeService" version="2.0.0"/>
organ-dubbo-config.xml ,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-redis="http://www.springframework.org/schema/integration/redis"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:stream="http://www.springframework.org/schema/integration/stream"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-2.1.xsd
http://www.springframework.org/schema/integration/redis
http://www.springframework.org/schema/integration/redis/spring-integration-redis-2.1.xsd
http://www.springframework.org/schema/integration/stream
http://www.springframework.org/schema/integration/stream/spring-integration-stream-2.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
<dubbo:application name="${dubbo.application.name}" />
<dubbo:protocol name="${dubbo.protocol.name}" port="${dubbo.protocol.port}" />
<dubbo:registry address="${dubbo.registry.address}" protocol="${dubbo.protocol.name}"/>
<dubbo:consumer timeout="10000" check="false" />
</beans>
applicationContext.xml 中代码添加如下:
<import resource="classpath:organ-dubbo-config.xml" />
<import resource="classpath:organ-dubbo-provider.xml" />
phoenix-organ-web:organ-dubbo-config.xml 代码如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-redis="http://www.springframework.org/schema/integration/redis"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:stream="http://www.springframework.org/schema/integration/stream"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-2.1.xsd
http://www.springframework.org/schema/integration/redis
http://www.springframework.org/schema/integration/redis/spring-integration-redis-2.1.xsd
http://www.springframework.org/schema/integration/stream
http://www.springframework.org/schema/integration/stream/spring-integration-stream-2.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
<dubbo:application name="${dubbo.application.name}" />
<dubbo:protocol name="${dubbo.protocol.name}" />
<dubbo:registry address="${dubbo.registry.address}" protocol="${dubbo.protocol.name}"/>
<dubbo:consumer timeout="10000" check="false" />
</beans>
applicationContext.xml 中代码添加如下:
<import resource="classpath:organ-dubbo-config.xml" />
<import resource="classpath:organ-dubbo-consumer.xml" />
5.启动dubbo。启动dubbo的时候,需要在phoenix-organ-provider 中启动,需要做一些启动配置,还有打包插件。
首先在 pom.xml 里添加插件插件配置:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
assembly.xml 代码如下:
<!--
- Copyright 1999-2011 Alibaba Group.
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-->
<assembly>
<id>assembly</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/dubbo/META-INF/assembly/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
</fileSet>
<fileSet>
<!-- 把替换好的属性文件输出到打包后的conf下 -->
<directory>${project.build.directory}/classes/conf</directory>
<outputDirectory>conf</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>
插件配置好以后 ,再配置dubbo的文件,dubbo.properties 和 log4j.xml
dubbo.properties 代码:
dubbo.spring.config=classpath:applicationContext.xml
dubbo.log4j.config=classpath:log4j.xml
organ.jdbc.driverClassName=com.mysql.jdbc.Driver
organ.jdbc.url=${jdbc.url}
organ.jdbc.username=${jdbc.username}
organ.jdbc.password=${jdbc.password}
cache.expiredTime=86400
organ.redis.host=${redis.host}
organ.redis.port=${redis.port}
organ.mongodb.address=${mongodb.address}
organ.mongodb.database=${mongodb.database}
organ.dubbo.application.name=${dubbo.application.name}
organ.dubbo.registry.address=${dubbo.registry.address}
organ.dubbo.protocol.name=${dubbo.protocol.name}
organ.dubbo.protocol.port=${dubbo.protocol.port}
organ.bigdata.host=${organ.bigdata.host}
log4j.path=${log4j.path}
log4j.level=${log4j.level}
log4j.xml 代码如下:
<?xml version="1.0"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- 控制台日志 <appender name="consolelog" class="org.apache.log4j.ConsoleAppender">
<param name="encoding" value="UTF-8" /> <layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyyMMddHHmmss} %m%n %p %c %t "
/> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param
name="LevelMax" value="error"></param> <param name="LevelMin" value="error"></param>
<param name="AcceptOnMatch" value="true" /> </filter> </appender> -->
<!-- 全部日志 -->
<appender name="normal_log_file" class="org.apache.log4j.DailyRollingFileAppender">
<param name="encoding" value="UTF-8" />
<param name="File" value="${log4j.path}/phoenix-organ/normal.log" />
<param name="DatePattern" value=".yyyyMMddHH" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%p %d{yyyyMMddHHmmss} %c %t %m%n" />
</layout>
</appender>
<logger name="com.ginkgocap.ywxt">
<level value="debug"></level>
</logger>
<root>
<level value="error" />
<appender-ref ref="normal_log_file" />
</root>
</log4j:configuration>
开始编译:
启动配置,com.alibaba.dubbo.container.Main是dubbuo自带的启动主类,同时引入编译好的conf下的两个文件:
启动成功,控制台会有一行启动成功的打印日志。
启动成功后,到dubbo监控中心(http://监控中心IP:9099/)查看,是否正确启动。
通过插件打包成 tar.gz 包,上传服务器,解压,运行start.sh 启动服务。