架构:第四章:微服务架构下的分布式架构之搭建环境
下面是我要写的一个简单的分布式架构:SpringCloud+SpringBoot+SpringMVC+MyBatis+MySQL
架构图:
准备工作:
技术选型:
使用SpringMVC进行表述层开发 使用MyBatis进行持久化层开发 使用Spring进行组件整合并管理声明式事务 使用Maven作为依赖管理和构建管理工具 使用BootStrap前端样式框架生成页面效果 使用MD5加密算法对用户密码进行加密 使用SpringMVC异常映射机制进行了异常信息的统一管理 使用MyBatis的PageHelper实现持久化层分页 使用jQuery的Pagination插件在页面上显示分页页码 使用BootStrap的模态框组件显示弹层窗口显示表格或表单等等 使用layer弹层组件显示提示消息 使用jQuery的on()函数给动态生成的元素绑定事件响应函数 使用zTree框架在页面上显示Permission数据的树形结构 通过在数据库中维护父节点id实现Permission数据的树形结构 通过动态查询每个用户已分配的许可菜单实现细粒度权限控制 使用SpringBoot开发各个具体业务模块的微服务工程 使用SpringCloud的Eureka服务作为微服务的注册中心 使用SpringCloud的Feign封装微服务信息,目的是实现像调用本地方法一样调用远程方法 使用RestTemplate将二进制文件数据上传到Provider 使用RestTemplate将二进制文件数据下载到Consumer 使用Activiti作为流程管理框架实现实名认证流程 使用FastDFS作为用户上传文件的远程分布式文件服务器 使用James邮件服务器发送邮件 使用PostMan对Provider提供的服务进行了测试 使用Freemarker作为SpringBoot环境下的视图技术 |
创建各个项目工程:
创建父工程:CrowdFundingParent
创建子工程:ConsumerPortal
创建子工程:ConsumerManager
其他工程都这样创建
子工程的packaging都是jar,所以可以直接finish
在CrowdFundingParent父工程的pom.xml中创建依赖管理和构建管理
<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>
<groupId>com</groupId>
<artifactId>CrowdFundingParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>RegisterCenterEureka</module>
<module>CommonBean</module>
<module>CommonUtil</module>
<module>CommonFeign</module>
<module>ProviderDatabase</module>
<module>ProviderActiviti</module>
<module>ConsumerPortal</module>
<module>ConsumerManager</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.12.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- Activiti场景启动器 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>5.21.0</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
在ConsumerPortal子工程的pom.xml中创建依赖管理和构建管理
<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</groupId>
<artifactId>CrowdFundingParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ConsumerPortal</artifactId>
<dependencies>
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>com</groupId>
<artifactId>CommonBean</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com</groupId>
<artifactId>CommonFeign</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com</groupId>
<artifactId>CommonUtil</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
jar包出错
网上解决方案 如果还有问题可能是maven环境配置有问题,或者是父工程没有加版本号
在ConsumerManager子工程的pom.xml中创建依赖管理和构建管理
<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</groupId>
<artifactId>CrowdFundingParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ConsumerManager</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>com</groupId>
<artifactId>CommonBean</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com</groupId>
<artifactId>CommonFeign</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com</groupId>
<artifactId>CommonUtil</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
在ProviderActiviti子工程的pom.xml中创建依赖管理和构建管理
<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</groupId>
<artifactId>CrowdFundingParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ProviderActiviti</artifactId>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- eureka-server客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>com</groupId>
<artifactId>CommonBean</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com</groupId>
<artifactId>CommonFeign</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com</groupId>
<artifactId>CommonUtil</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Activiti场景启动器 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
</dependency>
</dependencies>
</project>
在ProviderDatabase子工程的pom.xml中创建依赖管理和构建管理
<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</groupId>
<artifactId>CrowdFundingParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ProviderDatabase</artifactId>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- eureka-server客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>com</groupId>
<artifactId>CommonBean</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com</groupId>
<artifactId>CommonFeign</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com</groupId>
<artifactId>CommonUtil</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
在RegisterCenterEureka子工程的pom.xml中创建依赖管理和构建管理
<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</groupId>
<artifactId>CrowdFundingParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>RegisterCenterEureka</artifactId>
<dependencies>
<!-- eureka-server服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
在CommonFeign子工程的pom.xml中创建依赖管理和构建管理
<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</groupId>
<artifactId>CrowdFundingParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>CommonFeign</artifactId>
<dependencies>
<!-- Feign的场景启动器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>com</groupId>
<artifactId>CommonBean</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
其他工程无依赖管理和构建管理,后期如果需要在加即可
创建各工程主启动类
RegisterCenterEureka
@EnableEurekaServer
@SpringBootApplication
ProviderDatabase
@EnableEurekaClient
@MapperScan("com.mapper")
@SpringBootApplication
ProviderActiviti
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
ConsumerPortal
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
ConsumerManager
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
例图:
创建各工程yml文件
RegisterCenterEureka
server:
port: 84
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:84/eureka
ProviderDatabase
server:
port: 83
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml
mapper-locations:
- classpath:mybatis/mapper/*Mapper.xml
spring:
application:
name: ProviderDatabase
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/atcrowdfunding?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8
username: root
password: root
dbcp2:
min-idle: 5
initial-size: 5
max-total: 5
max-wait-millis: 200
eureka:
client:
service-url:
defaultZone: http://localhost:84/eureka
创建mybatis目录
创建mybatis.cfg.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>
<settings>
<setting name="cacheEnabled" value="true" />
</settings>
</configuration>
ProviderActiviti
server:
port: 82
spring:
application:
name: ProviderActiviti
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/atcrowdfunding?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8
username: root
password: root
dbcp2:
min-idle: 5
initial-size: 5
max-total: 5
max-wait-millis: 200
eureka:
client:
service-url:
defaultZone: http://localhost:84/eureka
ConsumerPortal
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://localhost:84/eureka
spring:
freemarker:
template-loader-path: classpath:/templates/
suffix: .page
crowdfunding.tracker.config.location: classpath:/tracker.conf
创建templates目录
创建tracker.conf
tracker_server=192.168.0.100:22122
ConsumerManager
server:
port: 81
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://localhost:84/eureka
spring:
freemarker:
template-loader-path: classpath:/templates/
suffix: .page
创建templates目录
现在写一个首页显示功能测试一下
显示首页:使用Freemarker作为SpringBoot环境下的视图技术,创建一个index.page,将前端页面摸板的index.html复制j进去
修改页面
修改后
在路径前加/表示在类路径下
修改后
添加静态资源
将下面的复制
在类路径下创建static文件夹,存放静态资源的文件目录必须是static
创建ProtalHandler.java
@Controller
public class PortalHandler {
@RequestMapping("/main")
public String toIndexPage() {
return "index";
}
}
启动工程
先启动注册中心RegisterCenterEureka
再启动子项目ConsumerPortal
在浏览器中http://localhost:80/main默认是80端口,所以也可以直接http://localhost/main
运行的项目yml配置为80所以浏览器使用的端口为80
这个时候页面就出来了。
数据库中创建库和表
CREATE DATABASE atcrowdfunding;
USE atcrowdfunding;
CREATE TABLE `t_member` (
`member_id` INT(11) NOT NULL AUTO_INCREMENT,
`login_acc` VARCHAR(100) DEFAULT NULL,
`login_pwd` VARCHAR(100) DEFAULT NULL,
`nick_name` VARCHAR(100) DEFAULT NULL,
`real_name` VARCHAR(100) DEFAULT NULL,
`card_num` VARCHAR(100) DEFAULT NULL,
`phone_num` VARCHAR(20) DEFAULT NULL,
`email_addr` VARCHAR(100) DEFAULT NULL,
`auth_status` TINYINT(4) DEFAULT NULL,
`acc_type` TINYINT(4) DEFAULT NULL,
`company` TINYINT(4) DEFAULT NULL,
PRIMARY KEY (`member_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
注意:这里只是简单的搭建,要准备的工作就在这,这里把我自己初始的项目分享出来CrowdFundingParent,子工程可以从父工程中导出来。
大家可以用最初的搭建好的项目进行功能编写。
我编写了部分功能:门户:登录与注册,登录后实名认证流程,管理:业务审核中的实名认证审核,业务管理中的流程管理
完成的项目分享CrowdFundingParent。有时间再编写其他功能。