java開源區(qū)塊鏈jdchain入門
前言
jdchain是京東數(shù)科開源的區(qū)塊鏈平臺,目標(biāo)是實現(xiàn)一個面向企業(yè)應(yīng)用場景的通用區(qū)塊鏈框架系統(tǒng),能夠作為企業(yè)級基礎(chǔ)設(shè)施,為業(yè)務(wù)創(chuàng)新提供高效、靈活和安全的解決方案。
之所以選擇jdchain研究是因為jdchain是為數(shù)不多的底層也是采用java實現(xiàn)的一個區(qū)塊鏈平臺
項目地址:https://github.com/blockchain...
文檔地址:https://gitee.com/jdchain
部署組件
- peer:區(qū)塊鏈主節(jié)點,參與共識、賬本操作等
- gateway:與Peer節(jié)點通信,負責(zé)區(qū)塊鏈瀏覽器及消息傳遞
- 客戶端:采用SDK和網(wǎng)關(guān)鏈接,通過網(wǎng)關(guān)發(fā)起交易
傻瓜式部署
獲取部署包
有兩種途徑可以拿到部署包,一、直接從官網(wǎng)下載.二、從github上拉源碼,然后自己構(gòu)建,在deployment目錄下會生成部署包,自己構(gòu)建的需要注意,如果是在Windows系統(tǒng)上構(gòu)建的包,包里的啟動腳本在linux系統(tǒng)下運行會有轉(zhuǎn)義字符的問題,需要在assembly.xml里設(shè)置lineEnding為unix
效果預(yù)覽
區(qū)塊鏈部署工具
區(qū)塊鏈瀏覽器
部署遇到的問題:
官方文檔算比較詳細,但是很多細節(jié)沒有寫明,一般體驗和開發(fā)時部署的環(huán)境比較簡單,可能在一個主機上部署4個節(jié)點的peer。這個時候端口的編排要非常注意,因為jdchain目前的共識算法采用的開源的bftsmart實現(xiàn),共識端口在同一臺主機上不能連續(xù),不然就會端口沖突。博主就遇到了這個問題,下面是詳細排錯過程:采用SDK創(chuàng)建用戶時拋如下異常,網(wǎng)關(guān)可以正常連接,秘鑰認(rèn)證沒有問題:
Caused by: java.lang.IndexOutOfBoundsException: The accessing index is out of BytesSlice's bounds! at com.jd.blockchain.utils.io.BytesSlice.checkBoundary(BytesSlice.java:174) at com.jd.blockchain.utils.io.BytesSlice.getInt(BytesSlice.java:97) at com.jd.blockchain.utils.io.BytesSlice.getInt(BytesSlice.java:86) at com.jd.blockchain.binaryproto.impl.HeaderEncoder.resolveCode(HeaderEncoder.java:71) at com.jd.blockchain.binaryproto.BinaryProtocol.decode(BinaryProtocol.java:41) at com.jd.blockchain.sdk.converters.BinarySerializeResponseConverter.getResponse(BinarySerializeResponseConverter.java:33) at com.jd.blockchain.utils.http.agent.HttpServiceAgent.invoke(HttpServiceAgent.java:587) ... 7 more
網(wǎng)關(guān)里的異常
11:57:05.537 ERROR com.jd.blockchain.gateway.web.GatewayGlobalExceptionHandler 34 json - Unexpected exception occurred! --[RequestURL=[POST] http://192.168.1.190:8081/rpc/tx][class java.lang.IllegalStateException]Returned object not currently part of this pool java.lang.IllegalStateException: Returned object not currently part of this pool at org.apache.commons.pool2.impl.GenericObjectPool.returnObject(GenericObjectPool.java:530) ~[commons-pool2-2.5.0.jar!/:2.5.0] at com.jd.blockchain.consensus.bftsmart.client.BftsmartMessageService.sendOrderedMessage(BftsmartMessageService.java:46) ~[consensus-bftsmart-1.1.1.RELEASE.jar!/:1.1.1.RELEASE] at com.jd.blockchain.consensus.bftsmart.client.BftsmartMessageService.sendOrdered(BftsmartMessageService.java:22) ~[consensus-bftsmart-1.1.1.RELEASE.jar!/:1.1.1.RELEASE] at com.jd.blockchain.sdk.service.NodeSigningAppender.process(NodeSigningAppender.java:84) ~[sdk-base-1.1.1.RELEASE.jar!/:1.1.1.RELEASE] at com.jd.blockchain.sdk.service.PeerServiceProxy.process(PeerServiceProxy.java:89) ~[sdk-base-1.1.1.RELEASE.jar!/:1.1.1.RELEASE] at com.jd.blockchain.gateway.web.TxProcessingController.process(TxProcessingController.java:70) ~[gateway-1.1.1.RELEASE.jar!/:1.1.1.RELEASE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_231] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_231] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_231] at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_231]
最終查明異常是由于網(wǎng)關(guān)里創(chuàng)建AsynchServiceProxy失敗導(dǎo)致的,這部分實現(xiàn)采用了Apache-commons-pool2。實現(xiàn)如下:
public class BftsmartPeerProxyFactory extends BasePooledObjectFactory{ private BftsmartClientSettings bftsmartClientSettings; private int gatewayId; private AtomicInteger index = new AtomicInteger(1); public BftsmartPeerProxyFactory(BftsmartClientSettings bftsmartClientSettings, int gatewayId) { this.bftsmartClientSettings = bftsmartClientSettings; this.gatewayId = gatewayId; } @Override public AsynchServiceProxy create() throws Exception { BftsmartTopology topology = BinarySerializeUtils.deserialize(bftsmartClientSettings.getTopology()); MemoryBasedViewStorage viewStorage = new MemoryBasedViewStorage(topology.getView()); TOMConfiguration tomConfiguration = BinarySerializeUtils.deserialize(bftsmartClientSettings.getTomConfig()); //every proxy client has unique id; tomConfiguration.setProcessId(gatewayId + index.getAndIncrement()); AsynchServiceProxy peerProxy = new AsynchServiceProxy(tomConfiguration, viewStorage); return peerProxy; } @Override public PooledObjectwrap(AsynchServiceProxy asynchServiceProxy) { return new DefaultPooledObject<>(asynchServiceProxy); } }
這個代碼BinarySerializeUtils.deserialize(bftsmartClientSettings.getTopology())返回的是null,沒有正確拿到Bftsmart的網(wǎng)絡(luò)拓撲。進而查看peer節(jié)點,發(fā)現(xiàn)只有節(jié)點0成功了,其他都拋如下異常,初步判斷bftsmart的副本服務(wù)因為端口占用啟動失敗了:
11:36:48.479 ERROR bftsmart.tom.ServiceReplica 247 init - null java.net.BindException: 地址已在使用 at sun.nio.ch.Net.bind0(Native Method) ~[?:1.8.0_231] at sun.nio.ch.Net.bind(Net.java:433) ~[?:1.8.0_231] at sun.nio.ch.Net.bind(Net.java:425) ~[?:1.8.0_231] at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[?:1.8.0_231] at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:130) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:558) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1358) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:501) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:486) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:1019) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:254) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:366) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:446) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at java.lang.Thread.run(Thread.java:748) ~[?:1.8.0_231]
從bftsmart官方文檔得知,如果在同一個主機運行,不能使用順序的端口號,即一開始編排的peer的共識端口,6000、6001、6002、6003是不行的,原因如下:如果在同一臺計算機(127.0.0.1)中部署/執(zhí)行了某些(或全部)副本,config/hosts.config則不能具有順序的端口號(例如10000、10001、10002、10003)。這是因為每個副本都綁定了兩個端口:一個用于接收來自客戶端的消息,另一個用于接收來自其他副本的消息(通過獲取下一個端口號選擇) 。更一般而言,如果為副本R分配了端口號P,它將嘗試將端口P(綁定到接收到的客戶端請求)和端口P + 1(綁定到其他副本)進行綁定。如果不執(zhí)行此準(zhǔn)則,則副本可能無法綁定所有需要的端口。
結(jié)語
jdchain是完整采用java實現(xiàn)的區(qū)塊鏈項目,是java開發(fā)者研究區(qū)塊鏈的一大福音,而且項目開源后一直在迭代,文檔和社區(qū)支持方面都比較友好。入門雖然很簡單,但是要深入到這個項目后面還有很多的東西要研究。樓主計劃,后面先把SDK和網(wǎng)關(guān)的交互搞清楚,然后在研究下最新的共識實現(xiàn)(基于RabbitMQ),然后在研究下智能合約的應(yīng)用,最后深入代碼實現(xiàn),可能后面還會有其他的關(guān)于jdchain的內(nèi)容輸出。最后希望全部掌握后能給社區(qū)貢獻點代碼、提供點案例、解答點問題。為開源盡自己的綿薄之力
以上就是java開源區(qū)塊鏈jdchain入門的詳細內(nèi)容,更多關(guān)于java開源區(qū)塊鏈jdchain的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java計時器StopWatch實現(xiàn)方法代碼實例
這篇文章主要介紹了Java計時器StopWatch實現(xiàn)方法代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07IDEA連接mysql數(shù)據(jù)庫報錯的解決方法
這篇文章主要介紹了IDEA連接mysql數(shù)據(jù)庫報錯的解決方法,文中有非常詳細的圖文示例,對出現(xiàn)Server returns invalid timezone. Go to ‘Advanced‘ tab and set ‘serverTimezone‘ prope報錯的小伙伴們很有幫助喲,需要的朋友可以參考下2021-05-05零基礎(chǔ)如何系統(tǒng)的學(xué)習(xí)Java
這篇文章主要介紹了零基礎(chǔ)如何系統(tǒng)的學(xué)習(xí)Java,很多朋友糾結(jié)這個問題,教材書不知道從何學(xué)起,今天小編給大家分享一篇教程幫助到家梳理這方面的知識2020-07-07淺談mybatisPlus的Ipage分頁和map參數(shù)的問題
這篇文章主要介紹了mybatisPlus的Ipage分頁和map參數(shù)的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12java實現(xiàn)文件和base64相互轉(zhuǎn)換
這篇文章主要為大家詳細介紹了java如何實現(xiàn)文件和base64相互轉(zhuǎn)換,文中的示例代碼講解詳細,具有一定的參考價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11