1. HttpClient
1.1 業務需求說明

1.2 HttpClient介紹
HTTP 協議可能是現在 Internet 上使用得最多、最重要的協議了,越來越多的 Java 應用程式需要直接通過 HTTP 協議來訪問網路資源,雖然在 JDK 的 java net包中已經提供了訪問 HTTP 協議的基本功能,但是對于大部分應用程式來說,JDK 庫本身提供的功能還不夠豐富和靈活,HttpClient 是 Apache Jakarta Common 下的子專案,用來提供高效的、最新的、功能豐富的支持 HTTP 協議的客戶端編程工具包,并且它支持 HTTP 協議最新的版本和建議,HttpClient 已經應用在很多的專案中,比如 Apache Jakarta 上很著名的另外兩個開源專案 Cactus 和 HTMLUnit 都使用了 HttpClient,現在HttpClient最新版本為 HttpClient 4.5 .6(2015-09-11)
1.3 HttpClient入門案例
1.3.1 匯入jar包
<!--添加httpClient jar包 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
1.3.2 HttpClient入門案例
package com.jt.test;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
//@SpringBootTest //從spring容器中獲取bean物件,之后完成測驗業務.
public class TestHttpClient {
/**
* 1.實體化HttpClient物件
* 2.定義遠程訪問的url地址
* 3.定義請求型別的物件
* 4.發起http請求,獲取回應的結果
* 5.對回傳值結果進行校驗.獲取真實的資料資訊.
* */
@Test
public void testGet() throws IOException {
HttpClient httpClient = HttpClients.createDefault();
String url = "http://www.baidu.com";
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
//常見結果狀態 200 404 406引數例外 500后端服務器例外 504超時 502訪問的網址不存在
//獲取狀態碼
int status = httpResponse.getStatusLine().getStatusCode();
if(status == 200){
//獲取回應結果
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity,"UTF-8");
System.out.println(result);
}
}
}
1.4 HttpClient實作業務邏輯
1.4.1 業務需求
用戶通過http://www.jt.com/user/testHttpClient請求,獲取UserList集合資訊.
JT-WEB服務器 訪問JT-SSO時的請求http://sso.jt.com/user/testHttpClient
1.4.2 編輯前端Controller
@RequestMapping("/testHttpClient")
@ResponseBody
public List<User> testHttpClient(){
return userService.testHttpClient();
}
1.4.2 編輯前端Service
package com.jt.service;
import com.jt.pojo.User;
import com.jt.util.ObjectMapperUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
@Override
public List<User> testHttpClient() {
List userList = new ArrayList<>();
//由jt-web服務器去鏈接jt-sso的服務器
String url = "http://sso.jt.com/user/testHttpClient";
HttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse httpResponse =httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode() == 200){
HttpEntity httpEntity = httpResponse.getEntity();
String result = EntityUtils.toString(httpEntity, "UTF-8");
userList = ObjectMapperUtil.toObject(result, userList.getClass());
/* for (LinkedHashMap<String,Object> map : userList){
User userTemp = new User();
userTemp.setId( Long.parseLong(map.get("id")+""));
userTemp.setUsername((String)map.get("username"));
userTemp.setPassword((String)map.get("password"));
userTemp.setPhone((String)map.get("phone"));
userTemp.setEmail((String)map.get("phone"));
userList2.add(userTemp);
}*/
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return userList;
}
}
1.4.3 編輯后端Controller
/**
* http://sso.jt.com/user/testHttpClient
* 回傳List<User>
*/
@RequestMapping("testHttpClient")
public List<User> testHttpClient(){
return userService.findAll();
}
1.4.4 編輯后端Service
@Override
public List<User> findAll() {
return userMapper.selectList(null);
}
2. SOA思想
2.1 SOA思想介紹
面向服務的架構(SOA)是一個組件模型,它將應用程式的不同功能單元(稱為服務)進行拆分,并通過這些服務之間定義良好的介面和協議聯系起來,介面是采用中立的方式進行定義的,它應該獨立于實作服務的硬體平臺、作業系統和編程語言,這使得構建在各種各樣的系統中的服務可以以一種統一和通用的方式進行互動,

