本項目的筆記和資料的Download,請點擊這一句話自行獲取。html
隨着互聯網的發展,網站應用的規模不斷擴大。需求的激增,帶來的是技術上的壓力。系統架構也所以也不斷的演進、升級、迭代。程序員
從單一應用,到垂直拆分,到分佈式服務,到SOA面向服務,以及如今火熱的微服務架構,還有在Google帶領下來勢洶涌的Service Mesh。web
當網站流量很小時,只需一個應用,將全部功能都部署在一塊兒,以減小部署節點和成本。此時,用於簡化增刪改查工做量的數據訪問框架(ORM)是影響項目開發的關鍵。spring
存在的問題:數據庫
當訪問量逐漸增大,單一應用沒法知足需求,此時爲了應對更高的併發和業務需求,咱們根據業務功能對系統進行拆分:apache
優勢:編程
缺點:json
當垂直應用愈來愈多,應用之間交互不可避免,將核心業務抽取出來,做爲獨立的服務,逐漸造成穩定的服務中心,使前端應用能更快速的響應多變的市場需求。此時,用於提升業務複用及整合的分佈式調用是關鍵。
優勢:
缺點:
當服務愈來愈多,容量的評估,小服務資源的浪費等問題逐漸顯現,此時需增長一個調度中心基於訪問壓力實時管理集羣容量,提升集羣利用率。此時,用於提升機器利用率的資源調度和治理中心(SOA)是關鍵
之前出現了什麼問題?
服務治理要作什麼?
缺點:
前面說的SOA,英文翻譯過來是面向服務。微服務,彷佛也是服務,都是對系統進行拆分。所以二者很是容易混淆,但其實缺有一些差異:
微服務的特色:
微服務結構圖:
不管是微服務仍是SOA,都面臨着服務間的遠程調用。那麼服務間的遠程調用方式有哪些呢?
常見的遠程調用方式有如下兩種:
RPC:Remote Produce Call遠程過程調用,相似的還有RMI。自定義數據格式,基於原生TCP通訊,速度快,效率高。如今熱門的dubbo,就是RPC的典型。
Http:http實際上是一種網絡傳輸協議,基於TCP,規定了數據傳輸的格式。如今客戶端瀏覽器與服務端通訊基本都是採用Http協議。也能夠用來進行遠程服務調用。缺點是消息封裝臃腫。
如今熱門的Rest風格,就能夠經過http協議來實現。
RPC,即 Remote Procedure Call(遠程過程調用),是一個計算機通訊協議。 該協議容許運行於一臺計算機的程序調用另外一臺計算機的子程序,而程序員無需額外地爲這個交互做用編程。說得通俗一點就是:A計算機提供一個服務,B計算機能夠像調用本地服務那樣調用A計算機的服務。
經過上面的概念,咱們能夠知道,實現RPC主要是作到兩點:
RPC調用流程圖:
Http協議:超文本傳輸協議,是一種應用層協議。規定了網絡傳輸的請求格式、響應格式、資源定位和操做的方式等。可是底層採用什麼網絡傳輸協議,並無規定,不過如今都是採用TCP協議做爲底層傳輸協議。說到這裏,你們可能以爲,Http與RPC的遠程調用很是像,都是按照某種規定好的數據格式進行網絡通訊,有請求,有響應。沒錯,在這點來看,二者很是類似,可是仍是有一些細微差異。
例如咱們經過瀏覽器訪問網站,就是經過Http協議。只不過瀏覽器把請求封裝,發起請求以及接收響應,解析響應的事情都幫咱們作了。若是是不經過瀏覽器,那麼這些事情都須要本身去完成。
既然兩種方式均可以實現遠程調用,咱們該如何選擇呢?
所以,二者都有不一樣的使用場景:
那麼咱們該怎麼選擇呢?
微服務,更增強調的是獨立、自治、靈活。而RPC方式的限制較多,所以微服務框架中,通常都會採用基於Http的Rest風格服務。
既然微服務選擇了Http,那麼咱們就須要本身來實現對請求和響應的處理。好在開源世界已經有不少的http客戶端工具,可以幫助咱們作這些事情,例如:
接下來,咱們就一塊兒瞭解一款比較流行的客戶端工具:HttpClient
HttpClient是Apache公司的產品,是Http Components下的一個組件。
官網地址:http://hc.apache.org/index.html
特色:
咱們導入課前資料提供的demo工程:《http-demo》
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.leyou.demo</groupId> <artifactId>http-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>http-demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--核心依賴,包括auto-configuration , logging等--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- springboot測試模塊的起步依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- http客戶端工具,個性化實現對請求和響應的處理 --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.10</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
HttpTests
package com.leyou.httpdemo; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.junit.Before; import org.junit.Test; import java.io.IOException; public class HttpTests { CloseableHttpClient httpClient; @Before //初始化 public void init() { httpClient = HttpClients.createDefault(); } @Test public void testGet() throws IOException { //發起get請求 HttpGet request = new HttpGet("http://www.baidu.com"); String response = this.httpClient.execute(request, new BasicResponseHandler()); System.out.println(response); } @Test public void testPost() throws IOException { //發起Post請求 HttpGet request = new HttpGet("http://www.oschina.net/"); request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"); String response = this.httpClient.execute(request, new BasicResponseHandler()); System.out.println(response); } //==========我是一個分割線============// }
HttpClient請求數據後是json字符串,須要咱們本身把Json字符串反序列化爲對象,可使用JacksonJson工具來實現。
JacksonJson
是SpringMVC內置的json處理工具,其中有一個ObjectMapper
類,能夠方便的實現對json的處理:
對象轉json
// json處理工具 private ObjectMapper mapper = new ObjectMapper(); // 成員變量 @Test public void testJson() throws JsonProcessingException { User user = new User(); // 局部變量 user.setId(8L); user.setAge(21); user.setName("柳巖"); user.setUserName("liuyan"); // 序列化 String json = mapper.writeValueAsString(user); System.out.println("json = " + json); }
結果:
// json處理工具:json轉普通對象 private ObjectMapper mapper = new ObjectMapper(); @Test public void testJson() throws IOException { User user = new User(); user.setId(8L); user.setAge(21); user.setName("柳巖"); user.setUserName("liuyan"); // 序列化 String json = mapper.writeValueAsString(user); // 反序列化,接收兩個參數:json數據,反序列化的目標類字節碼 User result = mapper.readValue(json, User.class); System.out.println("result = " + result); }
結果:
json轉集合相對比較麻煩,由於你沒法同時把集合的class和元素的class同時傳遞到那一個字節碼參數的位置。
所以Jackson作了一個類型工廠,用來解決這個問題:
// json處理工具:json轉集合 private ObjectMapper mapper = new ObjectMapper(); @Test public void testJson() throws IOException { User user = new User(); user.setId(8L); user.setAge(21); user.setName("柳巖"); user.setUserName("liuyan"); // 序列化,獲得對象集合的json字符串 String json = mapper.writeValueAsString(Arrays.asList(user, user)); // 反序列化,接收兩個參數:json數據,反序列化的目標類字節碼 List<User> users = mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, User.class)); for (User u : users) { System.out.println("u = " + u); } }
當對象泛型關係複雜時,類型工廠也很差使了。這個時候Jackson提供了TypeReference來接收類型泛型,而後底層經過反射來獲取泛型上的具體類型。實現數據轉換。
// json轉任意複雜類型 private ObjectMapper mapper = new ObjectMapper(); @Test public void testJson() throws IOException { User user = new User(); user.setId(8L); user.setAge(21); user.setName("柳巖"); user.setUserName("liuyan"); Country country = new Country(); country.setId(2L); country.setCountryname("中國"); country.setCountrycode("+86"); // 序列化,獲得對象集合的json字符串 String json = mapper.writeValueAsString(Arrays.asList(user, user)); System.out.println("json = " + json); System.out.println("=============="); // 反序列化,接收兩個參數:json數據,反序列化的目標類字節碼 TypeReference ref = new TypeReference<List<User>>(){}; List<User> users = mapper.readValue(json, ref); for (User u : users) { System.out.println("u = " + u); } }
結果:
Spring提供了一個RestTemplate模板工具類,對基於Http的客戶端進行了封裝,而且實現了對象與json的序列化和反序列化,很是方便。RestTemplate並無限定Http的客戶端類型,而是進行了抽象,目前經常使用的3種都有支持:
首先在項目中註冊一個RestTemplate
對象,能夠在啓動類位置註冊:
@SpringBootApplication public class HttpDemoApplication { public static void main(String[] args) { SpringApplication.run(HttpDemoApplication.class, args); } @Bean public RestTemplate restTemplate() { // 默認的RestTemplate,底層是走JDK的URLConnection方式。 return new RestTemplate(); } }
在測試類中直接@Autowired
注入:
import java.io.IOException; import java.util.Arrays; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest(classes = HttpDemoApplication.class) public class HttpDemoApplicationTests { @Autowired private RestTemplate restTemplate; @Test public void httpGet() { /*經過RestTemplate的getForObject()方法,傳遞url地址及實體類的字節碼, RestTemplate會自動發起請求,接收響應,而且幫咱們對響應結果進行反序列化。*/ User user = this.restTemplate.getForObject("http://localhost/hello", User.class); System.out.println(user); } //==============華麗的分割線==================//
學習完了Http客戶端工具,接下來就能夠正式學習微服務了。
- 知道什麼是SpringCloud
================================================
參考資料:
關於SpringBoot中spring-boot-starter-*起步依賴參考指南
end