Spring Cloud是一系列框架的有序集合。它利用Spring Boot的開發便利性巧妙地簡化了分佈式系統基礎設施的開發,如服務發現註冊、配置中心、消息總線、負載均衡、斷路器、數據監控等,均可以用Spring Boot的開發風格作到一鍵啓動和部署。java
Spring Cloud並無重複製造輪子,它只是將目前各家公司開發的比較成熟、經得起實際考驗的服務框架組合起來,經過Spring Boot風格進行再封裝屏蔽掉了複雜的配置和實現原理,最終給開發者留出了一套簡單易懂、易部署和易維護的分佈式系統開發工具包。mysql
https://projects.spring.io/spring-cloud/ springcloud項目官方主頁git
https://springcloud.cc/ springcloud中文網 有很詳細的翻譯文檔 github
http://springcloud.cn/ springcloud中文論壇 web
源碼下載地址:https://github.com/bangbangw/wfd360-station (選擇V1版本)spring
構建完成後的項目結構如圖:sql
咱們開發項目,如今基本都用到maven,以及用父子項目,以及公共模塊依賴,來構建方便擴展的項目體系;數據庫
首先咱們創建父項目 wfd360-station,主要是一個pom,管理module,以及管理依賴,規範全部jar包版本等;apache
咱們Springcloud版本 用 Edgware.SR4瀏覽器
Springboot版本 用 1.5.13.RELEASE
修改pom.xml爲:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.wfd360.station</groupId> 8 <artifactId>wfd360-station</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <modules> 11 <module>microservice-common</module> 12 <module>microservice-station-provider-1001</module> 13 <module>microservice-station-consumer-80</module> 14 </modules> 15 <packaging>pom</packaging> 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 <maven.compiler.source>1.8</maven.compiler.source> 19 <maven.compiler.target>1.8</maven.compiler.target> 20 <druid.version>1.1.10</druid.version> 21 </properties> 22 23 <dependencyManagement> 24 <dependencies> 25 <dependency> 26 <groupId>org.springframework.cloud</groupId> 27 <artifactId>spring-cloud-dependencies</artifactId> 28 <version>Edgware.SR4</version> 29 <type>pom</type> 30 <scope>import</scope> 31 </dependency> 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-dependencies</artifactId> 35 <version>1.5.13.RELEASE</version> 36 <type>pom</type> 37 <scope>import</scope> 38 </dependency> 39 <!-- 鏈接池 --> 40 <dependency> 41 <groupId>com.alibaba</groupId> 42 <artifactId>druid</artifactId> 43 <version>${druid.version}</version> 44 </dependency> 45 </dependencies> 46 </dependencyManagement> 47 48 </project>
這裏引入了 springcloud 以及springboot 包括 druid鏈接池 以及 屬性 規範編譯 目標版本,包括鏈接池版本 編碼等信息;
咱們新建module公共模塊項目 microservice-common,主要是放一些其餘項目公用的東西,好比實體類,工具類等等;
pom.xml 加下 jpa和 mysql驅動包依賴
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>wfd360-station</artifactId> 7 <groupId>com.wfd360.station</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>microservice-common</artifactId> 13 <packaging>jar</packaging> 14 15 <dependencies> 16 <dependency> 17 <groupId>org.springframework.boot</groupId> 18 <artifactId>spring-boot-starter-data-jpa</artifactId> 19 </dependency> 20 <dependency> 21 <groupId>mysql</groupId> 22 <artifactId>mysql-connector-java</artifactId> 23 </dependency> 24 </dependencies> 25 26 </project>
咱們新建Ticket實體
1 package com.wfd360.model; 2 3 import javax.persistence.*; 4 import java.io.Serializable; 5 6 /** 7 * Created by admin on 2019/3/25. 8 */ 9 @Entity 10 @Table(name="t_ticket") 11 public class Ticket implements Serializable { 12 /** 13 *序列號b vmvh 14 */ 15 private static final long serialVersionUID = 1L; 16 @Id 17 @GeneratedValue 18 private Integer id; 19 /** 20 * 出發車站 21 */ 22 @Column 23 private String startStation; 24 /** 25 * 到達車站 26 */ 27 @Column 28 private String stopStation; 29 /** 30 * 票價 31 */ 32 @Column 33 private Integer price; 34 35 36 public Integer getId() { 37 return id; 38 } 39 40 public void setId(Integer id) { 41 this.id = id; 42 } 43 44 public String getStartStation() { 45 return startStation; 46 } 47 48 public void setStartStation(String startStation) { 49 this.startStation = startStation; 50 } 51 52 public String getStopStation() { 53 return stopStation; 54 } 55 56 public void setStopStation(String stopStation) { 57 this.stopStation = stopStation; 58 } 59 60 public Integer getPrice() { 61 return price; 62 } 63 64 public void setPrice(Integer price) { 65 this.price = price; 66 } 67 }
包括映射
固然咱們數據庫裏也提早建一個db_station數據庫;
咱們新建一個服務器提供者module子模塊,相似前面建的common公共模塊,名稱是 microservice-ticket-provider-1001
pom.xml爲:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>wfd360-station</artifactId> 7 <groupId>com.wfd360.station</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>microservice-station-provider-1001</artifactId> 13 14 <dependencies> 15 <dependency> 16 <groupId>com.wfd360.station</groupId> 17 <artifactId>microservice-common</artifactId> 18 <version>${project.version}</version> 19 </dependency> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-web</artifactId> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-data-jpa</artifactId> 27 </dependency> 28 <dependency> 29 <groupId>mysql</groupId> 30 <artifactId>mysql-connector-java</artifactId> 31 </dependency> 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-starter-tomcat</artifactId> 35 </dependency> 36 <dependency> 37 <groupId>com.alibaba</groupId> 38 <artifactId>druid</artifactId> 39 </dependency> 40 <!-- 修改後當即生效,熱部署 --> 41 <dependency> 42 <groupId>org.springframework</groupId> 43 <artifactId>springloaded</artifactId> 44 </dependency> 45 <dependency> 46 <groupId>org.springframework.boot</groupId> 47 <artifactId>spring-boot-devtools</artifactId> 48 </dependency> 49 </dependencies> 50 51 52 </project>
該pom.xml中加common項目依賴,以及 springboot的 web jpa 驅動包 tomcat 鏈接池 配置 包括自動熱部署配置。
application.yml配置:
1 server: 2 port: 1001 3 context-path: / 4 5 # 數據源配置 6 spring: 7 datasource: 8 type: com.alibaba.druid.pool.DruidDataSource 9 driver-class-name: com.mysql.jdbc.Driver 10 url: jdbc:mysql://localhost:3306/db_station 11 username: root 12 password: admin 13 jpa: 14 hibernate: 15 ddl-auto: update 16 show-sql: true 17 thymeleaf: 18 cache: false
注意:空格
TicketRepository接口:
1 package com.wfd360.repository; 2 3 import com.wfd360.model.Ticket; 4 import org.springframework.data.jpa.repository.JpaRepository; 5 import org.springframework.data.jpa.repository.JpaSpecificationExecutor; 6 7 /** 8 * Created by 姿式帝-博客園 on 2019/3/25. 9 * 歡迎添加筆者wx(851298348)共同探討、學習! 10 */ 11 12 public interface TicketRepository extends 13 JpaRepository<Ticket, Integer>,JpaSpecificationExecutor<Ticket> { 14 }
TicketService接口:
1 package com.wfd360.service; 2 3 import com.wfd360.model.Ticket; 4 5 import java.util.List; 6 7 /** 8 * Created by 姿式帝-博客園 on 2019/3/25. 9 * 歡迎添加筆者wx(851298348)共同探討、學習! 10 */ 11 12 public interface TicketService { 13 /** 14 * 添加或者修改車票信息 15 * @param student 16 */ 17 public void save(Ticket student); 18 19 /** 20 * 根據id查找車票信息 21 * @param id 22 * @return 23 */ 24 public Ticket findById(Integer id); 25 26 /** 27 * 查詢車票信息 28 * @return 29 */ 30 public List<Ticket> list(); 31 32 /** 33 * 根據id刪除車票信息 34 * @param id 35 */ 36 public void delete(Integer id); 37 }
TicketServiceImpl實現類:
1 package com.wfd360.service.impl; 2 3 import com.wfd360.model.Ticket; 4 import com.wfd360.repository.TicketRepository; 5 import com.wfd360.service.TicketService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Service; 8 9 import java.util.List; 10 11 /** 12 * Created by 姿式帝-博客園 on 2019/3/25. 13 * 歡迎添加筆者wx(851298348)共同探討、學習! 14 */ 15 @Service 16 public class TicketServiceImpl implements TicketService { 17 @Autowired 18 private TicketRepository ticketRepository; 19 20 @Override 21 public void save(Ticket student) { 22 ticketRepository.save(student); 23 } 24 25 @Override 26 public Ticket findById(Integer id) { 27 return ticketRepository.findOne(id); 28 } 29 30 @Override 31 public List<Ticket> list() { 32 return ticketRepository.findAll(); 33 } 34 35 @Override 36 public void delete(Integer id) { 37 ticketRepository.delete(id); 38 } 39 }
TicketProviderController類:
1 package com.wfd360.controller; 2 3 import com.wfd360.model.Ticket; 4 import com.wfd360.service.TicketService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.*; 8 9 import java.util.List; 10 11 /** 12 * Created by 姿式帝-博客園 on 2019/3/25. 13 * 歡迎添加筆者wx(851298348)共同探討、學習! 14 */ 15 @Controller 16 @RequestMapping("/ticket") 17 public class TicketController { 18 @Autowired 19 private TicketService ticketService; 20 /** 21 * 添加或者修改車票信息-V1111 22 * @param ticket 23 * @return 24 */ 25 @PostMapping(value="/save") 26 @ResponseBody 27 public boolean save(Ticket ticket){ 28 try{ 29 ticketService.save(ticket); 30 return true; 31 }catch(Exception e){ 32 return false; 33 } 34 } 35 36 /** 37 * 查詢車票信息 38 * @return 39 */ 40 @GetMapping(value="/list") 41 @ResponseBody 42 public List<Ticket> list(){ 43 List<Ticket> list = ticketService.list(); 44 return list; 45 } 46 47 /** 48 * 根據id查詢車票信息 49 * @return 50 */ 51 @GetMapping(value="/get/{id}") 52 @ResponseBody 53 public Ticket get(@PathVariable("id") Integer id){ 54 return ticketService.findById(id); 55 } 56 57 /** 58 * 根據id刪除車票信息 59 * @return 60 */ 61 @GetMapping(value="/delete/{id}") 62 @ResponseBody 63 public boolean delete(@PathVariable("id") Integer id){ 64 try{ 65 ticketService.delete(id); 66 return true; 67 }catch(Exception e){ 68 return false; 69 } 70 } 71 }
啓動類TicketProviderApplication_1001:
1 package com.wfd360; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 /** 7 * Created by 姿式帝-博客園 on 2019/3/25. 8 * 歡迎添加筆者wx(851298348)共同探討、學習! 9 */ 10 @SpringBootApplication 11 public class TicketProviderApplication_1001 { 12 /** 13 * 14 * @param args 15 */ 16 public static void main(String[] args) { 17 SpringApplication.run(TicketProviderApplication_1001.class, args); 18 19 } 20 }
咱們運行啓動類,自動生成t_student表;
咱們加點數據,方便測試:
瀏覽器請求:http://localhost:1001/ticket/list
而後添加修改的話 咱們借用下 postman插件(請自行測試);