一、基本环境搭建
这里使用jdk8+maven3.6+mysql5.5
1、创建基本工程:
其中common公共模块pom文件导入如下:
shanjupay com.shanjupay 1.0-SNAPSHOT 4.0.0 shanjupay-common org.slf4j slf4j-api com.alibaba fastjson javax.servlet javax.servlet-api provided commons-lang commons-lang io.springfox springfox-swagger2 io.springfox springfox-swagger-ui org.projectlombok lombok org.springframework.boot spring-boot-maven-plugin true ${project.name} org.apache.maven.plugins maven-compiler-plugin 1.8 ${java.home}/lib/rt.jar;${java.home}/lib/jce.jar 项目父模块pom文件如下:
4.0.0 com.shanjupay shanjupay pom 1.0-SNAPSHOT shanjupay-common org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE UTF-8 UTF-8 1.8 1.18.0 1.3.0.Final com.alibaba.cloud spring-cloud-alibaba-dependencies 2.1.0.RELEASE pom import org.springframework.cloud spring-cloud-dependencies Greenwich.RELEASE pom import io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2 org.springframework.security spring-security-jwt 1.0.9.RELEASE org.springframework.security.oauth.boot spring-security-oauth2-autoconfigure 2.1.3.RELEASE org.projectlombok lombok ${org.projectlombok.version} com.alibaba druid-spring-boot-starter 1.1.16 mysql mysql-connector-java 8.0.11 com.baomidou mybatis-plus-boot-starter 3.1.0 com.baomidou mybatis-plus-extension 3.1.0 com.baomidou mybatis-plus-generator 3.1.0 org.mybatis mybatis-typehandlers-jsr310 1.0.2 commons-lang commons-lang 2.6 org.mapstruct mapstruct-jdk8 ${org.mapstruct.version} com.google.guava guava 25.0-jre com.google.code.gson gson 2.8.2 com.alibaba fastjson 1.2.47 org.apache.shardingsphere sharding-jdbc-spring-boot-starter 4.0.0-RC1 com.alipay.sdk alipay-sdk-java 3.7.73.ALL com.github.tedzhdz wxpay-sdk 3.0.10 com.github.binarywang weixin-java-pay 3.4.0 com.qiniu qiniu-java-sdk 7.2.25 com.squareup.okhttp3 okhttp 3.9.1 org.apache.rocketmq rocketmq-spring-boot-starter 2.0.2 org.springframework.boot spring-boot-starter-data-redis 2.1.3.RELEASE ${project.name} src/main/resources true **/* src/main/java **/*.xml org.springframework.boot spring-boot-maven-plugin org.apache.maven.plugins maven-compiler-plugin 1.8 maven-resources-plugin utf-8 true 安装lombok插件
二、搭建商户平台模块:
搭建以下几个服务:
这里说明下三者的关系
提供外部访问接口(controller):shanjupay-merchant-application
暴露给shanjupay-merchant-application调用的service层:shanjupay-merchant-api
真正的去实现业务逻辑(serviceImpl+dao+entity):shanjupay-merchant-service
controller层通过dubbo去调用service的接口(service的实现就不会暴露)
数据库导入
搭建上面的三个服务:
1、shanjupay-merchant-application
pom文件:
shanjupay com.shanjupay 1.0-SNAPSHOT 4.0.0 shanjupay-merchant-application com.aliyun aliyun-java-sdk-core 4.5.3 com.squareup.okhttp3 okhttp com.shanjupay shanjupay-common 1.0-SNAPSHOT com.shanjupay shanjupay-merchant-api 1.0-SNAPSHOT com.alibaba.cloud spring-cloud-starter-alibaba-config com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloud spring-cloud-starter-dubbo org.springframework.boot spring-boot-starter-web org.springframework.boot spring‐boot‐startee org.springframework.boot spring-boot-starter-logging org.springframework.boot spring‐boot‐starter‐log4j2 org.springframework.boot spring‐boot‐configuration‐processor org.springframework.boot spring‐boot‐starter‐actuator org.springframework.boot spring‐boot‐starter‐test bootstrap.yml文件(bootstrap.yml配置比application.yml先加载)
server: port: 57010 #启动端口 命令行注入 max-http-header-size: 100KB nacos: server: addr: 127.0.0.1:8848 spring: application: name: merchant-application main: allow-bean-definition-overriding: true # Spring Boot 2.1 需要设定 cloud: nacos: discovery: server-addr: ${nacos.server.addr} #调用上面的nacos地址 namespace: eeaa8bff-98c3-4222-ae95-dd92275ec758 #命名空间 cluster-name: DEFAULT config: server-addr: ${nacos.server.addr} # 配置中心地址 file-extension: yaml namespace: eeaa8bff-98c3-4222-ae95-dd92275ec758 # 命令行注入 group: SHANJUPAY_GROUP # 聚合支付业务组 ext-config: - refresh: true data-id: spring-boot-http.yaml # spring boot http配置 group: COMMON_GROUP # 通用配置组 #SpringMVC上传文件配置 servlet: multipart: #默认支持文件上传. enabled: true #支持文件写入磁盘. file-size-threshold: 0 # 上传文件的临时目录 location: # 最大支持文件大小 max-file-size: 1MB # 最大支持请求大小 max-request-size: 30MB dubbo: scan: # dubbo 服务扫描基准包 base-packages: com.shanjupay protocol: # dubbo 协议 name: dubbo port: 20891 registry: address: nacos://127.0.0.1:8848 application: qos: port: 22310 # dubbo qos端口配置 命令行注入 consumer: check: false timeout: 3000 retries: -1 logging: config: classpath: log4j2.xml #日志记录地址
在Nacos中添加spring-boot-http.yaml配置,Group:COMMON_GROUP 这里统一使用dev命名空间
在resources目录下添加log4j2配置文件:log4j2.xml log4j2是log4j的改进版本,性能比log4j要高,通常日志配置文件在开发可以调整日志级别,输出详细的日志 来跟踪程序的执行。
${project.name} logs [${project.name}][${env:SERVER_PORT}] %date{YYYY-MM-dd HH:mm:ss,SSS} %highlight{%level} [%thread][%file:%line] - %msg%n%throwable 2、shanjupay-merchant (里面包含service、api两个模块)
2.1、shanjupay-merchant-api模块:
pom文件:
shanjupay-merchant com.shanjupay 1.0-SNAPSHOT 4.0.0 shanjupay-merchant-api com.shanjupay shanjupay-common 1.0-SNAPSHOT 2.2、service模块
pom文件:
shanjupay-merchant com.shanjupay 1.0-SNAPSHOT 4.0.0 shanjupay-merchant-service com.shanjupay shanjupay-merchant-api 1.0-SNAPSHOT com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloud spring-cloud-starter-dubbo org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-logging org.springframework.boot spring-boot-starter-jdbc com.baomidou mybatis-plus-boot-starter com.baomidou mybatis-plus-generator org.mybatis mybatis-typehandlers-jsr310 com.alibaba druid-spring-boot-starter mysql mysql-connector-java org.springframework.boot spring-boot-starter-log4j2 org.springframework.boot spring-boot-configuration-processor true org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-test test org.apache.commons commons-pool2 org.mapstruct mapstruct-jdk8 org.mapstruct mapstruct-processor ${org.mapstruct.version} org.projectlombok lombok ${org.projectlombok.version} 加上上面的log4j2.xml
bootstrap.yml文件:
server: port: 56040 #启动端口 命令行注入 max-http-header-size: 100KB nacos: server: addr: 127.0.0.1:8848 spring: application: name: merchant-service main: allow-bean-definition-overriding: true # Spring Boot 2.1 需要设定 cloud: nacos: discovery: server-addr: ${nacos.server.addr} namespace: eeaa8bff-98c3-4222-ae95-dd92275ec758 cluster-name: DEFAULT config: server-addr: ${nacos.server.addr} # 配置中心地址 file-extension: yaml namespace: eeaa8bff-98c3-4222-ae95-dd92275ec758 # 命令行注入 group: SHANJUPAY_GROUP # 聚合支付业务组 ext-config: - refresh: true data-id: spring-boot-http.yaml # spring boot http配置 group: COMMON_GROUP # 通用配置组 - refresh: true data-id: spring-boot-starter-druid.yaml # spring boot starter druid配置 group: COMMON_GROUP # 通用配置组 - refresh: true data-id: spring-boot-mybatis-plus.yaml # spring boot mybatisplus配置 group: COMMON_GROUP # 通用配置组 #SpringMVC上传文件配置 servlet: multipart: #默认支持文件上传. enabled: true #支持文件写入磁盘. file-size-threshold: 0 # 上传文件的临时目录 location: # 最大支持文件大小 max-file-size: 1MB # 最大支持请求大小 max-request-size: 30MB dubbo: scan: # dubbo 服务扫描基准包 base-packages: com.shanjupay protocol: # dubbo 协议 name: dubbo port: 20890 registry: address: nacos://127.0.0.1:8848 application: qos: port: 22240 # dubbo qos端口配置 命令行注入 consumer: check: false timeout: 3000 retries: -1 logging: config: classpath: log4j2.xml
生成对应的entity、mapper
在nacos中新建连接池Druid配置:spring-boot-starter-druid.yaml,Group为:COMMON_GROUP
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver‐class‐name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/shanjupay_merchant_service?useSSL=false&useUnicode=true&serverTimezone=GMT%2B8 username: root password: 123456 druid: initial‐size: 5 min‐idle: 5 max‐active: 20 max‐wait: 60000 time‐between‐eviction‐runs‐millis: 60000 min‐evictable‐idle‐time‐millis: 300000 validation‐query: SELECT 1 FROM DUAL test‐while‐idle: true test‐on‐borrow: true test‐on‐return: false pool‐prepared‐statements: true max‐pool‐prepared‐statement‐per‐connection‐size: 20 filter: stat: slow‐sql‐millis: 1 log‐slow‐sql: true filters: config,stat,wall,log4j2 web‐stat‐filter: enabled: true url‐pattern: /* exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" session‐stat‐enable: false session‐stat‐max‐count: 1000 principal‐cookie‐name: admin principal‐session‐name: admin profile‐enable: true stat‐view‐servlet: enabled: true url‐pattern: /druid/* allow: 127.0.0.1,192.168.163.1 deny: 192.168.211.1 reset‐enable: false login‐password: admin login‐username: admin aop‐patterns: com.shanjupay.*.service.*
在nacos中添加配置:spring-boot-mybatis-plus.yaml,Group为:COMMON_GROUP
mybatis‐plus: configuration: cache‐enabled: false map‐underscore‐to‐camel‐case: true global‐config: id‐type: 0 field‐strategy: 0 db‐column‐underline: true refresh‐mapper: true typeAliasesPackage: com.shanjupay.merchant.entity mapper‐locations: classpath:com/shanjupay/*/mapper/*.xml
添加分页和性能分析插件:MybatisPlusConfig
package com.shanjupay.merchant.config; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author Administrator * @version 1.0 **/ @Configuration @MapperScan("com.shanjupay.**.mapper") public class MybatisPlusConfig { /** * 分页插件,自动识别数据库类型 */ @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } /** * 启用性能分析插件 */ @Bean public PerformanceInterceptor performanceInterceptor(){ return new PerformanceInterceptor(); } }
编写一个简单例子测试流程
package com.shanjupay.merchant.api.dto; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import java.io.Serializable; @Data @ApiModel(value = "MerchantDTO", description = "商户信息") public class MerchantDTO implements Serializable { @ApiModelProperty("商户id") private Long id; @ApiModelProperty("企业名称") private String merchantName; @ApiModelProperty("企业编号") private Long merchantNo; @ApiModelProperty("企业地址") private String merchantAddress; @ApiModelProperty("行业类型") private String merchantType; @ApiModelProperty("营业执照") private String businessLicensesImg; @ApiModelProperty("法人身份证正面") private String idCardFrontImg; @ApiModelProperty("法人身份证反面") private String idCardAfterImg; @ApiModelProperty("联系人") private String username; @ApiModelProperty("密码") private String password; @ApiModelProperty("手机号,关联统一账号") private String mobile; @ApiModelProperty("联系人地址") private String contactsAddress; @ApiModelProperty("审核状态,0‐未申请,1‐已申请待审核,2‐审核通过,3‐审核拒绝") private String auditStatus; @ApiModelProperty("租户ID") private Long tenantId; }
成功
去nacos哪里看:
这里的项目主要是自己复习看,很多细节没说清楚
集成swgger2
在shanjupay-merchant-application工程的config包中添加一个Swagger配置类
package com.shanjupay.merchant.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 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; @Configuration @ConditionalOnProperty(prefix = "swagger",value = {"enable"},havingValue = "true") @EnableSwagger2 public class SwaggerConfiguration { @Bean public Docket buildDocket() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(buildApiInfo()) .select() // 要扫描的API(Controller)基础包 .apis(RequestHandlerSelectors.basePackage("com.shanjupay.merchant.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo buildApiInfo() { Contact contact = new Contact("开发者","",""); return new ApiInfoBuilder() .title("闪聚支付‐商户应用API文档") .description("") .contact(contact) .version("1.0.0").build(); } }
. 添加SpringMVC配置类:WebMvcConfig,让外部可直接访问Swagger文档
package com.shanjupay.merchant.config; import org.springframework.stereotype.Component; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Component public class WebMvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations("classpath:/static/"); registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); // registry.addResourceHandler("/test/dove/*.jpeg").addResourceLocations("file:D:\\"); } }
重启,在需要使用swagger2的服务加上@EnableSwagger2注解即可使用(Swagger2回去找启动了@EnableSwagger2且使用了注解的接口等)http://localhost:57010/swagger-ui.html
总结
1、该系统是自己为复习看,会有些模糊,细节没说清楚。
2、本文主要可以学习的知识点:
- 2.1:日志系统log4j2的搭建
- 2.2:对外restful(springcloud)对内rpc(dubbo)的基本使用(将controller、service、serviceImpl+entity+mapper层分不同服务)
- 2.3:集成swagger2
- 2.4:nacos的注册中心、配置中心基本使用
3、虽说该系统写了部分实战内容,但为了更加系统,还是会对日志搭建、swagger2集成、nacos配置中心的使用进行其他文章的分析。
日志搭建:https://mp.csdn.net/console/editor/html/108536129
swagger2集成:https://blog.csdn.net/qq_35599414/article/details/108539906nacos配置中心的具体使用:
4、本文中遇到的一些错误:
- nacos的配置信息要导出到mysql时要用mysql5.6以上版本
- 当模块的maven依赖加载不了时可以将
先删除后等加载好了再加上去 - serviceImpl如果要用dubbo调用时要用
org.apache.dubbo.config.annotation.Service进行@Service的注解注入,在获取service对象时用@Reference注解获取
- 使用mysql5.7时建立连接时要加上:useSSL=false&serverTimezone = GMT%2B8 不然会报错
- 好了似乎就这几个需要注意的
5、下面会对该系统进行进一步记录