Spring頂級框架有眾多,那么接下的篇幅,我將重點講解SpringCloud微框架的實現
Spring 頂級項目,包含眾多,我們重點學習一下,SpringCloud項目以及SpringBoot項目
————————————————————main————————————————————
一、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 }
解釋:
@SpringBootApplication=@Configuration + @EnableAutoConfiguration + @ComponentScan
@Configuration,@ComponentSca這倆注解語法是spring框架中的。起步於spring 3.x
@EnableAutoConfiguration是spring boot語法,表示自動配置。
原創 ,歡迎轉載,請注明出處!
原文地址:http://www.cnblogs.com/hyhnet/p/5626421.html
交流wx請加: wixf150