写点什么

Dubbo 实战案例 01【需求分析及项目创建】

  • 2022 年 5 月 06 日
  • 本文字数:5943 字

    阅读完需:约 19 分钟

</dependency>


<dependency>


<groupId>javax.servlet</groupId>


<artifactId>jsp-api</artifactId>


<version>${jsp-api.version}</version>


<scope>provided</scope>


</dependency>


<dependency>


<groupId>com.alibaba</groupId>


<artifactId>dubbo</artifactId>


<version>${dubbo-version}</version>


</dependency>


<dependency>


<groupId>com.101tec</groupId>


<artifactId>zkclient</artifactId>


<version>${zkClient-version}</version>


</dependency>


</dependencies>


</dependencyManagement>


<build>


<resources>


<resource>


<directory>src/main/java</directory>


<includes>


<include>**/*.xml</include>


</includes>


</resource>


<resource>


<directory>src/main/resources</directory>


<includes>


<include>**/*.xml</include>


<include>**/*.properties</include>


</includes>


</resource>


</resources>


<pluginManagement>


<plugins>


<plugin>


<groupId>org.apache.tomcat.maven</groupId>


<artifactId>tomcat7-maven-plugin</artifactId>


<version>${tomcat.version}</version>


</plugin>


</plugins>


</pluginManagement>


</build>


[](()2.创建 dubbo-pojo



[](()2.1 创建项目


[](()2.2 创建实体类

package com.bobo.pojo;


public class User {


private int id;


private String username;


private int userage;


public int getId() {


return id;


}


public void setId(int id) {


this.id = id;


}


public String getUsername() {


return username;


}


public void setUsername(String username) {


this.username = username;


}


public int getUserage() {


return userage;


}


public void setUserage(int userage) {


this.userage = userage;


}


}


![在这里插入图片描述](https://img-blog.csdnimg.cn/20190327184413225.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9kcGItYm9ib2thb3lhLXNtLmJsb2cuY3Nkbi5uZXQ=,size_16 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 ,color_FFFFFF,t_70)


[](()3.创建 dubbo-mapper



[](()3.1 创建项目

[](()3.2 创建 UserMapper 接口

package com.bobo.mapper;


/**


  • UsersMapper 接口文件

  • @author dengp


*/


public interface UsersMapper {


}

