Spring集成Jedis(不信赖spring-data-redis)(单机/集群方式)(待实践)

Docker环境布署见上壹篇博客:http://www.cnblogs.com/cnblogs-wangzhipeng/p/6994541.html。

Jedis是Redis的Java客户端,Spring将Jedis连接池作为三个Bean来安排。如若在Spring
Data的官网上得以窥见,Spring Data Redis已经将Jedis集成进去了。

咱们配备分布式容器服务后就要在地点陈设法布里c互联网,那时候大家的法布里c-sdk-node
就派上用场了,选拔orderer节点所在的主机实行fabric-sdk-node安排,但是在配备环境从前大家要先安装一些额外的nodejs包:

Jedis连接池分为三种:

Fabric-sdk-node部署

壹种是“redis.clients.jedis.ShardedJedisPool”,那是基于hash算法的壹种分布式集群Redis客户端连接池。

一>先安装nodejs
(近期法布里c供给的版本是陆.九.x的版本),再设置npm(nodejs包的管理工科具。)

另一种是“redis.clients.jedis.JedisPool”,这是单机环境适用的Redis连接池。

二> 执行命令 code bash:

下边是介绍详细的集成格局:

cd
$fabric-sdk-node 

POM:

npm install
#这之中注意,最佳是准备个阶梯,不然非常的大致率安装不成事,恐怕用cpm代替 npm(推荐)

    <!-- Redis依赖包 -->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>

叁>对sdk举办测试

ShardedJedisPool是Redis集群客户端的对象池,能够因而他来操作ShardedJedis,上面是ShardedJedisPool的XML配置,spring-jedis.xml:

npm install gulp

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 引入jedis的properties配置文件 -->
    <!--如果你有多个数据源需要通过<context:property-placeholder管理,且不愿意放在一个配置文件里,那么一定要加上ignore-unresolvable=“true"-->
    <context:property-placeholder location="classpath:properties/redis.properties" ignore-unresolvable="true" />

    <!--shardedJedisPool的相关配置-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!--新版是maxTotal,旧版是maxActive-->
        <property name="maxTotal">
            <value>${redis.pool.maxActive}</value>
        </property>
        <property name="maxIdle">
            <value>${redis.pool.maxIdle}</value>
        </property>
        <property name="testOnBorrow" value="true"/>
        <property name="testOnReturn" value="true"/>
    </bean>

    <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"  scope="singleton">
        <constructor-arg index="0" ref="jedisPoolConfig" />
        <constructor-arg index="1">
            <list>
                <bean class="redis.clients.jedis.JedisShardInfo">
                    <constructor-arg name="host" value="${redis.uri}" />
                </bean>
            </list>
        </constructor-arg>
    </bean>
</beans>

gulp ca

下边是单机环境下Redis连接池的配置:

gulp test-headless

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 引入jedis的properties配置文件 -->
    <!--如果你有多个数据源需要通过<context:property-placeholder管理,且不愿意放在一个配置文件里,那么一定要加上ignore-unresolvable=“true"-->
    <context:property-placeholder location="classpath:properties/redis.properties" ignore-unresolvable="true" />

    <!--Jedis连接池的相关配置-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!--新版是maxTotal,旧版是maxActive-->
        <property name="maxTotal">
            <value>${redis.pool.maxActive}</value>
        </property>
        <property name="maxIdle">
            <value>${redis.pool.maxIdle}</value>
        </property>
        <property name="testOnBorrow" value="true"/>
        <property name="testOnReturn" value="true"/>
    </bean>

    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="poolConfig" ref="jedisPoolConfig" />
        <constructor-arg name="host" value="${redis.host}" />
        <constructor-arg name="port" value="${redis.port}" type="int" />
        <constructor-arg name="timeout" value="${redis.timeout}" type="int" />
        <constructor-arg name="password" value="${redis.password}" />
        <constructor-arg name="database" value="${redis.database}" type="int" />
    </bean>
</beans>

全数测试都跑过才算ok

对应的classpath:properties/redis.properties为:

4>整体育项目检验试

#最大分配的对象数
redis.pool.maxActive=200
#最大能够保持idel状态的对象数
redis.pool.maxIdle=50
redis.pool.minIdle=10
redis.pool.maxWaitMillis=20000
#当池内没有返回对象时,最大等待时间
redis.pool.maxWait=300

#格式:redis://:[密码]@[服务器地址]:[端口]/[db index]
redis.uri = redis://:12345@127.0.0.1:6379/0

redis.host = 127.0.0.1
redis.port = 6379
redis.timeout=30000
redis.password = 12345
redis.database = 0

cd  test/fixtures/

两岸操作代码类似,都以先注入连接池,然后通过连接池获得Jedis实例,通超过实际例对象操作Redis。

docker-compose up
–force-recreate

ShardedJedis操作:

