Spring-boot:快速搭建微框架服务
前言:
Spring Boot 是为了简化 Spring 应用的创建、运行、调试、部署等而出现的,使用它可以做到专注于 Spring 应用的开发,而无需过多关注 XML 的配置。
简单来说,它提供了一堆依赖打包,并已经按照使用习惯解决了依赖问题 --- 习惯大于约定。
Spring Boot 默认使用 tomcat 作为服务器,使用 logback 提供日志记录。
Spring Boot 的主要优点:
- 为所有 Spring 开发者更快的入门
- 开箱即用,提供各种默认配置来简化项目配置
- 内嵌式容器简化 Web 项目
- 没有冗余代码生成和 XML 配置的要求
技术栈:
- Java 8
- Maven
- Spring-boot
- Mybatis
- Redis
- Lombok
- Swagger2
- Jenkins
- SonarQuber
1、使用Maven构建项目
1.1 通过 SPRING INITIALIZR 工具生产基础项目
通过访问:http://start.spring.io/ 快速创建 Spring-boot 的服务框架。
初始化相应信息后,下载压缩包。解压完成后,用 IDEA 打开项目,项目的目录结构:
总体流程:
- 访问:
http://start.spring.io/
- 选择构建工具
Maven Project
、Spring Boot 版本1.3.2
以及一些工程基本信息 - 点击
Generate Project
下载项目压缩包
解压项目包,并用 IDE 以Maven
项目导入,以IntelliJ IDEA 14
为例:
- 菜单中选择
File
–>New
–>Project from Existing Sources...
- 选择解压后的项目文件夹,点击
OK
- 点击
Import project from external model
并选择Maven
,点击Next
到底为止。 - 若你的环境有多个版本的 JDK,注意到选择
Java SDK
的时候请选择Java 7
以上的版本
1.2 导入 Spring-boot 相关依赖
项目初始化时,相关依赖如下:
- spring-boot-starters:核心模块,包括自动配置支持、日志和 YAML
- spring-boot-starter-test:测试模块,包括 JUnit、Hamcrest、Mockito
- spring-boot-devtools:用于设置热部署
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency><span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span>spring-boot-starter-test<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">></span>test<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 128, 0, 1)"><!--</span><span style="color: rgba(0, 128, 0, 1)">热部署</span><span style="color: rgba(0, 128, 0, 1)">--></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span>spring-boot-devtools<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">optional</span><span style="color: rgba(0, 0, 255, 1)">></span>true<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">optional</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span></pre>
这里我们需要引入 Web 模块,需要添加:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
1.3 启动项目
添加首页控制层:
@RestController public class IndexController {@RequestMapping(</span>"index"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String index() { </span><span style="color: rgba(0, 0, 255, 1)">return</span> "hello world!"<span style="color: rgba(0, 0, 0, 1)">; }
}
运行 DemoApplication 中的 main 方法,启动服务:
服务启动后, 访问 http://localhost:8080/index ,可以看到页面输出 Hello world!。
2、整合 Mybatis
2.1 项目依赖
- 引入连接 mysql 的必要依赖 mysql-connector-java
- 引入整合 MyBatis 的核心依赖 mybatis-spring-boot-starter
- 引入 tk.mybatis 依赖,实现对实体类的增删改查的代码
- 引入 pagerhelper 依赖,实现分页功能
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.43</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>1.1.3</version> </dependency> <!--pagehelper--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.1.2</version> </dependency>
2.2 项目配置
修改 resources 下的 application.properties 文件:
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#实体类扫描包
mybatis.type-aliases-package=com.jaycekon.demo.model
#Mapper.xml 文件扫描目录
mybatis.mapper-locations=classpath:mapper/*.xml
#驼峰命名
mybatis.configuration.mapUnderscoreToCamelCase=true
#tkmapper 工具类
mapper.mappers=com.Jaycekon.demo.util.MyMapper
mapper.not-empty=false
mapper.identity=MYSQL
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
2.3 单元测试
创建实体类,我们引入 Lombok 相关依赖,用于避免数据 Get Set 方法的重复创建:
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> <scope>provided</scope> </dependency>
实体类最终的代码如下:
@Data @NoArgsConstructor @AllArgsConstructor @Accessors(chain = true) public class User { private int id; private String username; private String idCard; private String phone; private String password; }
可以看出,在添加了 Lombok 之后,我们的 Java 实体类代码简洁了很多。
接下来,我们需要创建 UserMapper 数据库处理类。由于 MyMapper 已经帮我们实现了基本的 CRUD 操作,因此我们这里并不需要再重写操作,我可以先一个根据用户名查找的方法:
@Mapper public interface UserMapper extends MyMapper<User> {@Select(</span>"select * from user where username=#{username}"<span style="color: rgba(0, 0, 0, 1)">) User selectByName(String username);
}
MyMapper 类位于 util 目录下:
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> { }
这里需要注意,MyMapper 与我们的实体类 Mapper 不能放在同一个目录。
测试类:
@RunWith(SpringRunner.class) @SpringBootTest @MapperScan("com.Jaycekon.demo.mapper") public class UserMapperTest { @Autowired private UserMapper mapper;@Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testInset() { User user </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> User(1, "Jaycekon","1234","1234","123"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">int</span> i =<span style="color: rgba(0, 0, 0, 1)"> mapper.insert(user); Assert.assertNotEquals(</span>0<span style="color: rgba(0, 0, 0, 1)">, i); } @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testSelect(){ User user </span>= mapper.selectByName("Jaycekon"<span style="color: rgba(0, 0, 0, 1)">); Assert.assertNotEquals(</span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">,user); }
}
3、整合 Redis
3.1 相关依赖
Spring Boot 提供的数据访问框架 Spring Data Redis 基于 Jedis。可以通过引入 spring-boot-starter-redis
来配置依赖关系。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency>
3.2 Redis 配置
1、Spring-boot 连接单机版 Redis 的配置如下:
# REDIS (RedisProperties)
# Redis 数据库索引(默认为 0)
spring.redis.database=0
# Redis 服务器地址
spring.redis.host=localhost
# Redis 服务器连接端口
spring.redis.port=6379
# Redis 服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
2、Spring-boot 连接 Sentinel 哨兵集群配置:
# REDIS (RedisProperties)
# Redis 数据库索引(默认为 0)
spring.redis.database=0
# Redis 服务器地址
#spring.redis.host=localhost
# Redis 服务器连接端口
#spring.redis.port=6379
# Redis 服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
#哨兵监听 redis server 名称
spring.redis.sentinel.master=cn-test-master
#哨兵的配置列表
spring.redis.sentinel.nodes=localhost:26379,localhost:36379,localhost:46379
3.3 Redis 操作工具类
1、StringRedisTemplate 工具类
StringRedisTemplate 工具类可以解决字符串级别的 Redis 操作。在写好配置后,可以直接通过 Autowried 就可以注入对象。
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(Application.class) public class ApplicationTests { @Autowired private StringRedisTemplate stringRedisTemplate; @Test public void test() throws Exception { // 保存字符串 stringRedisTemplate.opsForValue().set("aaa", "111"); Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa"));} }
2、RedisTemplate<Object,Object> 工具类
可以处理大部分的序列化操作,在这里我封装了一个简化 Redis 工具类,后续可以继续优化。
@Component public class RedisComponent { @Autowired //操作字符串的 template,StringRedisTemplate 是 RedisTemplate 的一个子集 private StringRedisTemplate stringRedisTemplate;</span><span style="color: rgba(0, 0, 255, 1)">private</span> Logger logger = LoggerFactory.getLogger(RedisComponent.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); @Autowired </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> RedisTemplate,可以进行所有的操作</span> <span style="color: rgba(0, 0, 255, 1)">private</span> RedisTemplate<Object, Object><span style="color: rgba(0, 0, 0, 1)"> redisTemplate; </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> set(String key, String value) { ValueOperations</span><String, String> ops = <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.stringRedisTemplate.opsForValue(); </span><span style="color: rgba(0, 0, 255, 1)">boolean</span> bExistent = <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.stringRedisTemplate.hasKey(key); </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (bExistent) { logger.info(</span>"this key is bExistent!"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { ops.set(key, value); } } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String get(String key) { </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.stringRedisTemplate.opsForValue().get(key); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> del(String key) { </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.stringRedisTemplate.delete(key); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> sentinelSet(String key, Object object) { redisTemplate.opsForValue().set(key, JSON.toJSONString(object)); } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String sentinelGet(String key) { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> String.valueOf(redisTemplate.opsForValue().get(key)); }
}
4、整合 Swagger2
4.1 添加 Swagger2 依赖:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
4.2 创建 Swagger2 配置类:
在 Application.java 同级创建一个 Swagger2 的配置类:
@Configuration @EnableSwagger2 public class Swagger2 {@Bean </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Docket webApi() { </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Docket(DocumentationType.SWAGGER_2) .groupName(</span>"DemoAPI接口文档"<span style="color: rgba(0, 0, 0, 1)">) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage(</span>"com.Jaycekon.demo.controller"<span style="color: rgba(0, 0, 0, 1)">)) .paths(PathSelectors.any()).build(); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> swagger2使用说明: @Api:用在类上,说明该类的作用 @ApiOperation:用在方法上,说明方法的作用 @ApiIgnore:使用该注解忽略这个API @ApiImplicitParams:用在方法上包含一组参数说明 @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面 paramType:参数放在哪个地方 header-->请求参数的获取:@RequestHeader query-->请求参数的获取:@RequestParam path(用于restful接口)-->请求参数的获取:@PathVariable body(不常用) form(不常用) name:参数名 dataType:参数类型 required:参数是否必须传 value:参数的意思 defaultValue:参数的默认值 @ApiResponses:用于表示一组响应 @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息 code:数字,例如400 message:信息,例如"请求参数没填好" response:抛出异常的类 @ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候) @ApiModelProperty:描述一个model的属性 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> ApiInfo apiInfo() { </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ApiInfoBuilder() .title(</span>"Demo使用Swagger2构建RESTful APIs"<span style="color: rgba(0, 0, 0, 1)">) .description(</span>"微信打卡服务"<span style="color: rgba(0, 0, 0, 1)">) .contact(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Contact("Jaycekon", "http://petstore.swagger.io/v2/swagger.json", "jaycekon@163.com"<span style="color: rgba(0, 0, 0, 1)">)) .version(</span>"1.0"<span style="color: rgba(0, 0, 0, 1)">) .build(); }
}
4.3 在需要生成 Api 的接口添加注解:
@Api(tags = "测试用例") @RestController @RequestMapping(value="/users") // 通过这里配置使下面的映射都在 /users 下,可去除 public class UserController {@ApiOperation(value</span>="获取用户列表", notes=""<span style="color: rgba(0, 0, 0, 1)">) @RequestMapping(value</span>={""}, method=<span style="color: rgba(0, 0, 0, 1)"> RequestMethod.GET) </span><span style="color: rgba(0, 0, 255, 1)">public</span> List<User><span style="color: rgba(0, 0, 0, 1)"> getUserList() { </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">(); } @ApiOperation(value</span>="创建用户", notes="根据User对象创建用户"<span style="color: rgba(0, 0, 0, 1)">) @ApiImplicitParam(name </span>= "user", value = "用户详细实体user", required = <span style="color: rgba(0, 0, 255, 1)">true</span>, dataType = "User"<span style="color: rgba(0, 0, 0, 1)">) @RequestMapping(value</span>="", method=<span style="color: rgba(0, 0, 0, 1)">RequestMethod.POST) </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String postUser(@RequestBody User user) { </span><span style="color: rgba(0, 0, 255, 1)">return</span> "success"<span style="color: rgba(0, 0, 0, 1)">; } @ApiOperation(value</span>="获取用户详细信息", notes="根据url的id来获取用户详细信息"<span style="color: rgba(0, 0, 0, 1)">) @ApiImplicitParam(name </span>= "id", value = "用户ID", required = <span style="color: rgba(0, 0, 255, 1)">true</span>, dataType = "Long"<span style="color: rgba(0, 0, 0, 1)">) @RequestMapping(value</span>="/{id}", method=<span style="color: rgba(0, 0, 0, 1)">RequestMethod.GET) </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> User getUser(@PathVariable Long id) { </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> User(); } @ApiOperation(value</span>="更新用户详细信息", notes="根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息"<span style="color: rgba(0, 0, 0, 1)">) @ApiImplicitParams({ @ApiImplicitParam(name </span>= "id", value = "用户ID", required = <span style="color: rgba(0, 0, 255, 1)">true</span>, dataType = "Long"<span style="color: rgba(0, 0, 0, 1)">), @ApiImplicitParam(name </span>= "user", value = "用户详细实体user", required = <span style="color: rgba(0, 0, 255, 1)">true</span>, dataType = "User"<span style="color: rgba(0, 0, 0, 1)">) }) @RequestMapping(value</span>="/{id}", method=<span style="color: rgba(0, 0, 0, 1)">RequestMethod.PUT) </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String putUser(@PathVariable Long id, @RequestBody User user) { </span><span style="color: rgba(0, 0, 255, 1)">return</span> "success"<span style="color: rgba(0, 0, 0, 1)">; } @ApiOperation(value</span>="删除用户", notes="根据url的id来指定删除对象"<span style="color: rgba(0, 0, 0, 1)">) @ApiImplicitParam(name </span>= "id", value = "用户ID", required = <span style="color: rgba(0, 0, 255, 1)">true</span>, dataType = "Long"<span style="color: rgba(0, 0, 0, 1)">) @RequestMapping(value</span>="/{id}", method=<span style="color: rgba(0, 0, 0, 1)">RequestMethod.DELETE) </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String deleteUser(@PathVariable Long id) { </span><span style="color: rgba(0, 0, 255, 1)">return</span> "success"<span style="color: rgba(0, 0, 0, 1)">; }
}
完成上述代码添加上,启动 Spring Boot 程序,访问:http://localhost:8080/swagger-ui.html
。就能看到前文所展示的 RESTful API 的页面。我们可以再点开具体的 API 请求,以 POST 类型的 /users 请求为例,可找到上述代码中我们配置的 Notes 信息以及参数 user 的描述信息,如下图所示。
4、接入 Jenkins&SonarQube
项目框架搭建好后,我们可以通 Jenkins 进行项目的自动发版,以及 SonarQube 进行代码质量检测。在接入钱,我们需要将项目打包成 war 包,需要进行以下修改:
1、修改项目打包类型:
<groupId>com.Jaycekon</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging>
2、修改 Application.java 文件:
@SpringBootApplication public class DemoApplication extends SpringBootServletInitializer {@Override </span><span style="color: rgba(0, 0, 255, 1)">protected</span><span style="color: rgba(0, 0, 0, 1)"> SpringApplicationBuilder configure(SpringApplicationBuilder application) { </span><span style="color: rgba(0, 0, 255, 1)">return</span> application.sources(DemoApplication.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { SpringApplication.run(DemoApplication.</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">, args); }
}
在我的上一篇博客,哆啦 A 梦的传送门,已经讲解了一些基本配置方法,这里为大家讲解一下,接入 SonarQube 进行代码质量检测的配置(需要本地安装 SonarQube 服务)。
首先需要在 MetaData 中,加入 SonarQube 的项目名(新建的命名):
然后在 Post Steps 中选择添加 Execute SonarQube Scanner:
在配置好这两项后,Jenkins 在编译文件时,就会执行 SonarQube 代码质量检测。
最后,我们可以设置项目在编译完后,执行 shell 脚本,进行项目的自动发版:
项目编译完后,会找到项目下的 playbook,执行里面的脚本,将我们的项目部署到设定的服务器中。
总结 :
本篇文章为大家带来了 Spring-boot 的架构搭建,主要使用到了目前较为流行的技术。
源码地址:https://github.com/jaycekon/SpringBootDemo