tnblog
首页
视频
资源
登录

dubbo快速入门-spring和springmvc整合 02

2937人阅读 2022/8/22 1:14 总访问:1445424 评论:0 收藏:0 手机
分类: Dubbo

一、

案例、
1、创建空项目

给项目设置jdk

创建完后的样子

2、设置Maven

3、创建模块
3.1、创建bubbo-service模块

模块的位置:选到空项目的位置以后加上 ”\模块名称“就是模块的位置

3.2、创建bubbo-web模块,和上面一样。
将来用web模块依赖于service模块。

两个模块创建完后的样子

4、导入坐标
4.1、dubbo-service模块的坐标

<properties>
    <spring.version>5.1.9.RELEASE</spring.version>
    <dubbo.version>2.7.4.1</dubbo.version>
   <zookeeper.version>4.0.0</zookeeper.version>
</properties>
<dependencies>
    <!--    servlet规范-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <!--        spring-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!--springMVC-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- 日志-->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.21</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.21</version>
    </dependency>
    <!--Dubbo的起步依赖,版本2.7之后统一为rg.apache .dubb -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo</artifactId>
        <version>${dubbo.version}</version>
    </dependency>
    <!--        zookeeper客户端实现-->
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>${zookeeper.version}</version>
    </dependency>
    <!--        zookeeper客户端实现-->
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-recipes</artifactId>
        <version>${zookeeper.version}</version>
    </dependency>
</dependencies>

4.2、dubbo-web模块的坐标,注意:因为它是web项目所以最好打war包,还有它依赖于dubbo-service模块

<!--    打war包-->
<packaging>war</packaging>

<!--    版本控制-->
<properties>
    <spring.version>5.1.9.RELEASE</spring.version>
    <dubbo.version>2.7.4.1</dubbo.version>
    <zookeeper.version>4.0.0</zookeeper.version>
</properties>
<dependencies>
    <!--    servlet规范-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <!--        spring-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!--springMVC-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- 日志-->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.21</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.21</version>
    </dependency>
    <!--Dubbo的起步依赖,版本2.7之后统一为rg.apache .dubb -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo</artifactId>
        <version>${dubbo.version}</version>
    </dependency>
    <!--        zookeeper客户端实现-->
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>${zookeeper.version}</version>
    </dependency>
    <!--        zookeeper客户端实现-->
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-recipes</artifactId>
        <version>${zookeeper.version}</version>
    </dependency>
    <!-- 依赖dubbo-service模块-->
    <dependency>
        <groupId>com.itheima</groupId>
        <artifactId>dubbo-service</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <!--  tomcat7-->
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
            <!--http默认端口改为8000端口-->
            <configuration>
                <!--修改tomcat启动的端口号-->
                <port>8000</port>
                <!--
                修改tomcat部署当前项目的虚拟目录(项目的访问根路径)
                这改为 / 就表示你访问localhost/就是你的index页面
                -->
                <path>/</path>
            </configuration>
        </plugin>
    </plugins>
</build>

5、dubbo-service模块创建的内容
5.1、红线为新创建的内容

5.2、applicationContext.xml的配置(对spring的配置)

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!--  包扫描-->
    <context:component-scan base-package="com.itheima.service"/>

</beans>

5.3、log4j.properties的配置(配置日志)

# 配置根Logger
log4j.rootLogger=info, ServerDailyRollingFile, stdout
# 有关日志输出位置的配置命名
# ServerDailyRollingFile代表每天产生一个日志文件
log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender
# 设置时间格式
log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd
# 日志文件生成路径
log4j.appender.ServerDailyRollingFile.File=logs/notify-subscription.log
# 日志格式
# PatternLayout可以灵活指定布局模式
log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d - %m%n
# 将消息增加到指定文件中,false指消息将覆盖指定文件的内容
log4j.appender.ServerDailyRollingFile.Append=true
# 配置输出到控制台
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %p [%c] %m%n

6、对dubbo-web模块的配置
6.1、红线为新创建的内容

6.2、springmvc.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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--    扫描sprig的bean-->
    <context:component-scan base-package="com.itheima.controller"/>
    <!--    mvc驱动-->
    <mvc:annotation-driven/>
</beans>

6.3、log4j.properties的配置和上面dubbo-service模块的日志配置一样
6.4、web.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <!--对spring配置的加载-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/applicationContext.xml</param-value>
    </context-param>
    <!--启动服务器时,通过监听器加载spring运行环境-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--用于解决中文乱码-->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--拦截-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:spring/springmvc.xml</param-value>
        </init-param>
    </servlet>
    <!--    <servlet-mapping>-->
    <!--        <servlet-name>DispatcherServlet</servlet-name>-->
    <!--        <url-pattern>/</url-pattern>-->
    <!--    </servlet-mapping>-->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

7、对dubbo-service模块进行install的作用(maven的install可以将项目/模块本身编译并打包到本地仓库

7.1、

对dubbo-service模块执行install命令

8、给dubbo-web模块写启动的命令。和步骤7类似

9、启动dubbo-web模块

启动后的端口

10、测试

评价
没有个性,不需要签名
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术