3. RPC介紹(呼叫形式的統稱)
3.1 RPC介紹
RPC(Remote Procedure Call)遠程程序呼叫,簡單的理解是一個節點請求另一個節點提供的服務
本地程序呼叫:如果需要將本地student物件的age+1,可以實作一個addAge()方法,將student物件傳入,對年齡進行更新之后回傳即可,本地方法呼叫的函式體通過函式指標來指定,
遠程程序呼叫:addAge方法在其他的服務器中,如果需要呼叫則必須通過遠程的方式通知其他服務器幫我完成業務呼叫.
總結: 利用第三方的服務器,幫我完成業務呼叫的程序.
理解: 分布式環境中 業務呼叫幾乎都是RPC的.
4. 微服務
4.1 什么是微服務
說明:
1. 為了降低代碼的耦合性,將專案進行了拆分.按照功能模塊拆分為若干個專案.該專案稱之為服務.(分布式思想).
2. 如果采用微服務的結構,要求服務器如果出現了故障應該實作自動化的故障的遷移(高可用HA)
4.2 現有服務分析
說明:由于nginx負載均衡/反向代理都需要人為的配置,并且出現了問題不能及時的實作故障的遷移,所以需要升級為微服務的架構的設計.

4.3 微服務架構設計

實作步驟:
1. 服務提供者啟動時,.將自己的資訊注冊到注冊中心中.
2. 注冊中心接受到了用戶的請求之后,更新服務串列資訊.
3. 當消費者啟動時,首先會鏈接注冊中心,獲取服務串列資料.
4. 注冊中心將自己的服務串列資訊同步給客戶端(消費者)
5. 消費者接收到服務串列資料之后,將資訊保存到自己的本地.方便下次呼叫
6. 當消費者接收到用戶的請求時,根據自己服務串列的資訊進行負載均衡的操作,選擇其中一個服務的提供者,根據IP:PORT 進行RPC呼叫.
7. 當服務提供者宕機時,注冊中心會有心跳檢測機制,如果檢查宕機,則更新本地的服務串列資料,并且全網廣播通知所有的消費者更新服務串列.
4.4 ZK安裝參見檔案
4.5 為什么集群一般都是奇數個?
公式: 存活的節點 > N/2
常識: 最小的集群的單位3臺.
例子:
1個節點能否搭建集群? 1-1 > 1/2 假的 1個節點不能搭建集群
2個節點能否搭建集群? 2-1 > 2/2 假的 2個節點不能搭建集群
3個節點能否搭建集群? 3-1 > 3/2 真的 3個節點能搭建集群
4個節點能否搭建集群? 4-1 > 4/2 真的 4個節點能搭建集群
3個節點最多允許宕機1臺,否則集群崩潰.
4個節點最多允許宕機1臺,否則集群崩潰.
搭建奇數臺和偶數臺其實都可以,但是從容災性的角度考慮,發現奇數和偶數的效果相同,.所以搭建奇數臺.
4.6 ZK集群選舉規則
說明: zk集群選舉采用最大值(myid)優先的演算法實作,如果集群中沒有主機,則開始選舉(超半數即可),如果有主機,則選舉結束.
考題: 1 2 3 4 5 6 7 依次啟動時
問題1:誰當主機? 4當主機
問題2:誰永遠不能當選主機? 1,2,3
5 Dubbo框架
5.1 Dubbo介紹
Apache Dubbo |?d?b??| 提供了六大核心能力:面向介面代理的高性能RPC呼叫,智能容錯和負載均衡,服務自動注冊和發現,高度可擴展能力,運行期流量調度,可視化的服務治理與運維,

呼叫原理圖:

5.2 Dubbo入門案例
5.2.1 修改組態檔內容
1).修改SpringBoot的版本

2).修改模塊名稱 改為dubbo-jt-demo-interface

5.2.2 匯入maven專案

5.2.3 測驗效果

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/224795.html
標籤:java
