0 课程地址
https://coding.imooc.com/lesson/201.html#mid=12744
1 重点关注
1.1 本节内容
使用tomcat启动dubbo服务
tomcat启动dubbo服务的弊端
2 课程内容
2.1 tomcat启动dubbo服务的弊端
tomcat本身也是软件,占用内存
3 Coding
3.1 使用tomcat启动dubbo服务
- 主要改造点:
涉及父项目,common项目,service和api项目共4个,
涉及
a service项目的pom文件改造
b service项目的log4j改造
c 启动配置
d 启动后的日志查看
- a service项目的pom文件改造
1 pom文件引入dubbo和zk依赖(注意聚合的项目只是引用,具体哪个server需要就在哪个service所在项目的pom中进行引用该依赖)
将父项目的依赖如下
<dubbo.version>2.5.3</dubbo.version> <zookeeper.version>3.4.11</zookeeper.version> <zkclient.version>0.1</zkclient.version> <!-- 引入dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>${zookeeper.version}</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>${zkclient.version}</version> </dependency>
移到service项目的pom文件中并修改,如下
<!-- 引入dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> </dependency>
2 pom文件 dependencies dubbo版本排除依赖spring,因为dubbo自带的spring版本2.5.6和pom文件里的spring版本有冲突,所以要把dubbo内置的spring依赖排除掉
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency>
3 packing 里是war包
<packaging>war</packaging>
4 引入tomcat7插件
<build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <port>8081</port> <path>/</path> <useBodyEncodingForURI>true</useBodyEncodingForURI> <uriEncoding>UTF-8</uriEncoding> </configuration> </plugin> <plugin> <artifactId>maven-eclipse-plugin</artifactId> <configuration> <additionalProjectnatures> <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature> </additionalProjectnatures> <additionalBuildcommands> <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand> </additionalBuildcommands> <downloadSources>true</downloadSources> <downloadJavadocs>true</downloadJavadocs> </configuration> </plugin> </plugins> </build>
- b log4j配置文件修改(log4j copy到item-service子项目里)
log4j.rootLogger=INFO,console
#INFO,console,dailyFile
log4j.logger.com.imooc.mapper=INFO
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.encoding=UTF-8
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%l] - [%p] %m%n
- c 启动配置
1 启动前install下
右键父项目,run as ->maven install,结果如下
2 上方菜单栏 debug按钮,debug configurations
3 maven build tree进行new configuration->更改Name为imooc-item,wokespace选择item-service项目
goals 输入:clean tomcat7:run
然后apply
source remove掉原来的,然后add ->java project->选择imooc dubbo的api,service和common,
然后apply
最后开始debug
- d 启动后的日志查看
启动后的日志查看,能看到 register dubbo 对应的ip和service已经启动