spring整合redis

本文介绍如何在Spring框架中配置和使用Redis集群,包括定义Jedis池配置、创建集群节点及初始化JedisCluster实例,实现数据读写操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

redis集群使用的一台虚拟机模拟6台服务器。

applicationContext-redis.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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xsi:schemaLocation="
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	
	<!-- 加载数据 -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 配置数据源 -->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxIdle" value="${redis.maxIdle}"></property>
		<property name="maxTotal" value="${redis.maxTotal}"></property>
		<property name="maxWaitMillis" value="${redis.maxWaitMillis}"></property>
		<property name="testOnBorrow" value="${redis.testOnBorrow}"></property>
	</bean>
	
	<!-- redis集群配置 -->
	<bean id="host1" class="redis.clients.jedis.HostAndPort">
		<constructor-arg name="host" value="${redis.host}"></constructor-arg>
		<constructor-arg name="port" value="${redis1.port}"></constructor-arg>
	</bean>
	
	<bean id="host2" class="redis.clients.jedis.HostAndPort">
		<constructor-arg name="host" value="${redis.host}"></constructor-arg>
		<constructor-arg name="port" value="${redis2.port}"></constructor-arg>
	</bean>
	
	<bean id="host3" class="redis.clients.jedis.HostAndPort">
		<constructor-arg name="host" value="${redis.host}"></constructor-arg>
		<constructor-arg name="port" value="${redis3.port}"></constructor-arg>
	</bean>
	
	<bean id="host4" class="redis.clients.jedis.HostAndPort">
		<constructor-arg name="host" value="${redis.host}"></constructor-arg>
		<constructor-arg name="port" value="${redis4.port}"></constructor-arg>
	</bean>
	
	<bean id="host5" class="redis.clients.jedis.HostAndPort">
		<constructor-arg name="host" value="${redis.host}"></constructor-arg>
		<constructor-arg name="port" value="${redis5.port}"></constructor-arg>
	</bean>
	
	<bean id="host6" class="redis.clients.jedis.HostAndPort">
		<constructor-arg name="host" value="${redis.host}"></constructor-arg>
		<constructor-arg name="port" value="${redis6.port}"></constructor-arg>
	</bean>
	
	<bean id="redisCluster" class="redis.clients.jedis.JedisCluster">
		<constructor-arg name="nodes">
			<set>
				<ref bean="host1"/>
				<ref bean="host2"/>
				<ref bean="host3"/>
				<ref bean="host4"/>
				<ref bean="host5"/>
				<ref bean="host6"/>
			</set>
		</constructor-arg>
		<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
		<constructor-arg name="timeout" value="${redis.timeout}"></constructor-arg>
	</bean>
</beans>

配置文件db.properties

redis.host=192.168.159.140
redis1.port=7001
redis2.port=7002
redis3.port=7003
redis4.port=7004
redis5.port=7005
redis6.port=7006
redis.maxIdle=50
redis.maxTotal=100
redis.maxWaitMillis=1000
redis.testOnBorrow=true
redis.timeout=6000

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>redis-cluster</display-name>
  	<!-- 加载spring容器 -->
	<context-param>
	  <param-name>contextConfigLocation</param-name>
	  <param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<listener>
	  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

ClusterTest

package com.redis.test;

import java.util.HashSet;
import java.util.Set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author 刘喆
 * @date   2018年11月15日
 */
public class ClusterTest {
	//spring
	public static void main(String[] args) {
		ApplicationContext context = 
				new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		JedisCluster jedisCluster = (JedisCluster) context.getBean("redisCluster");
		jedisCluster.flushAll();
	    jedisCluster.set("name", "redis");
		System.out.println(jedisCluster.get("name"));
	}
	
	//编码
//	public static void main(String[] args) {
//		JedisPoolConfig poolConfig = new JedisPoolConfig();
//		poolConfig.setMaxIdle(50);
//		poolConfig.setMinIdle(30);
//		poolConfig.setMaxTotal(300);
//		poolConfig.setTestOnBorrow(true);
//		Set<HostAndPort> nodes = new HashSet<>();
//		HostAndPort host1 = new HostAndPort("192.168.159.140",7001);
//		nodes.add(host1);
//		HostAndPort host2 = new HostAndPort("192.168.159.140",7002);
//		nodes.add(host2);
//		HostAndPort host3 = new HostAndPort("192.168.159.140",7003);
//		nodes.add(host3);
//		HostAndPort host4 = new HostAndPort("192.168.159.140",7004);
//		nodes.add(host4);
//		HostAndPort host5 = new HostAndPort("192.168.159.140",7005);
//		nodes.add(host5);
//		HostAndPort host6 = new HostAndPort("192.168.159.140",7006);
//		nodes.add(host6); 
//		JedisCluster jedisCluster = new JedisCluster(nodes, 6000, poolConfig);
//		jedisCluster.set("name", "redis");
//		System.out.println(jedisCluster.get("name"));
//	}
}

运行结果

 十一月 16, 2018 9:25:17 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@617c74e5: startup date [Fri Nov 16 09:25:17 GMT+08:00 2018]; root of context hierarchy
十一月 16, 2018 9:25:17 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
十一月 16, 2018 9:25:18 上午 org.springframework.context.support.PropertySourcesPlaceholderConfigurer loadProperties
信息: Loading properties file from class path resource [db.properties]
redis

github:https://github.com/LiuZhe715718/redis-cluster 

 

 

 

 

在当今计算机视觉领域,深度学习模型在图像分割任务中发挥着关键作用,其中 UNet 是一种在医学影像分析、遥感图像处理等领域广泛应用的经典架构。然而,面对复杂结构和多尺度特征的图像,UNet 的性能存在局限性。因此,Nested UNet(也称 UNet++)应运而生,它通过改进 UNet 的结构,增强了特征融合能力,提升了复杂图像的分割效果。 UNet 是 Ronneberger 等人在 2015 年提出的一种卷积神经网络,主要用于生物医学图像分割。它采用对称的编码器 - 解码器结构,编码器负责提取图像特征,解码器则将特征映射回原始空间,生成像素级预测结果。其跳跃连接设计能够有效传递低层次的细节信息,从而提高分割精度。 尽管 UNet 在许多场景中表现出色,但在处理复杂结构和多尺度特征的图像时,性能会有所下降。Nested UNet 通过引入更深层次的特征融合来解决这一问题。它在不同尺度上建立了密集的连接路径,增强了特征的传递与融合。这种“嵌套”结构不仅保持了较高分辨率,还增加了特征学习的深度,使模型能够更好地捕获不同层次的特征,从而显著提升了复杂结构的分割效果。 模型结构:在 PyTorch 中,可以使用 nn.Module 构建 Nested UNet 的网络结构。编码器部分包含多个卷积层和池化层,并通过跳跃连接传递信息;解码器部分则包含上采样层和卷积层,并与编码器的跳跃连接融合。每个阶段的连接路径需要精心设计,以确保不同尺度信息的有效融合。 编码器 - 解码器连接:Nested UNet 的核心在于多层次的连接。通过在解码器中引入“skip connection blocks”,将编码器的输出与解码器的输入相结合,形成一个密集的连接网络,从而实现特征的深度融合。 训练与优化:训练 Nested UNet 时,需要选择合适的损失函数和优化器。对于图像分割任务,常用的损失
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值