Spring Boot 快速搭建微服務(wù)框架詳細(xì)教程
前言:
SpringBoot是為了簡(jiǎn)化Spring應(yīng)用的創(chuàng)建、運(yùn)行、調(diào)試、部署等而出現(xiàn)的,使用它可以做到專(zhuān)注于Spring應(yīng)用的開(kāi)發(fā),而無(wú)需過(guò)多關(guān)注XML的配置。
簡(jiǎn)單來(lái)說(shuō),它提供了一堆依賴(lài)打包,并已經(jīng)按照使用習(xí)慣解決了依賴(lài)問(wèn)題---習(xí)慣大于約定。
Spring Boot默認(rèn)使用tomcat作為服務(wù)器,使用logback提供日志記錄。
Spring Boot的主要優(yōu)點(diǎn):
- 為所有Spring開(kāi)發(fā)者更快的入門(mén)
- 開(kāi)箱即用,提供各種默認(rèn)配置來(lái)簡(jiǎn)化項(xiàng)目配置
- 內(nèi)嵌式容器簡(jiǎn)化Web項(xiàng)目
- 沒(méi)有冗余代碼生成和XML配置的要求
技術(shù)棧:
- Java 8
- Maven
- Spring-boot
- Mybatis
- Redis
- Lombok
- Swagger2
- Jenkins
- SonarQuber
1、使用Maven構(gòu)建項(xiàng)目
1.1 通過(guò) SPRING INITIALIZR 工具生產(chǎn)基礎(chǔ)項(xiàng)目
通過(guò)訪問(wèn):http://start.spring.io/ 快速創(chuàng)建Spring-boot 的服務(wù)框架。
初始化相應(yīng)信息后,下載壓縮包。解壓完成后,用IDEA打開(kāi)項(xiàng)目,項(xiàng)目的目錄結(jié)構(gòu):
總體流程:
- 訪問(wèn):http://start.spring.io/
- 選擇構(gòu)建工具M(jìn)aven Project、Spring Boot版本1.3.2以及一些工程基本信息
- 點(diǎn)擊Generate Project下載項(xiàng)目壓縮包
解壓項(xiàng)目包,并用IDE以Maven項(xiàng)目導(dǎo)入,以IntelliJ IDEA 14為例:
- 菜單中選擇File–>New–>Project from Existing Sources...
- 選擇解壓后的項(xiàng)目文件夾,點(diǎn)擊OK
- 點(diǎn)擊Import project from external model并選擇Maven,點(diǎn)擊Next到底為止。
- 若你的環(huán)境有多個(gè)版本的JDK,注意到選擇Java SDK的時(shí)候請(qǐng)選擇Java 7以上的版本
1.2 導(dǎo)入Spring-boot 相關(guān)依賴(lài)
項(xiàng)目初始化時(shí),相關(guān)依賴(lài)如下:
- spring-boot-starters:核心模塊,包括自動(dòng)配置支持、日志和YAML
- spring-boot-starter-test:測(cè)試模塊,包括JUnit、Hamcrest、Mockito
- spring-boot-devtools:用于設(shè)置熱部署
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--熱部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
這里我們需要引入Web模塊,需要添加:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
1.3 啟動(dòng)項(xiàng)目
添加首頁(yè)控制層:
@RestController public class IndexController { @RequestMapping("index") public String index() { return "hello world!"; } }
運(yùn)行DemoApplication中的main方法,啟動(dòng)服務(wù):
服務(wù)啟動(dòng)后, 訪問(wèn) http://localhost:8080/index ,可以看到頁(yè)面輸出Hello world!。
2、整合Mybatis
2.1 項(xiàng)目依賴(lài)
- 引入連接mysql的必要依賴(lài)mysql-connector-java
- 引入整合MyBatis的核心依賴(lài)mybatis-spring-boot-starter
- 引入tk.mybatis 依賴(lài),實(shí)現(xiàn)對(duì)實(shí)體類(lèi)的增刪改查的代碼
- 引入pagerhelper 依賴(lài),實(shí)現(xiàn)分頁(yè)功能
<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 項(xiàng)目配置
修改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 #實(shí)體類(lèi)掃描包 mybatis.type-aliases-package=com.jaycekon.demo.model #Mapper.xml文件掃描目錄 mybatis.mapper-locations=classpath:mapper/*.xml #駝峰命名 mybatis.configuration.mapUnderscoreToCamelCase=true #tkmapper 工具類(lèi) 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 單元測(cè)試
創(chuàng)建實(shí)體類(lèi),我們引入Lombok相關(guān)依賴(lài),用于避免數(shù)據(jù)Get Set方法的重復(fù)創(chuàng)建:
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> <scope>provided</scope> </dependency>
實(shí)體類(lèi)最終的代碼如下:
@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 實(shí)體類(lèi)代碼簡(jiǎn)潔了很多。
接下來(lái),我們需要?jiǎng)?chuàng)建UserMapper 數(shù)據(jù)庫(kù)處理類(lèi)。由于MyMapper 已經(jīng)幫我們實(shí)現(xiàn)了基本的CRUD操作,因此我們這里并不需要再重寫(xiě)操作,我可以先一個(gè)根據(jù)用戶(hù)名查找的方法:
@Mapper public interface UserMapper extends MyMapper<User> { @Select("select * from user where username=#{username}") User selectByName(String username); } MyMapper 類(lèi)位于util 目錄下: public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> { }
這里需要注意,MyMapper 與我們的實(shí)體類(lèi)Mapper 不能放在同一個(gè)目錄。
測(cè)試類(lèi):
@RunWith(SpringRunner.class) @SpringBootTest @MapperScan("com.Jaycekon.demo.mapper") public class UserMapperTest { @Autowired private UserMapper mapper; @Test public void testInset() { User user = new User(1, "Jaycekon","1234","1234","123"); int i = mapper.insert(user); Assert.assertNotEquals(0, i); } @Test public void testSelect(){ User user = mapper.selectByName("Jaycekon"); Assert.assertNotEquals(null,user); } }
3、整合Redis
3.1 相關(guān)依賴(lài)
Spring Boot提供的數(shù)據(jù)訪問(wèn)框架Spring Data Redis基于Jedis??梢酝ㄟ^(guò)引入 spring-boot-starter-redis 來(lái)配置依賴(lài)關(guān)系。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency>
3.2 Redis 配置
1、Spring-boot 連接單機(jī)版Redis 的配置如下:
# REDIS (RedisProperties) # Redis數(shù)據(jù)庫(kù)索引(默認(rèn)為0) spring.redis.database=0 # Redis服務(wù)器地址 spring.redis.host=localhost # Redis服務(wù)器連接端口 spring.redis.port=6379 # Redis服務(wù)器連接密碼(默認(rèn)為空) spring.redis.password= # 連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制) spring.redis.pool.max-active=8 # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制) spring.redis.pool.max-wait=-1 # 連接池中的最大空閑連接 spring.redis.pool.max-idle=8 # 連接池中的最小空閑連接 spring.redis.pool.min-idle=0 # 連接超時(shí)時(shí)間(毫秒) spring.redis.timeout=0
2、Spriig-boot 連接Sentinel 哨兵集群配置:
# REDIS (RedisProperties) # Redis數(shù)據(jù)庫(kù)索引(默認(rèn)為0) spring.redis.database=0 # Redis服務(wù)器地址 #spring.redis.host=localhost # Redis服務(wù)器連接端口 #spring.redis.port=6379 # Redis服務(wù)器連接密碼(默認(rèn)為空) spring.redis.password= # 連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制) spring.redis.pool.max-active=8 # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制) spring.redis.pool.max-wait=-1 # 連接池中的最大空閑連接 spring.redis.pool.max-idle=8 # 連接池中的最小空閑連接 spring.redis.pool.min-idle=0 # 連接超時(shí)時(shí)間(毫秒) spring.redis.timeout=0 #哨兵監(jiān)聽(tīng)redis server名稱(chēng) spring.redis.sentinel.master=cn-test-master #哨兵的配置列表 spring.redis.sentinel.nodes=localhost:26379,localhost:36379,localhost:46379
3.3 Redis 操作工具類(lèi)
1、StringRedisTemplate 工具類(lèi)
StringRedisTemplate 工具類(lèi)可以解決字符串級(jí)別的Redis操作。在寫(xiě)好配置后,可以直接通過(guò)Autowried 就可以注入對(duì)象。
@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> 工具類(lèi)
可以處理大部分的序列化操作,在這里我封裝了一個(gè)簡(jiǎn)化Redis工具類(lèi),后續(xù)可以繼續(xù)優(yōu)化。
@Component public class RedisComponent { @Autowired //操作字符串的template,StringRedisTemplate是RedisTemplate的一個(gè)子集 private StringRedisTemplate stringRedisTemplate; private Logger logger = LoggerFactory.getLogger(RedisComponent.class); @Autowired // RedisTemplate,可以進(jìn)行所有的操作 private RedisTemplate<Object, Object> redisTemplate; public void set(String key, String value) { ValueOperations<String, String> ops = this.stringRedisTemplate.opsForValue(); boolean bExistent = this.stringRedisTemplate.hasKey(key); if (bExistent) { logger.info("this key is bExistent!"); } else { ops.set(key, value); } } public String get(String key) { return this.stringRedisTemplate.opsForValue().get(key); } public void del(String key) { this.stringRedisTemplate.delete(key); } public void sentinelSet(String key, Object object) { redisTemplate.opsForValue().set(key, JSON.toJSONString(object)); } public String sentinelGet(String key) { return String.valueOf(redisTemplate.opsForValue().get(key)); } }
4、整合Swagger2
4.1 添加Swagger2 依賴(lài):
<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 創(chuàng)建Swagger2 配置類(lèi):
在Application.java 同級(jí)創(chuàng)建一個(gè)Swagger2 的配置類(lèi):
@Configuration @EnableSwagger2 public class Swagger2 { @Bean public Docket webApi() { return new Docket(DocumentationType.SWAGGER_2) .groupName("DemoAPI接口文檔") .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.Jaycekon.demo.controller")) .paths(PathSelectors.any()).build(); } /** swagger2使用說(shuō)明: @Api:用在類(lèi)上,說(shuō)明該類(lèi)的作用 @ApiOperation:用在方法上,說(shuō)明方法的作用 @ApiIgnore:使用該注解忽略這個(gè)API @ApiImplicitParams:用在方法上包含一組參數(shù)說(shuō)明 @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一個(gè)請(qǐng)求參數(shù)的各個(gè)方面 paramType:參數(shù)放在哪個(gè)地方 header-->請(qǐng)求參數(shù)的獲?。篅RequestHeader query-->請(qǐng)求參數(shù)的獲?。篅RequestParam path(用于restful接口)-->請(qǐng)求參數(shù)的獲?。篅PathVariable body(不常用) form(不常用) name:參數(shù)名 dataType:參數(shù)類(lèi)型 required:參數(shù)是否必須傳 value:參數(shù)的意思 defaultValue:參數(shù)的默認(rèn)值 @ApiResponses:用于表示一組響應(yīng) @ApiResponse:用在@ApiResponses中,一般用于表達(dá)一個(gè)錯(cuò)誤的響應(yīng)信息 code:數(shù)字,例如400 message:信息,例如"請(qǐng)求參數(shù)沒(méi)填好" response:拋出異常的類(lèi) @ApiModel:描述一個(gè)Model的信息(這種一般用在post創(chuàng)建的時(shí)候,使用@RequestBody這樣的場(chǎng)景,請(qǐng)求參數(shù)無(wú)法使用@ApiImplicitParam注解進(jìn)行描述的時(shí)候) @ApiModelProperty:描述一個(gè)model的屬性 */ private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Demo使用Swagger2構(gòu)建RESTful APIs") .description("微信打卡服務(wù)") .contact(new Contact("Jaycekon", "http://petstore.swagger.io/v2/swagger.json", "jaycekon@163.com")) .version("1.0") .build(); } }
4.3 在需要生成Api 的接口添加注解:
@Api(tags = "測(cè)試用例") @RestController @RequestMapping(value="/users") // 通過(guò)這里配置使下面的映射都在/users下,可去除 public class UserController { @ApiOperation(value="獲取用戶(hù)列表", notes="") @RequestMapping(value={""}, method= RequestMethod.GET) public List<User> getUserList() { return new ArrayList<>(); } @ApiOperation(value="創(chuàng)建用戶(hù)", notes="根據(jù)User對(duì)象創(chuàng)建用戶(hù)") @ApiImplicitParam(name = "user", value = "用戶(hù)詳細(xì)實(shí)體user", required = true, dataType = "User") @RequestMapping(value="", method=RequestMethod.POST) public String postUser(@RequestBody User user) { return "success"; } @ApiOperation(value="獲取用戶(hù)詳細(xì)信息", notes="根據(jù)url的id來(lái)獲取用戶(hù)詳細(xì)信息") @ApiImplicitParam(name = "id", value = "用戶(hù)ID", required = true, dataType = "Long") @RequestMapping(value="/{id}", method=RequestMethod.GET) public User getUser(@PathVariable Long id) { return new User(); } @ApiOperation(value="更新用戶(hù)詳細(xì)信息", notes="根據(jù)url的id來(lái)指定更新對(duì)象,并根據(jù)傳過(guò)來(lái)的user信息來(lái)更新用戶(hù)詳細(xì)信息") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用戶(hù)ID", required = true, dataType = "Long"), @ApiImplicitParam(name = "user", value = "用戶(hù)詳細(xì)實(shí)體user", required = true, dataType = "User") }) @RequestMapping(value="/{id}", method=RequestMethod.PUT) public String putUser(@PathVariable Long id, @RequestBody User user) { return "success"; } @ApiOperation(value="刪除用戶(hù)", notes="根據(jù)url的id來(lái)指定刪除對(duì)象") @ApiImplicitParam(name = "id", value = "用戶(hù)ID", required = true, dataType = "Long") @RequestMapping(value="/{id}", method=RequestMethod.DELETE) public String deleteUser(@PathVariable Long id) { return "success"; } }
完成上述代碼添加上,啟動(dòng)Spring Boot程序,訪問(wèn):http://localhost:8080/swagger-ui.html
。
就能看到前文所展示的RESTful API的頁(yè)面。我們可以再點(diǎn)開(kāi)具體的API請(qǐng)求,以POST類(lèi)型的/users請(qǐng)求為例,可找到上述代碼中我們配置的Notes信息以及參數(shù)user的描述信息,如下圖所示。
4、接入Jenkins&SonarQube
項(xiàng)目框架搭建好后,我們可以通Jenkins 進(jìn)行項(xiàng)目的自動(dòng)發(fā)版,以及SonarQube 進(jìn)行代碼質(zhì)量檢測(cè)。在接入錢(qián),我們需要將項(xiàng)目打包成war包,需要進(jìn)行以下修改:
1、修改項(xiàng)目打包類(lèi)型:
<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 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(DemoApplication.class); } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
在我的上一篇博客,哆啦A夢(mèng)的傳送門(mén),已經(jīng)講解了一些基本配置方法,這里為大家講解一下,接入SonarQube 進(jìn)行代碼質(zhì)量檢測(cè)的配置(需要本地安裝SonarQube服務(wù))。
首先需要在MetaData 中,加入SonarQube 的項(xiàng)目名(新建的命名):
然后在Post Steps 中選擇添加 Execute SonarQube Scanner:
在配置好這兩項(xiàng)后,Jenkins 在編譯文件時(shí),就會(huì)執(zhí)行SonarQube 代碼質(zhì)量檢測(cè)。
最后,我們可以設(shè)置項(xiàng)目在編譯完后,執(zhí)行shell 腳本,進(jìn)行項(xiàng)目的自動(dòng)發(fā)版:
項(xiàng)目編譯完后,會(huì)找到項(xiàng)目下的playbook,執(zhí)行里面的腳本,將我們的項(xiàng)目部署到設(shè)定的服務(wù)器中。
源碼地址:https://github.com/jaycekon/SpringBootDemo
總結(jié) :
本篇文章為大家?guī)?lái)了Spring-boot 的架構(gòu)搭建,主要使用到了目前較為流行的技術(shù)。希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 關(guān)于SpringBoot3.x中spring.factories功能被移除的解決方案
- 淺析SpringBoot微服務(wù)中異步調(diào)用數(shù)據(jù)提交數(shù)據(jù)庫(kù)的問(wèn)題
- 使用Spring?Boot+gRPC構(gòu)建微服務(wù)并部署的案例詳解
- 教你在Spring Boot微服務(wù)中集成gRPC通訊的方法
- SpringBoot+Nacos+Kafka微服務(wù)流編排的簡(jiǎn)單實(shí)現(xiàn)
- SpringBoot+SpringCloud用戶(hù)信息微服務(wù)傳遞實(shí)現(xiàn)解析
- SpringBoot集成gRPC微服務(wù)工程搭建實(shí)踐的方法
- 分享Spring Boot 3.x微服務(wù)升級(jí)歷程
相關(guān)文章
全面了解java byte數(shù)組與文件讀寫(xiě)
下面小編就為大家?guī)?lái)一篇全面了解java byte數(shù)組與文件讀寫(xiě)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08Java源碼解析HashMap的tableSizeFor函數(shù)
今天小編就為大家分享一篇關(guān)于Java源碼解析HashMap的tableSizeFor函數(shù),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01Java String方法獲取字符出現(xiàn)次數(shù)及字符最大相同部分示例
這篇文章主要介紹了Java String方法獲取字符出現(xiàn)次數(shù)及字符最大相同部分,涉及java字符串的遍歷、比較、計(jì)算等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09MyBatis將查詢(xún)出的兩列數(shù)據(jù)裝配成鍵值對(duì)的操作方法
這篇文章主要介紹了MyBatis將查詢(xún)出的兩列數(shù)據(jù)裝配成鍵值對(duì)的操作代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08Java中獲取?List中最后一個(gè)元素3種方法以及實(shí)際應(yīng)用
這篇文章主要給大家介紹了關(guān)于Java中獲取?List中最后一個(gè)元素3種方法以及實(shí)際應(yīng)用的相關(guān)資料,由于List的索引是從0開(kāi)始的,所以最后一個(gè)元素的索引是List的大小減1,需要的朋友可以參考下2023-11-11