[](()3.3 创建 UsersMapper 映射配置文件

<?xml version="1.0" encoding="UTF-8" ?>


<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD


Mapper 3.0//EN"


"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >


<mapper namespace="com.bobo.mapper.UsersMapper">


</mapper>

[](()3.4 4 修改 POM 文件

<dependencies>


<dependency>


<groupId>com.bobo</groupId>


<artifactId>dubbo-pojo</artifactId>


<version>0.0.1-SNAPSHOT</version>


</dependency>


<dependency>


<groupId>org.mybatis</groupId>


<artifactId>mybatis</artifactId>


</dependency>


<dependency>


<groupId>org.mybatis</groupId>


<artifactId>mybatis-spring</artifactId>


</dependency>


<dependency>


<groupId>mysql</groupId>


<artifactId>mysql-connector-java</artifactId>


</dependency>


<dependency>


<groupId>com.alibaba</groupId>


<artifactId>druid</artifactId>


</dependency>


</dependencies>


<build>


<resources>


<resource>


<directory>src/main/java</directory>


<includes>


<include>**/*.xml</include>


</includes>


</resource>


</resources>


</build>


[](()4.创建服务提供者相关项目



[](()4.1 创建 dubbo-user-provider

[](()4.1.1 创建项目

[](()4.2 创建 dubbo-user-interface

[](()4.2.1 创建项目

[](()4.2.2 修改 POM 文件

<dependencies>


<dependency>


<groupId>com.bobo</groupId>


<artifactId>dubbo-pojo</artifactId>


<version>0.0.1-SNAPSHOT</version>


</dependency>


</dependencies>

[](()4.3 创建 dubbo-user-service

[](()4.3.1 创建项目

[](()4.3.2 修改 POM 文件

<dependencies>


<dependency>


<groupId>com.bobo</groupId>


<artifactId>dubbo-user-interface</artifactId>


<version>0.0.1-SNAPSHOT</version>


</dependency>


<dependency>


<groupId>com.bobo</groupId>


<artifactId>dubbo-mapper</artifactId>


<version>0.0.1-SNAPSHOT</version>


</dependency>


<dependency>


<groupId>com.alibaba</groupId>


<artifactId>dubbo</artifactId>


</dependency>


<dependency>


<groupId>com.101tec</groupId>


<artifactId>zkclient</artifactId>


</dependency>


<dependency>


<groupId>org.springframework</groupId>


<artifactId>spring-jdbc</artifactId>


</dependency>


<dependency>


<groupId>org.springframework</groupId>


<artifactId>spring-aspects</artifactId>


</dependency>


</dependencies>

[](()4.3.3 配置 MyBatis 与 Dubbo

[](()db.properties

jdbc.driver=com.mysql.jdbc.Driver


jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8


jdbc.username=root


jdbc.password=123456

[](()applicationContext-dao.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:p="http://www.springframework.org/schema/p"


xmlns:context="http://www.springframework.org/schema/context"


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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd


http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


<context:property-placeholder location="classpath:db.properties"/>


<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">


<property name="url" value="${jdbc.url}" />


<property name="username" value="${jdbc.username}" />


<property name="password" value="${jdbc.password}" />


<property name="driverClassName" value="${jdbc.driver}" />


<property name="maxActive" value="10" />


<property name="minIdle" value="5" />


</bean>


<bean class="org.mybatis.spring.SqlSessionFactoryBean">


<property name="dataSource">


<ref bean="dataSource"/>


</property>


<property name="configLocation">


<value>classpath:SqlMapperClient.xml</value>


</property>


</bean>


<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">


<property name="basePackage" value="com.bobo.mapper"/>


</bean>


</beans>

[](()applicationContext-service.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:p="http://www.springframework.org/schema/p"


xmlns:context="http://www.springframework.org/schema/context"


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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd


http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


<context:component-scan base-package="com.bobo.dubbo.service.impl"/>


</beans>

[](()applicationContext-trans.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:p="http://www.springframework.org/schema/p"


xmlns:aop="http://www.springframework.org/schema/aop"


xmlns:tx="http://www.springframework.org/schema/tx"


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-4.0.xsd


http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd


http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd


http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


<bean id="transactionMananger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">


<property name="dataSource" ref="dataSource"/>


</bean>


<tx:advice id="advice" transaction-manager="transactionMananger">


tx:attributes


<tx:method name="add*" propagation="REQUIRED"/>


<tx:method name="modify*" propagation="REQUIRED"/>


<tx:method name="update*" propagation="REQUIRED"/>


<tx:method name="dorp*" propagation="REQUIRED"/>


<tx:method name="del*" propagation="REQUIRED"/>


<tx:method name="find*" read-only="true"/>


</tx:attributes>


</tx:advice>


aop:config


<aop:advisor advice-ref="advice" pointcut="execution(* com.bobo.dubbo.service.impl*.*(..))"/>


</aop:config>


</beans>

[](()SqlMapperClient.xml

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE configuration


PUBLIC "-//mybatis.org//DTD Config 3.0//EN"


"http://mybatis.org/dtd/mybatis-3-config.dtd">


<configuration>


</configuration>

[](()application-dubbo.xml

服务提供者的配置文件我们放到 META-INF/spring/目录下,我们通过 main 方法启动会自动加载该文件。



<?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:aop="http://www.springframework.org/schema/aop"


xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"


xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"


xsi:schemaLocation="http://www.springframework.org/schema/beans


http://www.springframework.org/schema/beans/spring-beans.xsd


http://www.springframework.org/schema/aop


http://www.springframework.org/schema/aop/spring-aop.xsd


http://www.springframework.org/schema/tx


http://www.springframework.org/schema/tx/spring-tx.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context.xsd


http://code.alibabatech.com/schema/dubbo


http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


<import resource="../../applicationContext-dao.xml" />


<import resource="../../applicationContext-service.xml" />


<import resource="../../applicationContext-trans.xml" />


<dubbo:application name="user-provider" />


<dubbo:registry protocol="zookeeper"


address="192.168.88.171:2181,192.168.88.172:2181,192.168.88.173:2181"/>


<dubbo:protocol name="dubbo" port="20880"/>


</beans>

[](()4.3.3 测试整合

package com.bobo;


import com.alibaba.dubbo.container.Main;


public class Start {


/**


  • 特点:

  • 1,自带线程阻塞

  • 2,支持优雅关系

  • Main 类下的 main 方法在启动时默认的回去 classpath:/META-INF/spring/*.xml

  • @param args


*/


public static void main(String[] args) {


Main.main(args);


}


}



[](()5.创建服务消费者相关项目




创建消费者相关的服务。

[](()5.1 创建 dubbo-user-consumer

[](()5.1.1 创建项目

[](()5.2 创建 dubbo-user-portal-service

[](()5.2.1 创建项目

[](()5.2.2 修改 POM 文件

<dependencies>


<dependency>


<groupId>com.bobo</groupId>


<artifactId>dubbo-user-interface</artifactId>


<version>0.0.1-SNAPSHOT</version>


</dependency>


<dependency>


<groupId>com.bobo</groupId>


<artifactId>dubbo-pojo</artifactId>


<version>0.0.1-SNAPSHOT</version>


</dependency>


<dependency>


<groupId>org.springframework</groupId>


<artifactId>spring-context</artifactId>


</dependency>


<dependency>


<groupId>org.springframework</groupId>


<artifactId>spring-beans</artifactId>


</dependency>


</dependencies>

[](()5.3 创建 dubbo-user-portal

[](()5.3.1 创建项目(war)

[](()5.3.2 修改 POM 文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


<modelVersion>4.0.0</modelVersion>


<parent>


<groupId>com.bobo</groupId>


<artifactId>dubbo-user-consumer</artifactId>


<version>0.0.1-SNAPSHOT</version>


</parent>


<artifactId>dubbo-user-portal</artifactId>


<packaging>war</packaging>


<dependencies>


<dependency>


<groupId>com.bobo</groupId>


<artifactId>dubbo-user-portal-service</artifactId>


<version>0.0.1-SNAPSHOT</version>


</dependency>


<dependency>


<groupId>junit</groupId>


<artifactId>junit</artifactId>


</dependency>


<dependency>


<groupId>org.slf4j</groupId>


<artifactId>slf4j-log4j12</artifactId>


</dependency>


<dependency>


<groupId>org.springframework</groupId>


<artifactId>spring-webmvc</artifactId>


</dependency>


<dependency>


<groupId>jstl</groupId>


<artifactId>jstl</artifactId>


</dependency>


<dependency>


<groupId>javax.servlet</groupId>


<artifactId>servlet-api</artifactId>


<scope>provided</scope>


</dependency>


<dependency>


<groupId>javax.servlet</groupId>


<artifactId>jsp-api</artifactId>


<scope>provided</scope>


</dependency>


<dependency>


<groupId>com.alibaba</groupId>


<artifactId>dubbo</artifactId>


</dependency>


<dependency>


<groupId>com.101tec</groupId>


<artifactId>zkclient</artifactId>


</dependency>


</dependencies>


<build>


<plugins>


<plugin>


<groupId>org.apache.tomcat.maven</groupId>


<artifactId>tomcat7-maven-plugin</artifactId>

用户头像

还未添加个人签名 2022.04.13 加入

还未添加个人简介

评论

发布
暂无评论
Dubbo实战案例01【需求分析及项目创建】_Java_爱好编程进阶_InfoQ写作社区