这是demo的第三步,第二步完成了dubbo的redis注册中心的cluster,第三步接入redis cache,并且用jedis封装的sentinel做集群及主从故障切换。
tip:本次最麻烦的地方是jar包管理。所以一定要注意jar包的版本号
依赖:jedis-2.9.2 替换掉之前的jedis2.0.0
spring-data-redis-1.8.4.RELEASE
这次遇到一个jar包间依赖的冲突问题,jedis-2.9.2的包需要common-pool2,而dubbo-2.5.3需要common-pool,所以改了一下dubbo的源码,下面会上传改完的class文件。
这里为什么不用jedis-2.0.0,因为spring-data-redis不支持jedis的低版本,会报错。
下面上代码。
一、dubbo-demo-service
1.1 代码结构
1.2 pom文件:新增加了 spring-data-redis-1.8.4.RELEASE
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.liang</groupId>
<artifactId>dubbo-common-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>com.liang</groupId>
<artifactId>dubbo-demo-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.liang</groupId>
<artifactId>dubbo-common-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>com.liang</groupId>
<artifactId>dubbo-demo-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
1.3 配置文件
新增配置文件: spring-stringredistemplat.xml
内容:
<?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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"
xmlns:p="http://www.springframework.org/schema/p">
<context:component-scan base-package="com.liang"/>
<context:property-placeholder location="classpath:redis.properties"/>
<!--池属性配置-->
<bean id="jedisPoolConfig1" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="30"/>
<property name="maxIdle"
value="${redis.pool.maxIdle}"/>
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
</bean>
<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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"
xmlns:p="http://www.springframework.org/schema/p">
<context:component-scan base-package="com.liang"/>
<context:property-placeholder location="classpath:redis.properties"/>
<!--池属性配置-->
<bean id="jedisPoolConfig1" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="30"/>
<property name="maxIdle"
value="${redis.pool.maxIdle}"/>
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
</bean>
<bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<property name="master">
<bean class="org.springframework.dat
<property name="master">
<bean class="org.springframework.dat