国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

分布式軟件架構整合(二)

you_De / 1543人閱讀

摘要:續上篇所需技術等說明本編講解一個分布式架構的整個流程,首先對上編中的工程作一些小小的改動,以支持接下來的實驗。本篇中,定義了服務提供者,注冊中心及客戶端消費者三者關系。一服務提供者這里首先對服務端進行改造。

(續上篇)

所需技術:spring、mybatis、druid、flyway、logback、nodejs,zookeeper,dubbo,dubbo-admin等;
說明:本編講解一個分布式架構的整個流程,首先對上編中的demo工程作一些小小的改動,以支持接下來的實驗。特別說明,zookeeper作為服務注冊中心,dubbo-admin作為注冊中心管理客戶端,雖然都是開源軟件,但本章不作安裝部署講解。

目錄結構調整上圖所示,紅框中,作為調整內容,其它內容不變。

簡要
本篇引入了阿里公司開源的分布式服務框架[dubbo][1]。本篇中,定義了provider服務提供者,zookeeper注冊中心及客戶端消費者三者關系。provider是數據源,基于底層的數據交互都在這里完成。provider將服務暴露給zookeeper注冊中心,provider的所有服務都由zookeeper進行統一管理。client作為消費者,連接到注冊中心獲取相應的服務。
本文中消費者部分由nodejs編寫。


一、Provider服務提供者
這里首先對服務端進行改造。提供兩個接口,各一個方法。DemoService接口中提供一個方法,客戶端傳入String型參數,處理此參數后返回客戶端。IUserService接口中提供一個根據id查詢用戶信息的方法。方法實現同上篇文中介紹一樣不變。部分代碼如下:

DemoServiceImpl.java

package com.soyann.provider.demo.impl;

import com.soyann.provider.demo.DemoService;

/**
 * @ProjectName: soyann
 * @FileName: com.soyann.provider.demo.impl
 * @Description: (do what)
 * @Copyright: Copyright(C) 2016-2017 All rights Reserved
 * @Company: ShenZhen Information Technology Co.,LTD.
 * @Author: neil
 * @Version V1.0
 * @Date: 2017/11/17
 * 

* Modification History: * Date Author Version Discription * ----------------------------------------------------------------------------------- * 2017/11/17 neil 1.0 1.0 * Why & What is modified: <修改原因描述> */ public class DemoServiceImpl implements DemoService { @Override public String sayHello(String name) { return "Hello Dubbo,Hello " + name; } }

UserServiceImpl.java

package com.soyann.provider.user.service.impl;

import com.soyann.provider.user.mapper.UserMapper;
import com.soyann.provider.user.model.UserEntity;
import com.soyann.provider.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ProjectName: soyann
 * @FileName: com.soyann.business.user.service.impl
 * @Description: (do what)
 * @Copyright: Copyright(C) 2016-2017 All rights Reserved
 * @Company: ShenZhen Information Technology Co.,LTD.
 * @Author: dell657 neil
 * @Version V1.0
 * @Date: 2017/10/28
 * 

* Modification History: * Date Author Version Discription * ----------------------------------------------------------------------------------- * 2017/10/28 neil 1.0 1.0 * Why & What is modified: <修改原因描述> */ @Service("userService") public class UserServiceImpl implements IUserService { @Autowired(required=false) private UserMapper userMapper; @Override public UserEntity getUserById(int userId) { return userMapper.selectByPrimaryKey(userId); } @Override public List getAllUser() { return userMapper.getAllUser(); } }

spring-provider.xml



    
    
    
    
    
    


    
    
    
    


    
    
    
    


在spring-applicationContext.xml中,引入spring-provider.xml

添加服務啟動測試類TestProvider.java

package com.soyann.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * @ProjectName: soyann
 * @FileName: com.soyann.demo
 * @Description: (do what)
 * @Copyright: Copyright(C) 2016-2017 All rights Reserved
 * @Company: ShenZhen Information Technology Co.,LTD.
 * @Author: neil
 * @Version V1.0
 * @Date: 2017/11/17
 * 

