Spring Boot 快速搭建微服務(wù)框架詳細教程
前言:
SpringBoot是為了簡化Spring應(yīng)用的創(chuàng)建、運行、調(diào)試、部署等而出現(xiàn)的,使用它可以做到專注于Spring應(yīng)用的開發(fā),而無需過多關(guān)注XML的配置。
簡單來說,它提供了一堆依賴打包,并已經(jīng)按照使用習(xí)慣解決了依賴問題---習(xí)慣大于約定。
Spring Boot默認使用tomcat作為服務(wù)器,使用logback提供日志記錄。
Spring Boot的主要優(yōu)點:
- 為所有Spring開發(fā)者更快的入門
- 開箱即用,提供各種默認配置來簡化項目配置
- 內(nèi)嵌式容器簡化Web項目
- 沒有冗余代碼生成和XML配置的要求
技術(shù)棧:
- Java 8
- Maven
- Spring-boot
- Mybatis
- Redis
- Lombok
- Swagger2
- Jenkins
- SonarQuber
1、使用Maven構(gòu)建項目
1.1 通過 SPRING INITIALIZR 工具生產(chǎn)基礎(chǔ)項目
通過訪問:http://start.spring.io/ 快速創(chuàng)建Spring-boot 的服務(wù)框架。
初始化相應(yīng)信息后,下載壓縮包。解壓完成后,用IDEA打開項目,項目的目錄結(jié)構(gòu):
總體流程:
- 訪問:http://start.spring.io/
- 選擇構(gòu)建工具Maven Project、Spring Boot版本1.3.2以及一些工程基本信息
- 點擊Generate Project下載項目壓縮包
解壓項目包,并用IDE以Maven項目導(dǎo)入,以IntelliJ IDEA 14為例:
- 菜單中選擇File–>New–>Project from Existing Sources...
- 選擇解壓后的項目文件夾,點擊OK
- 點擊Import project from external model并選擇Maven,點擊Next到底為止。
- 若你的環(huán)境有多個版本的JDK,注意到選擇Java SDK的時候請選擇Java 7以上的版本
1.2 導(dǎo)入Spring-boot 相關(guān)依賴
項目初始化時,相關(guān)依賴如下:
- spring-boot-starters:核心模塊,包括自動配置支持、日志和YAML
- spring-boot-starter-test:測試模塊,包括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 啟動項目
添加首頁控制層:
@RestController public class IndexController { @RequestMapping("index") public String index() { return "hello world!"; } }
運行DemoApplication中的main方法,啟動服務(wù):
服務(wù)啟動后, 訪問 http://localhost:8080/index ,可以看到頁面輸出Hello world!。
2、整合Mybatis
2.1 項目依賴
- 引入連接mysql的必要依賴mysql-connector-java
- 引入整合MyBatis的核心依賴mybatis-spring-boot-starter
- 引入tk.mybatis 依賴,實現(xiàn)對實體類的增刪改查的代碼
- 引入pagerhelper 依賴,實現(xiàn)分頁功能
<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 單元測試
創(chuàng)建實體類,我們引入Lombok相關(guān)依賴,用于避免數(shù)據(jù)Get Set方法的重復(fù)創(chuàng)建:
<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 實體類代碼簡潔了很多。
接下來,我們需要創(chuàng)建UserMapper 數(shù)據(jù)庫處理類。由于MyMapper 已經(jīng)幫我們實現(xiàn)了基本的CRUD操作,因此我們這里并不需要再重寫操作,我可以先一個根據(jù)用戶名查找的方法:
@Mapper public interface UserMapper extends MyMapper<User> { @Select("select * from user where username=#{username}") 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 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)依賴
Spring Boot提供的數(shù)據(jù)訪問框架Spring Data Redis基于Jedis。可以通過引入 spring-boot-starter-redis 來配置依賴關(guān)系。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency>
3.2 Redis 配置
1、Spring-boot 連接單機版Redis 的配置如下:
# REDIS (RedisProperties) # Redis數(shù)據(jù)庫索引(默認為0) spring.redis.database=0 # Redis服務(wù)器地址 spring.redis.host=localhost # Redis服務(wù)器連接端口 spring.redis.port=6379 # Redis服務(wù)器連接密碼(默認為空) spring.redis.password= # 連接池最大連接數(shù)(使用負值表示沒有限制) 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、Spriig-boot 連接Sentinel 哨兵集群配置:
# REDIS (RedisProperties) # Redis數(shù)據(jù)庫索引(默認為0) spring.redis.database=0 # Redis服務(wù)器地址 #spring.redis.host=localhost # Redis服務(wù)器連接端口 #spring.redis.port=6379 # Redis服務(wù)器連接密碼(默認為空) spring.redis.password= # 連接池最大連接數(shù)(使用負值表示沒有限制) 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 #哨兵監(jiān)聽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工具類,后續(xù)可以繼續(xù)優(yōu)化。
@Component public class RedisComponent { @Autowired //操作字符串的template,StringRedisTemplate是RedisTemplate的一個子集 private StringRedisTemplate stringRedisTemplate; private Logger logger = LoggerFactory.getLogger(RedisComponent.class); @Autowired // RedisTemplate,可以進行所有的操作 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 依賴:
<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 配置類:
在Application.java 同級創(chuàng)建一個Swagger2 的配置類:
@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使用說明: @Api:用在類上,說明該類的作用 @ApiOperation:用在方法上,說明方法的作用 @ApiIgnore:使用該注解忽略這個API @ApiImplicitParams:用在方法上包含一組參數(shù)說明 @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一個請求參數(shù)的各個方面 paramType:參數(shù)放在哪個地方 header-->請求參數(shù)的獲?。篅RequestHeader query-->請求參數(shù)的獲取:@RequestParam path(用于restful接口)-->請求參數(shù)的獲?。篅PathVariable body(不常用) form(不常用) name:參數(shù)名 dataType:參數(shù)類型 required:參數(shù)是否必須傳 value:參數(shù)的意思 defaultValue:參數(shù)的默認值 @ApiResponses:用于表示一組響應(yīng) @ApiResponse:用在@ApiResponses中,一般用于表達一個錯誤的響應(yīng)信息 code:數(shù)字,例如400 message:信息,例如"請求參數(shù)沒填好" response:拋出異常的類 @ApiModel:描述一個Model的信息(這種一般用在post創(chuàng)建的時候,使用@RequestBody這樣的場景,請求參數(shù)無法使用@ApiImplicitParam注解進行描述的時候) @ApiModelProperty:描述一個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 = "測試用例") @RestController @RequestMapping(value="/users") // 通過這里配置使下面的映射都在/users下,可去除 public class UserController { @ApiOperation(value="獲取用戶列表", notes="") @RequestMapping(value={""}, method= RequestMethod.GET) public List<User> getUserList() { return new ArrayList<>(); } @ApiOperation(value="創(chuàng)建用戶", notes="根據(jù)User對象創(chuàng)建用戶") @ApiImplicitParam(name = "user", value = "用戶詳細實體user", required = true, dataType = "User") @RequestMapping(value="", method=RequestMethod.POST) public String postUser(@RequestBody User user) { return "success"; } @ApiOperation(value="獲取用戶詳細信息", notes="根據(jù)url的id來獲取用戶詳細信息") @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Long") @RequestMapping(value="/{id}", method=RequestMethod.GET) public User getUser(@PathVariable Long id) { return new User(); } @ApiOperation(value="更新用戶詳細信息", notes="根據(jù)url的id來指定更新對象,并根據(jù)傳過來的user信息來更新用戶詳細信息") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Long"), @ApiImplicitParam(name = "user", value = "用戶詳細實體user", required = true, dataType = "User") }) @RequestMapping(value="/{id}", method=RequestMethod.PUT) public String putUser(@PathVariable Long id, @RequestBody User user) { return "success"; } @ApiOperation(value="刪除用戶", notes="根據(jù)url的id來指定刪除對象") @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Long") @RequestMapping(value="/{id}", method=RequestMethod.DELETE) public String deleteUser(@PathVariable Long id) { return "success"; } }
完成上述代碼添加上,啟動Spring Boot程序,訪問:http://localhost:8080/swagger-ui.html
。
就能看到前文所展示的RESTful API的頁面。我們可以再點開具體的API請求,以POST類型的/users請求為例,可找到上述代碼中我們配置的Notes信息以及參數(shù)user的描述信息,如下圖所示。
4、接入Jenkins&SonarQube
項目框架搭建好后,我們可以通Jenkins 進行項目的自動發(fā)版,以及SonarQube 進行代碼質(zhì)量檢測。在接入錢,我們需要將項目打包成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 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(DemoApplication.class); } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
在我的上一篇博客,哆啦A夢的傳送門,已經(jīng)講解了一些基本配置方法,這里為大家講解一下,接入SonarQube 進行代碼質(zhì)量檢測的配置(需要本地安裝SonarQube服務(wù))。
首先需要在MetaData 中,加入SonarQube 的項目名(新建的命名):
然后在Post Steps 中選擇添加 Execute SonarQube Scanner:
在配置好這兩項后,Jenkins 在編譯文件時,就會執(zhí)行SonarQube 代碼質(zhì)量檢測。
最后,我們可以設(shè)置項目在編譯完后,執(zhí)行shell 腳本,進行項目的自動發(fā)版:
項目編譯完后,會找到項目下的playbook,執(zhí)行里面的腳本,將我們的項目部署到設(shè)定的服務(wù)器中。
源碼地址:https://github.com/jaycekon/SpringBootDemo
總結(jié) :
本篇文章為大家?guī)砹薙pring-boot 的架構(gòu)搭建,主要使用到了目前較為流行的技術(shù)。希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- 關(guān)于SpringBoot3.x中spring.factories功能被移除的解決方案
- 淺析SpringBoot微服務(wù)中異步調(diào)用數(shù)據(jù)提交數(shù)據(jù)庫的問題
- 使用Spring?Boot+gRPC構(gòu)建微服務(wù)并部署的案例詳解
- 教你在Spring Boot微服務(wù)中集成gRPC通訊的方法
- SpringBoot+Nacos+Kafka微服務(wù)流編排的簡單實現(xiàn)
- SpringBoot+SpringCloud用戶信息微服務(wù)傳遞實現(xiàn)解析
- SpringBoot集成gRPC微服務(wù)工程搭建實踐的方法
- 分享Spring Boot 3.x微服務(wù)升級歷程
相關(guān)文章
Java源碼解析HashMap的tableSizeFor函數(shù)
今天小編就為大家分享一篇關(guān)于Java源碼解析HashMap的tableSizeFor函數(shù),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01Java String方法獲取字符出現(xiàn)次數(shù)及字符最大相同部分示例
這篇文章主要介紹了Java String方法獲取字符出現(xiàn)次數(shù)及字符最大相同部分,涉及java字符串的遍歷、比較、計算等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09MyBatis將查詢出的兩列數(shù)據(jù)裝配成鍵值對的操作方法
這篇文章主要介紹了MyBatis將查詢出的兩列數(shù)據(jù)裝配成鍵值對的操作代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08Java中獲取?List中最后一個元素3種方法以及實際應(yīng)用
這篇文章主要給大家介紹了關(guān)于Java中獲取?List中最后一個元素3種方法以及實際應(yīng)用的相關(guān)資料,由于List的索引是從0開始的,所以最后一個元素的索引是List的大小減1,需要的朋友可以參考下2023-11-11