一、创建父项目
1.只留下pom.xml,并删除 <description> 标签下面内容
2.添加 <packaging>pom</packaging> 和 <modules>子项目</modules>
3.在每一个子项目pom.xml中的<parent>标签添加父项目依赖,并改变关系路径 ../
2.添加 <packaging>pom</packaging> 和 <modules>子项目</modules>
3.在每一个子项目pom.xml中的<parent>标签添加父项目依赖,并改变关系路径 ../
二、创建eureka注册中心 (勾选 Eureka Server )
1.在.properties文件中添加:
server.port=8000
#单体模式,不把自己注册到其他注册中心
eureka.client.register-with-eureka=false
server.port=8000
#单体模式,不把自己注册到其他注册中心
eureka.client.register-with-eureka=false
#单体模式,不从其他注册中心获取注册信息
eureka.client.fetch-registry=false
#注册中心服务地址
eureka.client.service-url.defaultZone=http://localhost:8000/eureka
2.在主程序上添加 @EnableEurekaServer 注解
三、创建common公用项目 (勾选 lombok )
1.移除 resources 和 test 文件夹
2.删除主函数
3.pom.xml 中去除无关依赖test和 <build>
2.删除主函数
3.pom.xml 中去除无关依赖test和 <build>
4.创建实体类和一些公用工具类
四、创建server服务项目 (勾选 Spring Web、MyBatis Framework、MySQL Driver、Spring Data Redis、Eureka Discovery Client )
1.在.properties文件中添加:
server.port=8081
server.port=8081
#redis相关配置
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.host=localhost
spring.redis.port=6379
#mysql相关配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名?characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名?characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=admin
#实体类的包路径
mybatis.type-aliases-package=cn.kgc.common.entity
mybatis.type-aliases-package=cn.kgc.common.entity
#日志监测位置
logging.level.cn.kgc.server.mapper=debug
logging.level.cn.kgc.server.mapper=debug
spring.jackson.time-zone=GMT+8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
#当前服务在注册中心的名称,顺便将服务注册到eureka服务注册中心去
spring.application.name=服务名称
spring.application.name=服务名称
#服务注册中心的地址(就是eureka-center项目中的配置)
eureka.client.service-url.defaultZone=http://localhost:8000/eureka
2.在pom.xml中添加依赖:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
eureka.client.service-url.defaultZone=http://localhost:8000/eureka
2.在pom.xml中添加依赖:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
3.在pom.xml的 <build> 中添加:
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
4.在主函数上添加 @EnableEurekaClient 和 @EnableSwagger2 注解
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
3.在pom.xml的 <build> 中添加:
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
4.在主函数上添加 @EnableEurekaClient 和 @EnableSwagger2 注解
5.包体结构:
1. mapper层:由Interface和xml文件组成。在interface上面要添加 @Mapper注解
2. service层:由Interface和类Class组成。在实现类Class上面要添加 @Service注解
3. controller层:由类Class组成。在类Class上面要添加 @RestController注解
注意:在service层调用mapper层或在controller层调用service层时,用 @Resource注解 或 @Autowired注解
6.编写config.SwaggerConfig类(名字随意),写下:
@Configuration
public class SwaggerConfig {
@Configuration
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cn.kgc.service.controller"))
.paths(PathSelectors.any())
.build();
}
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cn.kgc.service.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("xxxAPI")
.description("提供xxx的增删改查服务。")
.termsOfServiceUrl("http://blog.csdn.net/saytime")
.version("1.0")
.build();
}
}
7、@ApiOperation(value = "") 解释这个接口用途或作用
@ApiImplicitParam(name = "aid",value = "账号主键编号",required = true,dataType ="Integer",paramType = "path")
name:参数名称,value:什么值,required:是否是必须的(true/false),dataType:参数的数据类型,
paramType:参数从哪取值(path:请求路径上、body:参数的实体类、query:参数中的其他)
@ApiImplicitParams({
@ApiImplicitParam() ... 多个参数(这个),一个参数(上面那个)
})
8、一顿打包运行servcie,然后在浏览其中输入localhost:8081/swagger-ui.html 访问
return new ApiInfoBuilder()
.title("xxxAPI")
.description("提供xxx的增删改查服务。")
.termsOfServiceUrl("http://blog.csdn.net/saytime")
.version("1.0")
.build();
}
}
7、@ApiOperation(value = "") 解释这个接口用途或作用
@ApiImplicitParam(name = "aid",value = "账号主键编号",required = true,dataType ="Integer",paramType = "path")
name:参数名称,value:什么值,required:是否是必须的(true/false),dataType:参数的数据类型,
paramType:参数从哪取值(path:请求路径上、body:参数的实体类、query:参数中的其他)
@ApiImplicitParams({
@ApiImplicitParam() ... 多个参数(这个),一个参数(上面那个)
})
8、一顿打包运行servcie,然后在浏览其中输入localhost:8081/swagger-ui.html 访问
五、创建client客户端 (勾选 Spring Web、Apache Freemarker、Eureka Discovery Client、OpenFeign )
1.在.properties文件中添加:
server.port=8080
server.port=8080
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.suffix=.ftl
spring.freemarker.content-type=text/html
spring.freemarker.charset=UTF-8
spring.freemarker.suffix=.ftl
spring.freemarker.content-type=text/html
#设置时间输入输出格式
spring.jackson.time-zone=GMT+8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
#当前服务在注册中心的名称,顺便将服务注册到eureka服务注册中心去
spring.application.name=服务名称
spring.application.name=服务名称
#服务注册中心的地址(就是eureka-center项目中的配置)
eureka.client.service-url.defaultZone=http://localhost:8000/eureka
eureka.client.service-url.defaultZone=http://localhost:8000/eureka
2.在 pom.xml 中<build> 中 添加 四.3 内容
3.在主程序上打 @EnableEurekaClient 和 @EnableFeignClients 注解
3.在主程序上打 @EnableEurekaClient 和 @EnableFeignClients 注解
4.包体结构:
1. service层:由Interface组成,简单理解为将module-server中的controller粘贴过来。在Interface上添加 @Service注解 和 @FeignClient("选择的服务")注解
2. controller层:由类Class组成。在类Class上面要添加 @Controller注解。方法上添加 @RequestMapping("")注解,如果是Ajax请求还用添加 @ResponseBody注解
注意:情况复杂的时候可能还会添加其他层。
六、常用技巧
1.同源策略解决方案#在服务端controller上写(允许跨源请求):
@CrossOrigin(value = "*", maxAge = 3600)
2、Apache Freemarker 常用技巧
?c 可以解决数字格式含有逗号;
! 可以解决${字段}输出为空的报错
?date 可以设置时间格式
?string("yyyy-MM-dd HH:mm:ss") 可以设置时间格式
<#assign 变量=值 > 标签可以定义变量
<#if 判断条件 > 标签可以进行条件选择
<#list 集合 as 单个> 类似java的for循环,还可以 <#list ["苹果","香蕉","西瓜"] as 单个>
字段_index 可以取得下标
${(num?? && num=="${字段}")?string("selected","")} 类似java的三木运算符,?? 代表不为空
3、添加缓存
(1)在.properties文件中添加
spring.cache.type=redis
(2)在dao或service层加上注解
@Cacheable(value = "article", key = "#id") ,其中id为方法上的参数
(3)在当前主程序上打上注解
@EnableCaching
4、熔断器
@FeignClient(value = "服务名", fallback = ArticleDaoImpl.class)
(1)在.properties文件中添加
spring.cache.type=redis
(2)在dao或service层加上注解
@Cacheable(value = "article", key = "#id") ,其中id为方法上的参数
(3)在当前主程序上打上注解
@EnableCaching
4、熔断器
@FeignClient(value = "服务名", fallback = ArticleDaoImpl.class)
5、使用PageHelper进行分页
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
(1)mapper层返回值使用List<实体>,service层返回值使用PageInfo<实体>
(2)mapper层方法不写分页参数,service层写(curr,size)两个分页参数
(3)serviceImpl层,先写PageHelper.startPage(curr, size);
(4)使用mapper层的方法获得实体集合list
(5)将实体集合放入 PageInfo<实体> info = new PageInfo<>(list);
(6)返回info对象
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
(1)mapper层返回值使用List<实体>,service层返回值使用PageInfo<实体>
(2)mapper层方法不写分页参数,service层写(curr,size)两个分页参数
(3)serviceImpl层,先写PageHelper.startPage(curr, size);
(4)使用mapper层的方法获得实体集合list
(5)将实体集合放入 PageInfo<实体> info = new PageInfo<>(list);
(6)返回info对象