* Modification History: * Date Author Version Discription * ----------------------------------------------------------------------------------- * 2017/11/17 neil 1.0 1.0 * Why & What is modified: <修改原因描述> */ public class TestProvider { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-applicationContext.xml", "spring/spring-mybatis.xml"); System.out.println(context.getDisplayName() + ": here"); context.start(); System.out.println("服務已經啟動..."); System.in.read(); } }

運行測試類,輸出以下信息:

……
18:45:48.299 [main] DEBUG ResultSet - {conn-10010, stmt-20003, rs-50003} Header: [installed_rank, version, description, type, script, checksum, installed_on, installed_by, execution_time, success]
18:45:48.299 [main] DEBUG ResultSet - {conn-10010, stmt-20003, rs-50003} closed
18:45:48.299 [main] DEBUG Statement - {conn-10010, stmt-20003} closed
18:45:48.299 [main] INFO  DbMigrate - Current version of schema `soyann`: 1.0.1
18:45:48.299 [main] WARN  DbMigrate - Schema `soyann` has version 1.0.1, but no migration could be resolved in the configured locations !
18:45:48.300 [main] DEBUG Statement - {conn-10010, pstmt-20004} created. SELECT RELEASE_LOCK("Flyway--294804349")
18:45:48.300 [main] DEBUG Statement - {conn-10010, pstmt-20004} Parameters : []
18:45:48.300 [main] DEBUG Statement - {conn-10010, pstmt-20004} Types : []
18:45:48.300 [main] DEBUG Statement - {conn-10010, pstmt-20004} executed. 0.414796 millis. SELECT RELEASE_LOCK("Flyway--294804349")
18:45:48.300 [main] DEBUG Statement - {conn-10010, pstmt-20004} clearParameters. 
18:45:48.300 [main] INFO  DbMigrate - Schema `soyann` is up to date. No migration necessary.
18:45:48.301 [main] DEBUG Connection - {conn-10010} setAutoCommit false
18:45:48.301 [main] DEBUG Connection - {conn-10010} commited
18:45:48.301 [main] DEBUG Connection - {conn-10010} setAutoCommit true
18:45:48.302 [main] DEBUG Connection - {conn-10010} pool-recycle
18:45:48.527 [main] DEBUG SqlSessionFactoryBean - Parsed configuration file: "class path resource [mybatis/mybatis-config.xml]"
18:45:48.674 [main] DEBUG SqlSessionFactoryBean - Parsed mapper file: "file [D:workspaceJAVAsoyannprovider	argetclassesmybatismappersqlmap-mapping-user.xml]"
org.springframework.context.support.ClassPathXmlApplicationContext@f2a0b8e: here
服務已經啟動...

這樣表示服務已經發布成功!(zookeeper注冊中心要先啟動,啟動方法略)

二、服務消費者部分
消費者部分采用node工程,這里也簡單創建了一個node工程進行連接實驗。目錄結構如下:

這里,必須的文件有三個文件,package.json,server.js,soyannClient-route.js

package.json

{
  "name": "nodeClient",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1",
    "build": "babel src -d lib"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cookie-parser": "^1.4.3",
    "express": "^4.16.2",
    "http": "^0.0.0",
    "js-to-java": "^2.4.0",
    "morgan": "^1.9.0",
    "node-zookeeper-dubbo": "^2.2.1"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-env": "^1.6.1"
  }
}

server.js

/**
 * @ProjectName: nodeClient
 * @FileName:
 * @Description: (服務主入口)
 * @Copyright: Copyright(C) 2016-2017 All rights Reserved
 * @Company: ShenZhen Information Technology Co.,LTD.
 * @Author: dell657 neil
 * @Version V1.0
 * @Date:  2017/11/17
 *
 * Modification  History:
 * Date         Author        Version        Discription
 * -----------------------------------------------------------------------------------
 * 2017/11/17     neil          1.0             1.0
 * Why & What is modified: <修改原因描述>
 */

const logger = require("morgan"),
    http = require("http"),
    express = require("express"),
    bodyParser = require("body-parser"),
    debug = require("debug")("mydebug:http"),
    cookieParser = require("cookie-parser");


const app = express();
//設置跨域訪問
app.all("*", function (req, res, next) {
    if(req.headers.origin == "http://localhost:4200" || req.headers.origin == "http://ng2.com"){
        res.header("Access-Control-Allow-Origin",req.headers.origin);   //設置跨域訪問
        res.header("Access-Control-Allow-Credentials", "true");
        res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        res.header("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With, Authorization");
        res.header("Content-Type", "application/json");
    }
    next();
});


app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

app.use(function (err, req, res, next) {
    if (err.name === "StatusError") {
        res.send(err.status, err.message);
    } else {
        next(err);
    }
});

/*引用模塊*/
app.use(require("./modules/soyannClient-route"));
//app.use(require("./modules/zkclient-route"));

/*監聽端口*/
const port = process.env.PORT || 8090;
http.createServer(app).listen(port, function (err) {
    console.log("Node服務已啟動,請訪問:http://localhost:" + port);
});

soyannClient-route.js

/**
 * @ProjectName: nodeClient
 * @FileName:
 * @Description: (do what)
 * @Copyright: Copyright(C) 2016-2017 All rights Reserved
 * @Company: ShenZhen Information Technology Co.,LTD.
 * @Author: dell657 neil
 * @Version V1.0
 * @Date:  2017/11/17
 *
 * Modification  History:
 * Date         Author        Version        Discription
 * -----------------------------------------------------------------------------------
 * 2017/11/17     neil          1.0             1.0
 * Why & What is modified: <修改原因描述>
 */
