1. 程式人生 > >【微框架】之一:從零開始,輕鬆搞定SpringCloud微框架系列--開山篇

【微框架】之一:從零開始,輕鬆搞定SpringCloud微框架系列--開山篇

一、SpringCloud專案簡介

  Spring Cloud:

    微服務工具包,為開發者提供了在分散式系統的配置管理、服務發現、斷路器、智慧路由、微代理、控制匯流排等開發工具包。

  Spring Boot:

    旨在簡化建立產品級的 Spring 應用和服務,簡化了配置檔案,使用嵌入式web伺服器,含有諸多開箱即用微服務功能

    可以和spring cloud聯合部署。

    

二、SpringCloud子專案介紹

  Spring Cloud Config:配置管理開發工具包,可以讓你把配置放到遠端伺服器,目前支援本地儲存、Git以及Subversion。
  Spring Cloud Bus:

事件、訊息匯流排,用於在叢集(例如,配置變化事件)中傳播狀態變化,可與Spring Cloud Config聯合實現熱部署。
  Spring Cloud Netflix:針對多種Netflix元件提供的開發工具包,其中包括Eureka、Hystrix、Zuul、Archaius等。
  Netflix Eureka:雲端負載均衡,一個基於 REST 的服務,用於定位服務,以實現雲端的負載均衡和中間層伺服器的故障轉移。
  Netflix Hystrix:容錯管理工具,旨在通過控制服務和第三方庫的節點,從而對延遲和故障提供更強大的容錯能力。
  Netflix Zuul:邊緣服務工具,是提供動態路由,監控,彈性,安全等的邊緣服務。
  Netflix Archaius:
配置管理API,包含一系列配置管理API,提供動態型別化屬性、執行緒安全配置操作、輪詢框架、回撥機制等功能。
  Spring Cloud for Cloud Foundry:通過Oauth2協議繫結服務到CloudFoundry,CloudFoundry是VMware推出的開源PaaS雲平臺。
  Spring Cloud Sleuth:日誌收集工具包,封裝了Dapper,Zipkin和HTrace操作。
  Spring Cloud Data Flow:大資料操作工具,通過命令列方式操作資料流。
  Spring Cloud Security:安全工具包,為你的應用程式新增安全控制,主要是指OAuth2。
  Spring Cloud Consul:
封裝了Consul操作,consul是一個服務發現與配置工具,與Docker容器可以無縫整合。
  Spring Cloud Zookeeper:操作Zookeeper的工具包,用於使用zookeeper方式的服務註冊和發現。
  Spring Cloud Stream:資料流操作開發包,封裝了與Redis,Rabbit、Kafka等傳送接收訊息。
  Spring Cloud CLI:基於 Spring Boot CLI,可以讓你以命令列方式快速建立雲元件。

三、微服務開發要素

  1、Codebase:從一個程式碼庫部署到多個環境。

  2、Dependencies:使用顯式的宣告隔離依賴,即模組單獨執行,並可以顯式管理依賴。

  3、Config:在系統外部儲存配置資訊。

  4、Backing Services:把支援性服務看做是資源,支援性服務包括資料庫、訊息佇列、緩衝伺服器等。

  5、Build, release, run:嚴格的劃分編譯、構建、執行階段,每個階段由工具進行管理。

  6、Processes:應用作為無狀態執行。

  7、Port binding:經由埠繫結匯出服務,優先選擇 HTTP API 作為通用的整合框架。

  8、Concurrency:併發性使用水平擴充套件實現,對於web就是水平擴充套件web應用實現。

  9、Disposability:服務可處置性,任何服務可以隨意終止或啟動。

  10、Dev/prod parity:開發和生產環境保持高度一致,一鍵式部署。

  11、Logs:將日誌看做是事件流來管理,所有參與的服務均使用該方式處理日誌。

  12、Admin processes:管理任務作為一次性的過程執行(使用指令碼管理服務啟動和停止)。

——————————————————————————————————————————

接下來,我們開始建立應用了····

那麼...

四、使用spring boot建立第一個應用

   4.1 前言

    spring boot 的核心技術當然還是spring,是基於spring 4.x。

   4.2 環境說明

IDE:Myeclipse 10

    JDK:1.8

    管理:mvn 3

    伺服器:tomcat

    (關於環境搭建我們這裡不多說了,需要的自行找度娘)

  4.3 建立一個maven專案

先在pom.xml中加入依賴的包。

複製程式碼
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4 
 5     <modelVersion>4.0.0</modelVersion>
 6     <groupId>Cloud</groupId>
 7     <artifactId>hyh</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9 
10     <parent>
11         <groupId>org.springframework.boot</groupId>
12         <artifactId>spring-boot-starter-parent</artifactId>
13         <version>1.3.0.RELEASE</version>
14     </parent>
15 
16     <dependencies>
17         <dependency>
18             <groupId>org.springframework.boot</groupId>
19             <artifactId>spring-boot-starter-web</artifactId>
20         </dependency>
21         
22     </dependencies>
23 
24     <build>
25         <plugins>
26             <plugin>
27                 <groupId>org.springframework.boot</groupId>
28                 <artifactId>spring-boot-maven-plugin</artifactId>
29                 <dependencies>
30                     <dependency>
31                         <groupId>org.springframework</groupId>
32                         <artifactId>springloaded</artifactId>
33                         <version>1.2.5.RELEASE</version>
34                     </dependency>
35                 </dependencies>
36             </plugin>
37         </plugins>
38     </build>
39 </project>
複製程式碼

如圖:

    

     我們建立了一個類:SpringBootTest.java:

複製程式碼
 1 package com.hyh.bk;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.ResponseBody;
 8 @Controller
 9 @SpringBootApplication
10 public class SpringBootTest {
11     
12     @ResponseBody
13     @RequestMapping(value="/")
14     String location(){
15         return "北京";
16     }
17     /**
18      * 主函式
19      * 
20      */
21     public static void main(String[] args) {
22         System.out.println("-------------");
23         SpringApplication.run(SpringBootTest.class, args);
24     }
25 }
複製程式碼

    解釋:

      @[email protected]  + @EnableAutoConfiguration + @ComponentScan

      @Configuration,@ComponentSca這倆註解語法是spring框架中的。起步於spring 3.x

      @EnableAutoConfiguration是spring boot語法,表示自動配置。

    原創 ,歡迎轉載,請註明出處!

    交流wx請加: wixf150