Spring Cloud之路:(一)Spring Boot小試
一、序言
Spring Cloud簡介
Spring Cloud 為開發人員提供了快速構建分散式系統的一些工具,包括配置管理、服務發現、斷路器、路由、微代理、事件匯流排、全域性鎖、決策競選、分散式會話等等。它執行環境簡單,可以在開發人員的電腦上跑。
Spring Boot 簡介
Spring Boot 致力於簡潔,讓開發者寫更少的配置,程式能夠更快的執行和啟動。它是下一代javaweb框架,並且它是Spring Cloud (微服務)的基礎。
二者關係
此外,最最最重要的是Spring Boot做為下一代 web 框架,當下作為最新最火的微服務的翹楚的Spring Cloud則是基於Spring Boot的,所以需要開發中對Spring Boot有一定的瞭解,
二、例項
開發環境:Mac + IDEA + Mysql
1、搭建Spring Boot程式
具體步驟:
- new prpject -> spring initializr -> next
- 再次,next
- Web -> Web -> next
應用建立成功後,會生成相應的目錄和檔案。其中有一個Application類,它是程式的入口。
2、程式的入口檔案:DemoApplication.java
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
3、程式配置檔案:application.properties
在resources檔案下有一個檔案,它是程式的配置檔案。預設為空,寫點配置 ,程式的埠為8080,context-path為 /springboot:
server.port=8080
server.context-path=/springboot
girl.name =B
girl.age = 18
girl.content = content:${name},age:${age}
spring.profiles.active=prod
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 在mysql中建立dbgirl資料庫
spring.datasource.url=jdbc:mysql://localhost:3306/dbgirl?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
spring.datasource.username=root
spring.datasource.password=1234567890
#通過jpa方式操作資料庫
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
這些都是資料庫常見的一些配置沒什麼可說的,其中ddl_auto: create 代表在資料庫建立表,update 代表更新,首次啟動需要create ,如果你想通過hibernate 註解的方式建立資料庫的表的話,之後需要改為 update.
4、專案的依賴配置檔案
由於本案例採用通過jpa方式操作Mysql資料庫。需要匯入相應的jar ,在pom.xml中新增依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
5、實體檔案:Girl.java
建立一個實體girl,這是基於hibernate的:
@Entity
public class Girl {
@Id
@GeneratedValue
private Integer id;
private String cupSize;
private Integer age;
public Girl() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
}
6、實體屬性注入檔案:GirlProperties.java
通過ConfigurationProperties註解,將屬性注入到bean中,通過Component註解將bean註解到spring容器中:
@ConfigurationProperties(prefix="girl")
@Component
public class GirlProperties {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
7、DAO介面檔案:GirlRep.java
建立Dao介面, springboot 將介面類會自動註解到spring容器中,不需要做任何配置,只需要繼承JpaRepository 即可:
//其中第二個引數為Id的型別
public interface GirlRep extends JpaRepository<Girl,Integer> {
}
8、路由控制檔案:GirlController.java
@RestController
public class GirlController {
@Autowired
private GirlRep girlRep;
/**
* 查詢所有女生列表
* @return
*/
@RequestMapping(value = "/girls",method = RequestMethod.GET)
public List<Girl> getGirlList(){
return girlRep.findAll();
}
}
9、執行程式
- 執行前務必在mysql中建立dbgirl資料庫
- 啟動DemoApplication檔案的main函式
- 訪問網址,http://localhost:8080/springboot/girls,結果
[]
。 - 訪問mysql,執行插入語句,
mysql> insert into girl(age,cup_size) values(22,"A");
- 重新整理頁面,http://localhost:8080/springboot/girls,結果
[[{"id":1,"cupSize":"A","age":22}]]
,查詢資料庫成功。
Q & A
1、報錯:解決問題Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean
方案:在DemoApplication.java檔案中新增如下程式碼:
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
return factory;
}
2、報錯:springboot configuration annotation processor not found in classpath
方案:在pom.xml新增
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
附錄
SpringBoot非官方教程 | 第二十五篇:2小時學會springboot,案例僅供參考,有比較汙的地方本人概不負責……
示例程式碼-github,望star。