那时单机模拟条件已经运行

    @Autowired
    private ShardedJedisPool shardedJedisPool;//注入ShardedJedisPool

    @RequestMapping(value = "/demo_set",method = RequestMethod.GET)
    @ResponseBody
    public String demo_set(){
        //获取ShardedJedis对象
        ShardedJedis shardJedis = shardedJedisPool.getResource();
        //存入键值对
        shardJedis.set("key1","hello jedis");
        //回收ShardedJedis实例
        shardJedis.close();

        return "set";
    }

    @RequestMapping(value = "/demo_get",method = RequestMethod.GET)
    @ResponseBody
    public String demo_get(){
        ShardedJedis shardedJedis = shardedJedisPool.getResource();
        //根据键值获得数据
        String result = shardedJedis.get("key1");
        shardedJedis.close();

        return result;
    }

cd $fabric-sdk-node

Jedis操作:

gulp test

    @Autowired
    private JedisPool jedisPool;//注入JedisPool

    @RequestMapping(value = "/demo_set",method = RequestMethod.GET)
    @ResponseBody
    public String demo_set(){
        //获取ShardedJedis对象
        Jedis jedis = jedisPool.getResource();
        //存入键值对
        jedis.set("key2","hello jedis one");
        //回收ShardedJedis实例
        jedis.close();

        return "set";
    }

    @RequestMapping(value = "/demo_get",method = RequestMethod.GET)
    @ResponseBody
    public String demo_get(){
        Jedis jedis = jedisPool.getResource();
        //根据键值获得数据
        String result = jedis.get("key2");
        jedis.close();

        return result;
    }

享有测试都跑过才算ok

 

参考网站:https://github.com/luckydogchina/fabric-sdk-node/blob/v1.0.0-alpha.1/README.md

参考:

如上都ok了证实,你的fabric-sdk-node
sdk没反常。

http://www.cnblogs.com/red-code/p/6657517.html(以上内容转自此篇小说)

 

http://blog.csdn.net/javaloveiphone/article/details/52355180

法布里c网络安插

http://blog.csdn.net/w410589502/article/details/54341040

方法一:

http://blog.csdn.net/tomli2017/article/details/69929371

一>运行分布式环境:

http://www.cnblogs.com/hz-cww/p/6030504.html

以此实操见上1篇博客,笔者将fabric-sdk-node
和 fabric orderer以及Org一、Couhdb节点铺排在了
主机1九贰.168.一.拾肆上,Org二布署在了主机1玖贰.16八.①.121上。

http://blog.csdn.net/wang_keng/article/details/51753637

二>
大家要对法布里c-sdk-node的代码实行一下修改:

$fabric-sdk-node/test/integration/e2e/joinchannel.js

 

var Client = require('fabric-client');
var EventHub = require('fabric-client/lib/EventHub.js');
var Block = require('fabric-client/lib/Block.js');

var grpc = require('grpc');
var _commonProto = grpc.load(path.join(__dirname, '../../../fabric-client/lib/protos/common/common.proto')).common;
var _configtxProto = grpc.load(path.join(__dirname, '../../../fabric-client/lib/protos/common/configtx.proto')).common;

var testUtil = require('../../unit/util.js');

var the_user = null;
var tx_id = null;
var nonce = null;

Client.addConfigFile(path.join(__dirname, './config.json'));
var ORGS = Client.getConfigSetting('test-network');
//此处添加以下两行,192。168.1.121是另一个主机的ip,修改时请替换为实际主机的ip
ORGS.org2.peer1.requests = "grpcs://192.168.1.121:7051"
ORGS.org2.peer1.events = "grpcs://192.168.1.121:7053"

var allEventhubs = [];

//

 

$fabric-sdk-node/test/integration/e2e/e2eUtils.js

var hfc = require('fabric-client');
var EventHub = require('fabric-client/lib/EventHub.js');
var testUtil = require('../../unit/util.js');

var e2e = testUtil.END2END;
hfc.addConfigFile(path.join(__dirname, './config.json'));
var ORGS = hfc.getConfigSetting('test-network');

//添加以下代码,192.168.1.121为另一个主机的地址,请根据实际情况进行替换
ORGS.org2.peer1.requests = "grpcs://192.168.1.121:7051"
ORGS.org2.peer1.events = "grpcs://192.168.1.121:7053"

var tx_id = null;
var nonce = null;
var the_user = null;

修改后按如下顺序执行js脚本:

$fabric-sdk-node/test/integration/e2e/create-channel.js

$fabric-sdk-node/test/integration/e2e/join-channel.js

$fabric-sdk-node/test/integration/e2e/install-chaincode.js

$fabric-sdk-node/test/integration/e2e/instantiate-chaincode.js

即使都实施通过了,那么fabric的的互连网搭建和chaincode安装配置就曾经完毕。

 

行使Fbaric互连网执行交易

 与地方的改动类似,也是将容器所在的地方,替换到对应的地点(主机ip:本地映射端口号)。执行invoke-chaincode.js
和query-chaincode.js,就可进展览贸易易。

 

方法二:

除开上边的改动章程之外,还有3个最简易实用的章程,修改工程的配置文件:

图片 1

 

 把个中grpcs
的地址修改成容器对应的地址(具体端口是由docker-compose中的配置文件决定的,id是宿主机的位置ip):

图片 2

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注