摘要:需求在了解了前面我們關(guān)于服務(wù)治理出現(xiàn)的必要性之后。我們知道服務(wù)治理是建立在眾多服務(wù)基礎(chǔ)之上的,那么,第一步,打通這些服務(wù)是基礎(chǔ),也就是我們常說的遠(yuǎn)程調(diào)用。上面執(zhí)行遠(yuǎn)程調(diào)用也類似。
需求
在了解了前面我們關(guān)于服務(wù)治理出現(xiàn)的必要性之后。我們知道服務(wù)治理是建立在眾多“服務(wù)”基礎(chǔ)之上的,那么,第一步,打通這些服務(wù)是基礎(chǔ),也就是我們常說的 RPC 遠(yuǎn)程調(diào)用。要像調(diào)用本地方法一樣調(diào)用遠(yuǎn)程服務(wù)器上的方法。
現(xiàn)在簡單粗暴口語化的方式來介紹一個(gè)需求:
分析A 服務(wù)器上部署的項(xiàng)目中,有一個(gè)UserService里面有一個(gè)getUserInfo的方法。
B 服務(wù)器上想"直接"調(diào)用該方法,怎么辦?
我們以 PHP 為例來進(jìn)行分析。
我們希望在 B 服務(wù)器上實(shí)現(xiàn)類似于 A 服務(wù)器上直接調(diào)用方式
$userService = new UserService(); $userService->getUserInfo($uid);
我們經(jīng)常會(huì)使用 SDK 來調(diào)用第三方提供的 api 服務(wù),我們的做法肯定類似于
$client = new SDKClient(); $request = new SDKUserServiceRequestGetStudentInfoRequest(); $request->setUid($uid); $request->setMethod("GET"); $response = $client->doAction($request);
sdk 里面的 GetStudentInfoRequest 通過http映射 A 服務(wù)器上的UserService::getUserInfo。
sdk 的改造我們只需要在原來的基礎(chǔ)上稍作修改即可,下面的代碼僅做簡單的演示
服務(wù)端該服務(wù)部署在localhost:8081
class UserService { public static function getUserInfo($uid) { // 假設(shè)以下內(nèi)容從數(shù)據(jù)庫取出 return [ "id" => $uid, "username" => "mengkang", ]; } } $service = $_GET["service"]; $action = $_GET["action"]; $argv = file_get_contents("php://input"); if (!$service || !$action) { die(); } if ($argv) { $argv = json_decode($argv, true); } $res = call_user_func_array([$service, $action], $argv); echo json_encode($res);客戶端
class Client { private $url; private $service; private $rpcConfig = [ "UserService" => "http://127.0.0.1:8081", ]; /** * Client constructor. * @param $service */ public function __construct($service) { if (array_key_exists($service, $this->rpcConfig)) { $this->url = $this->rpcConfig[$service]; $this->service = $service; } } public function __call($action, $arguments) { $content = json_encode($arguments); $options["http"] = [ "timeout" => 5, "method" => "POST", "header" => "Content-type:application/x-www-form-urlencoded", "content" => $content, ]; $context = stream_context_create($options); $get = [ "service" => $this->service, "action" => $action, ]; $url = $this->url . "?" . http_build_query($get); $res = file_get_contents($url, false, $context); return json_decode($res, true); } } $userService = new Client("UserService"); var_export($userService->getUserInfo(103));
這樣是不是就非常方便的在客戶端實(shí)現(xiàn)了像在本地一樣調(diào)用遠(yuǎn)程的方法呢?這也是鳥哥 @Laruence yar 的操作原理。下面對比下 Yar 的 demo:
Yar 演示yar https://github.com/laruence/yar
yar 的 java 客戶端 https://github.com/zhoumengka...
客戶端代碼,假設(shè)該服務(wù)設(shè)在局域網(wǎng)10.211.55.4上
class RpcClient { // RPC 服務(wù)地址映射表 public static $rpcConfig = array( "RewardScoreService" => "http://10.211.55.4/yar/server/RewardScoreService.class.php", ); public static function init($server){ if (array_key_exists($server, self::$rpcConfig)) { $uri = self::$rpcConfig[$server]; return new Yar_Client($uri); } } } $RewardScoreService = RpcClient::init("RewardScoreService"); var_dump($RewardScoreService->support(1, 2));
服務(wù)器端代碼
class RewardScoreService { /** * $uid 給 $feedId 點(diǎn)贊 * @param $feedId interge * @param $uid interge * @return void */ public function support($uid,$feedId){ return "uid = ".$uid.", feedId = ".$feedId; } } $yar_server = new Yar_server(new RewardScoreService()); $yar_server->handle();
yar 背后的故事就是我前面那段 sdk 改造的代碼演示。想必看到這里,rpc 框架不再那么神秘了吧。
當(dāng)然這只是實(shí)現(xiàn)了 rpc 的一小部分,簡單的遠(yuǎn)程調(diào)用。畢竟 php 是世界上最好的語言。
java 上面執(zhí)行遠(yuǎn)程調(diào)用也類似。
如果換成 java 可稍微麻煩點(diǎn),java 實(shí)現(xiàn)起來之后會(huì)讓你覺得更加的本地化,所以 java 也是最強(qiáng)大的語言。
由于 java 是靜態(tài)編譯的,不存在類似于 php 里的__call方法的方式來實(shí)現(xiàn)遠(yuǎn)程調(diào)用,一般通過動(dòng)態(tài)代理來實(shí)現(xiàn)
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; /** * Created by zhoumengkang on 5/12/15. */ /** * 點(diǎn)贊的積分服務(wù)接口 */ interface RewardScoreService{ String support(int uid,int feedId); } public class SupportService { public static void main(String[] args) { add(1,2); } /** * uid 給 feedId 點(diǎn)贊 * @param uid * @param feedId * @return */ public static String add(int uid, int feedId){ YarClient yarClient = new YarClient(); RewardScoreService rewardScoreService = (RewardScoreService) yarClient.proxy(RewardScoreService.class); return rewardScoreService.support(uid, feedId); } } class YarClient { public final Object proxy(Class type) { YarClientInvocationHandler handler = new YarClientInvocationHandler(); return Proxy.newProxyInstance(type.getClassLoader(), new Class[]{type}, handler); } } final class YarClientInvocationHandler implements InvocationHandler { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("這里的動(dòng)態(tài)調(diào)用實(shí)現(xiàn)了 php 的 __call 方法"); System.out.println("method : " + method.getName()); for (int i = 0; i < args.length; i++) { System.out.println("args["+ i +"] : " + args[i]); } return null; } }了解更多
看完本篇,是不是頓時(shí)覺得 rpc 框架不再那么神秘,有一點(diǎn)點(diǎn)感覺了呢?
老鐵周末的直播:揭開她的神秘面紗 - 零基礎(chǔ)構(gòu)建自己的服務(wù)治理框架 趕快上車
https://segmentfault.com/l/15...
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/31974.html
摘要:服務(wù)化的出現(xiàn)假想一個(gè)京東的發(fā)展路程都是我虛構(gòu)的。更多的服務(wù)的提取抽離,更多的團(tuán)隊(duì)出現(xiàn)業(yè)務(wù)繼續(xù)發(fā)展,出現(xiàn)了京東大藥房,專門賣藥,需要調(diào)用京東目前的財(cái)務(wù)系統(tǒng)。 以下內(nèi)容都是自己的理解,不保證正確,可能是對的,也可能把你帶溝里,自己甄別。 更多詳情請看直播 揭開她的神秘面紗 - 零基礎(chǔ)構(gòu)建自己的服務(wù)治理框架 https://segmentfault.com/l/15... 很久之前聽別人分...
摘要:在服務(wù)治理方面,相較于而言,并不成熟。遺憾的是,往往被部分開發(fā)者片面地視作服務(wù)治理的框架,而非微服務(wù)基礎(chǔ)設(shè)施。因此,建議開發(fā)人員將或者遷移為服務(wù)。因此,下一步需要將其配置服務(wù)遠(yuǎn)程。當(dāng)服務(wù)提供方啟動(dòng)后,下一步實(shí)現(xiàn)一個(gè)服務(wù)消費(fèi)方。 原文鏈接:Dubbo Spring Cloud 重塑微服務(wù)治理,來自于微信公眾號:次靈均閣 摘要 在 Java 微服務(wù)生態(tài)中,Spring Cloud1 成為...
摘要:大揭秘目標(biāo)了解的新特性,以及版本升級的引導(dǎo)。四元數(shù)據(jù)改造我們知道以前的版本只有注冊中心,注冊中心的有數(shù)十個(gè)的鍵值對,包含了一個(gè)服務(wù)所有的元數(shù)據(jù)。 DUBBO——2.7大揭秘 目標(biāo):了解2.7的新特性,以及版本升級的引導(dǎo)。 前言 我們知道Dubbo在2011年開源,停止更新了一段時(shí)間。在2017 年 9 月 7 日,Dubbo 悄悄的在 GitHub 發(fā)布了 2.5.4 版本。隨后,版本...
摘要:管理這些服務(wù)方案則叫服務(wù)治理。協(xié)議假定某些傳輸協(xié)議的存在,如或,為通信程序之間攜帶信息數(shù)據(jù)。請求程序就是一個(gè)客戶機(jī),而服務(wù)提供程序就是一個(gè)服務(wù)器。在服務(wù)器端,進(jìn)程保持睡眠狀態(tài)直到調(diào)用信息到達(dá)為止。 不涉及其他的語言及工具,我們從PHP本身來談如何實(shí)現(xiàn)服務(wù)治理 本猿人已經(jīng)寫好的服務(wù)治理 https://github.com/CrazyCodes... 治理什么? 這個(gè)專業(yè)名詞很容易發(fā)現(xiàn)...
閱讀 3082·2021-11-24 10:32
閱讀 750·2021-11-24 10:19
閱讀 5509·2021-08-11 11:17
閱讀 1526·2019-08-26 13:31
閱讀 1315·2019-08-23 15:15
閱讀 2335·2019-08-23 14:46
閱讀 2348·2019-08-23 14:07
閱讀 1188·2019-08-23 14:03