最近在做的项目用到了springboot、dubbo等等一些的东西,因为我之前没有正式接触过dubbo,所以对其框架进行了一定的学习,自己搭建了一个demo,仅为个人学习总结,大佬们有好的建议或改进方法欢迎评价!
首先来看一下整个项目的结构
其中:
common ---> 公共的工具类
system ---> 真正的业务模块
web ---> 接口提供模块
在各个业务模块中,又分为4个模块:
service-boot ---> 启动模块,只包含了启动的main
client ---> 业务模块的入口,包含一些入参出参以及service接口
service ---> 真正的处理业务的业务模块
datasource ---> 数据库交互模块,负责与数据库进行数据交互
在web模块中,又分为2个模块:
web-boot ---> 启动模块,只包含了启动的main
web ---> 接口的入口,包括入口controller和一些拦截器以及swagger等一些配置信息
再来看一下dubbo需要的依赖
<!-- dubbo、zk依赖-->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
<exclusions>
<exclusion>
<artifactId>dubbo</artifactId>
<groupId>com.alibaba</groupId>
</exclusion>
<exclusion>
<artifactId>zkclient</artifactId>
<groupId>com.101tec</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.5</version>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
其他的依赖包省略。。。
配置文件如下(注意端口不可重复):
server:
port: 8023
spring:
dubbo:
application:
name: system-local
registry:
address: zookeeper://localhost:2181
protocol:
name: dubbo
port: 20823
scan: com.jaiaxn.distribute
spring.datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://106.13.65.122:3306/jaiaxn_test_db?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8
username: jaiaxn
password: jaiaxn_!@#456
logging.level.com.jaiaxn.distribute.system.mapper: debug
mybatis-plus:
mapper-locations: classpath:system/mapper/*.xml
#主键生成策略
db-type: TelDB
mybatis:
configuration:
map-underscore-to-camel-case: true
在添加完依赖和配置文件之后,我们就可以开始项目内容的填充了
需要注意的事项
在业务逻辑层,编写service的时候,我们要注意注解的使用:
@Service使用的是com.alibaba.dubbo.config.annotation.Service,而不是org.springframework.stereotype.Service
在其他模块(例如web)调用业务模块,获取service的时候,要用@Reference注解:
该注解用的是com.alibaba.dubbo.config.annotation.Reference,而不是jdk.nashorn.internal.ir.annotations.Reference或者org.springframework.data.annotation.Reference
最后,再给项目填一点枝叶
整合mybatis-plus和swagger,
数据库交互进行简化,提供接口详细信息(类似于接口协议)
依赖:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
关键代码:
package com.jaiaxn.distributed.web.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author: wang.jiaxin
* @date: 2019年03月23日
* @description: swagger-ui
**/
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//修正Byte转string的Bug
.directModelSubstitute(Byte.class, Integer.class)
.select()
//为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.jaiaxn.distributed"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("Project Restful API")
//描述
.description("API 描述")
//创建人
.contact(new Contact("jaiaxn", "https://github.com/jaiaxnGithub/distributed-parent", "[email protected]"))
//版本号
.version("1.0")
.build();
}
}
附上源代码地址: