spring整合redis

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 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在MATLAB中,NURBS(非均匀有理B样条)是一种强大的数学工具,用于表示和处理复杂的曲线和曲面。NURBS在计算机图形学、CAD(计算机辅助设计)、CAM(计算机辅助制造)等领域有着广泛的应用。下面将详细探讨MATLAB中NURBS的绘制方法以及相关知识点。 我们需要理解NURBS的基本概念。NURBS是B样条(B-Spline)的一种扩展,其特殊之处在于引入了权重因子,使得曲线和曲面可以在不均匀的参数空间中进行平滑插值。这种灵活性使得NURBS在处理非均匀数据时尤为有效。 在MATLAB中,可以使用`nurbs`函数创建NURBS对象,它接受控制点、权值、 knot向量等参数。控制点定义了NURBS曲线的基本形状,而knot向量决定了曲线的平滑度和分布。权值则影响曲线通过控制点的方式,大的权值会使曲线更靠近该点。 例如,我们可以使用以下代码创建一个简单的NURBS曲线: ```matlab % 定义控制点 controlPoints = [1 1; 2 2; 3 1; 4 2]; % 定义knot向量 knotVector = [0 0 0 1 1 1]; % 定义权值(默认为1,如果未指定) weights = ones(size(controlPoints,1),1); % 创建NURBS对象 nurbsObj = nurbs(controlPoints, weights, knotVector); ``` 然后,我们可以用`plot`函数来绘制NURBS曲线: ```matlab plot(nurbsObj); grid on; ``` `data_example.mat`可能包含了一个示例的NURBS数据集,其中可能包含了控制点坐标、权值和knot向量。我们可以通过加载这个数据文件来进一步研究NURBS的绘制: ```matlab load('data_example.mat'); % 加载数据 nurbsData = struct2cell(data_example); % 转换为cell数组 % 解析数据 controlPoints = nurbsData{1}; weights = nurbsData{2}; knotVector = nurbsData{3}; % 创建并绘制NURBS曲线 nurbsObj = nurbs(controlPoints, weights, knotVector); plot(nurbsObj); grid on; ``` MATLAB还提供了其他与NURBS相关的函数,如`evalnurbs`用于评估NURBS曲线上的点,`isoparm`用于生成NURBS曲面上的等参线,以及`isocurve`用于在NURBS曲面上提取特定参数值的曲线。这些工具对于分析和操作NURBS对象非常有用。 MATLAB中的NURBS功能允许用户方便地创建、编辑和可视化复杂的曲线和曲面。通过对控制点、knot向量和权值的调整,可以精确地控制NURBS的形状和行为,从而满足各种工程和设计需求。通过深入理解和熟练掌握这些工具,可以在MATLAB环境中实现高效的NURBS建模和分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值