一、創建父項目
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對象