const express = require("express"),
    debug = require("debug")("mydebug:http"),
    zookeeperClient = require("node-zookeeper-dubbo");

const app = module.exports = express.Router();

const opt = {
    application: {name: "soyann-provider"},
    register: "localhost:2181",
    dubboVer: "2.5.7",
    root: "dubbo",
    dependencies: {
        Demo: {
            interface: "com.soyann.provider.demo.DemoService",
            version: "1.0.0",
            timeout: 6000,
            group: "g-demo",
            methodSignature: {
                sayHello: sayHello = (name) => (java) => [ java.String(name) ]
            }
        },
        Soyann: {
            interface: "com.soyann.provider.user.service.IUserService",
            version: "2.0.0",
            timeout: 6000,
            group: "g-soyann",
            methodSignature: {
                getUserById: getUserById = (id) => [ {"$class": "int", "$": id} ]
            }
        }
    }
};

opt.java = require("js-to-java");

const Dubbo = new zookeeperClient(opt);



/*傳參數實例*/
app.post("/api/sayHello",(req,res)=>{
    debug(req);
    Dubbo.Demo
        .sayHello(req.body.name)
        .then(data=>res.send(data))
        .catch(err=>res.send(err))
});

/*傳遞整形參數*/
app.post("/api/getUserById",(req,res)=>{
    Dubbo.Soyann
        .getUserById(Number(req.body.id))
        .then(data=>res.send(data))
        .catch(err=>res.send(err))
});


/*訪問實例*/
app.get("/api/demo",(req,res)=>{
    const result={"result":"Hello world Neil!"};
    debug(result);
    res.status(200).send(result);
});

這個工程中,使用了node-zookeeper-dubbo這個開源模塊,使得node與java通信成為現實。

啟動工程,后面不用再說了吧,本篇完結。

http://dubbo.io/

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/68047.html

相關文章

  • 哪種Scale out架構能更有效滿足布式計算?

    摘要:那么,的架構是否真的需要多臺小計算量的節點來實現才是最完美的架構呢,讓我們來細細剖析什么樣的架構才能最有效的滿足分布式計算的特點。近些年,隨著分布式計算的越來越火熱,Scale out分布式應用架構也如雨后春筍般不斷涌現,大到Big Data平臺架構,小到前端應用App的架構,似乎都要基于Scale out 的架構才算是與時俱進的先進架構。分布式架構的優勢顯而易見,一方面,Scale out...

    idisfkj 評論0 收藏0
  • 布式軟件架構整合(一)

    摘要:通過整合及可以實現數據庫查詢后將數據持久化。但是可能出現幻像讀這是花費最高代價但是最可靠的事務隔離級別。事務被處理為順序執行。 所需技術:spring、mybatis、druid、flyway、logback、nodejs、html、css3 ;目標:創建一個業務框架,后端采用spring+mybatis,中間層采用node,前端html5,css3等; showImg(https:/...

    mochixuan 評論0 收藏0
  • 云存儲主要技術路線選型比較

    摘要:云存儲主要技術路線有哪些各有哪些優缺點分享一存儲虛擬化存儲虛擬化更多是對傳統塊的虛擬化。也是云存儲的主流當家花旦。哪些應用場景適合云存儲?存儲虛擬化、分布式存儲、對象存儲這幾種技術主要解決什么問題?技術產品選型如何考慮? 企業哪些應用場景適合借助云存儲來實現? 傳統 IT 環境中使用傳統存儲的困境有那些?那些應用場景是傳統存儲不能滿足而必須借助云存儲來實現的? 分享一: ...

    zlyBear 評論0 收藏0
  • 墻裂推薦:搜云庫技術團隊,面試必備的技術干貨

    摘要:今天整理了一下近大半年以來的一些文章,和我的預期一樣,很多文章我都忘記自己曾經寫過了,這個記錄的過程讓我也有了新的理解。希望大家,收藏,點贊,加轉發。 今天整理了一下近大半年以來的一些文章,和我的預期一樣,很多文章我都忘記自己曾經寫過了,這個記錄的過程讓我也有了新的理解。希望大家,收藏,點贊,加轉發。 面試必備 面試必備:深入Spring MVC DispatchServlet 源碼...

    SegmentFault 評論0 收藏0
  • 墻裂推薦:搜云庫技術團隊,面試必備的技術干貨

    摘要:今天整理了一下近大半年以來的一些文章,和我的預期一樣,很多文章我都忘記自己曾經寫過了,這個記錄的過程讓我也有了新的理解。希望大家,收藏,點贊,加轉發。 今天整理了一下近大半年以來的一些文章,和我的預期一樣,很多文章我都忘記自己曾經寫過了,這個記錄的過程讓我也有了新的理解。希望大家,收藏,點贊,加轉發。 面試必備 面試必備:深入Spring MVC DispatchServlet 源碼...

    Neilyo 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<