【微框架】之一:從零開始,輕鬆搞定SpringCloud微服務系列--開山篇(spring boot 小demo)

Spring 頂級項目,包含衆多,咱們重點學習一下,SpringCloud項目以及SpringBoot項目html

————————————————————main————————————————————java

1、SpringCloud項目簡介

  Spring Cloud:web

    微服務工具包,爲開發者提供了在分佈式系統的配置管理、服務發現、斷路器、智能路由、微代理、控制總線等開發工具包。spring

  Spring Boot:數據庫

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

    能夠和spring cloud聯合部署。tomcat

    

 

2、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,可讓你以命令行方式快速創建雲組件。安全

3、微服務開發要素

  一、Codebase:從一個代碼庫部署到多個環境。服務器

  二、Dependencies:使用顯式的聲明隔離依賴,即模塊單獨運行,並能夠顯式管理依賴。併發

  三、Config:在系統外部存儲配置信息。

  四、Backing Services:把支持性服務看作是資源,支持性服務包括數據庫、消息隊列、緩衝服務器等。

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

  六、Processes:應用做爲無狀態執行。

  七、Port binding:經由端口綁定導出服務,優先選擇 HTTP API 做爲通用的集成框架。

  八、Concurrency:併發性使用水平擴展實現,對於web就是水平擴展web應用實現。

  九、Disposability:服務可處置性,任何服務能夠隨意終止或啓動。

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

  十一、Logs:將日誌看作是事件流來管理,全部參與的服務均使用該方式處理日誌。

  十二、Admin processes:管理任務做爲一次性的過程運行(使用腳本管理服務啓動和中止)。

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

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

那麼...

4、使用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 }
複製代碼

    解釋:

      @SpringBootApplication=@Configuration  + @EnableAutoConfiguration + @ComponentScan

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

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

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

    原文地址:http://www.cnblogs.com/hyhnet/p/5626421.html

    交流wx請加: wixf150

相關文章
相關標籤/搜索