java開(kāi)源區(qū)塊鏈jdchain入門
前言
jdchain是京東數(shù)科開(kāi)源的區(qū)塊鏈平臺(tái),目標(biāo)是實(shí)現(xiàn)一個(gè)面向企業(yè)應(yīng)用場(chǎng)景的通用區(qū)塊鏈框架系統(tǒng),能夠作為企業(yè)級(jí)基礎(chǔ)設(shè)施,為業(yè)務(wù)創(chuàng)新提供高效、靈活和安全的解決方案。
之所以選擇jdchain研究是因?yàn)閖dchain是為數(shù)不多的底層也是采用java實(shí)現(xiàn)的一個(gè)區(qū)塊鏈平臺(tái)
項(xiàng)目地址:https://github.com/blockchain...
文檔地址:https://gitee.com/jdchain
部署組件
- peer:區(qū)塊鏈主節(jié)點(diǎn),參與共識(shí)、賬本操作等
- gateway:與Peer節(jié)點(diǎn)通信,負(fù)責(zé)區(qū)塊鏈瀏覽器及消息傳遞
- 客戶端:采用SDK和網(wǎng)關(guān)鏈接,通過(guò)網(wǎng)關(guān)發(fā)起交易
傻瓜式部署
獲取部署包
有兩種途徑可以拿到部署包,一、直接從官網(wǎng)下載.二、從github上拉源碼,然后自己構(gòu)建,在deployment目錄下會(huì)生成部署包,自己構(gòu)建的需要注意,如果是在Windows系統(tǒng)上構(gòu)建的包,包里的啟動(dòng)腳本在linux系統(tǒng)下運(yùn)行會(huì)有轉(zhuǎn)義字符的問(wèn)題,需要在assembly.xml里設(shè)置lineEnding為unix
效果預(yù)覽
區(qū)塊鏈部署工具
區(qū)塊鏈瀏覽器
部署遇到的問(wèn)題:
官方文檔算比較詳細(xì),但是很多細(xì)節(jié)沒(méi)有寫(xiě)明,一般體驗(yàn)和開(kāi)發(fā)時(shí)部署的環(huán)境比較簡(jiǎn)單,可能在一個(gè)主機(jī)上部署4個(gè)節(jié)點(diǎn)的peer。這個(gè)時(shí)候端口的編排要非常注意,因?yàn)閖dchain目前的共識(shí)算法采用的開(kāi)源的bftsmart實(shí)現(xiàn),共識(shí)端口在同一臺(tái)主機(jī)上不能連續(xù),不然就會(huì)端口沖突。博主就遇到了這個(gè)問(wèn)題,下面是詳細(xì)排錯(cuò)過(guò)程:采用SDK創(chuàng)建用戶時(shí)拋如下異常,網(wǎng)關(guān)可以正常連接,秘鑰認(rèn)證沒(méi)有問(wè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)致的,這部分實(shí)現(xiàn)采用了Apache-commons-pool2。實(shí)現(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); } }
這個(gè)代碼BinarySerializeUtils.deserialize(bftsmartClientSettings.getTopology())返回的是null,沒(méi)有正確拿到Bftsmart的網(wǎng)絡(luò)拓?fù)?。進(jìn)而查看peer節(jié)點(diǎn),發(fā)現(xiàn)只有節(jié)點(diǎn)0成功了,其他都拋如下異常,初步判斷bftsmart的副本服務(wù)因?yàn)槎丝谡加脝?dòng)失敗了:
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官方文檔得知,如果在同一個(gè)主機(jī)運(yùn)行,不能使用順序的端口號(hào),即一開(kāi)始編排的peer的共識(shí)端口,6000、6001、6002、6003是不行的,原因如下:如果在同一臺(tái)計(jì)算機(jī)(127.0.0.1)中部署/執(zhí)行了某些(或全部)副本,config/hosts.config則不能具有順序的端口號(hào)(例如10000、10001、10002、10003)。這是因?yàn)槊總€(gè)副本都綁定了兩個(gè)端口:一個(gè)用于接收來(lái)自客戶端的消息,另一個(gè)用于接收來(lái)自其他副本的消息(通過(guò)獲取下一個(gè)端口號(hào)選擇) 。更一般而言,如果為副本R分配了端口號(hào)P,它將嘗試將端口P(綁定到接收到的客戶端請(qǐng)求)和端口P + 1(綁定到其他副本)進(jìn)行綁定。如果不執(zhí)行此準(zhǔn)則,則副本可能無(wú)法綁定所有需要的端口。
結(jié)語(yǔ)
jdchain是完整采用java實(shí)現(xiàn)的區(qū)塊鏈項(xiàng)目,是java開(kāi)發(fā)者研究區(qū)塊鏈的一大福音,而且項(xiàng)目開(kāi)源后一直在迭代,文檔和社區(qū)支持方面都比較友好。入門雖然很簡(jiǎn)單,但是要深入到這個(gè)項(xiàng)目后面還有很多的東西要研究。樓主計(jì)劃,后面先把SDK和網(wǎng)關(guān)的交互搞清楚,然后在研究下最新的共識(shí)實(shí)現(xiàn)(基于RabbitMQ),然后在研究下智能合約的應(yīng)用,最后深入代碼實(shí)現(xiàn),可能后面還會(huì)有其他的關(guān)于jdchain的內(nèi)容輸出。最后希望全部掌握后能給社區(qū)貢獻(xiàn)點(diǎn)代碼、提供點(diǎn)案例、解答點(diǎn)問(wèn)題。為開(kāi)源盡自己的綿薄之力
以上就是java開(kāi)源區(qū)塊鏈jdchain入門的詳細(xì)內(nèi)容,更多關(guān)于java開(kāi)源區(qū)塊鏈jdchain的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java計(jì)時(shí)器StopWatch實(shí)現(xiàn)方法代碼實(shí)例
這篇文章主要介紹了Java計(jì)時(shí)器StopWatch實(shí)現(xiàn)方法代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07IDEA連接mysql數(shù)據(jù)庫(kù)報(bào)錯(cuò)的解決方法
這篇文章主要介紹了IDEA連接mysql數(shù)據(jù)庫(kù)報(bào)錯(cuò)的解決方法,文中有非常詳細(xì)的圖文示例,對(duì)出現(xiàn)Server returns invalid timezone. Go to ‘Advanced‘ tab and set ‘serverTimezone‘ prope報(bào)錯(cuò)的小伙伴們很有幫助喲,需要的朋友可以參考下2021-05-05零基礎(chǔ)如何系統(tǒng)的學(xué)習(xí)Java
這篇文章主要介紹了零基礎(chǔ)如何系統(tǒng)的學(xué)習(xí)Java,很多朋友糾結(jié)這個(gè)問(wèn)題,教材書(shū)不知道從何學(xué)起,今天小編給大家分享一篇教程幫助到家梳理這方面的知識(shí)2020-07-07淺談mybatisPlus的Ipage分頁(yè)和map參數(shù)的問(wèn)題
這篇文章主要介紹了mybatisPlus的Ipage分頁(yè)和map參數(shù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12java實(shí)現(xiàn)文件和base64相互轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了java如何實(shí)現(xiàn)文件和base64相互轉(zhuǎn)換,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11Java簡(jiǎn)單計(jì)算器的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Java簡(jiǎn)單計(jì)算器的實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12