![](https://i-blog.csdnimg.cn/blog_migrate/84da37bf7e6ae0ab1e4175152eea9097.png)
<context:property-placeholder location="classpath:properties/app.properties, classpath:properties/app-${deploy.env}.properties" ignore-resource-not-found="true" ignore-unresolvable="true" />
特别地,由于是运行时动态选择配置文件,所以需要将ignore-resource-not-found和ignore-unresolvable两项配置置为true,保证工程可以编译通过。这样的配置,有一个缺陷是,无法在编译时就能判断配置文件能否正确加载。因此,引入了前言部分提到的第二个工作项:运行参数的检查和初始化。
/** * 预设系统参数的监听器,在web容器创建{@link javax.servlet.ServletContext}时执行操作 * <p>该监听器配置在servlet应用的web.xml中,一般作为首个listener配置,务必放在 * {@link org.springframework.web.context.ContextLoaderListener}之前 * <p>通过读取环境变量中的deploy.env来生成环境标识,并存入到Java系统属性中 */ public class PropertyConfigurationListener implements ServletContextListener { /** 预先设置的环境类型,DEV,SIT,PROD等 */ public final static String DEPLOY_ENV_KEY = "deploy.env"; @Override public void contextInitialized(ServletContextEvent servletContextEvent) { this.resolveProfileValue(); } private void resolveProfileValue() { String profileValue = ""; if (SystemPropertyUtil.contains(DEPLOY_ENV_KEY)) { profileValue = SystemPropertyUtil.get(DEPLOY_ENV_KEY); } else { throw new IllegalStateException( "获取不到'" + DEPLOY_ENV_KEY + "'的值,检查是否在 '.profile'配置中配置或在JVM启动时加上参数'-D" + DEPLOY_ENV_KEY + "'。"); } SystemPropertyUtil.set(ACTIVE_PROFILES_PROPERTY_NAME, profileValue.toLowerCase()); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { //Do nothing! } }
值得一提的是,在resolveProfileValue方法的最后一行,设置了AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME的值为当前环境的配置值。这样做的目的是,应用很可能在不同的运行环境下,会使用spring-context.xml中的针对不同环境的配置项(<beans profile="" />) 。最后为了让PropertyConfigurationListener在启动之初执行,将其配置在web.xml的最前面,配置如下
<listener> <listener-class>profile.PropertyConfigurationListener</listener-class> </listener>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.pool.maxIdle}" /> <property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}" /> <property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}" /> <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /> <property name="maxTotal" value="${redis.pool.maxTotal}" /> <property name="testWhileIdle" value="${redis.pool.testWhileIdle}" /> </bean> <bean id="redisSentinelConfig" class="org.springframework.data.redis.connection.RedisSentinelConfiguration"> <constructor-arg name="propertySource" > <bean class="org.springframework.core.env.MapPropertySource"> <constructor-arg name="name" value="sentinelConfig" /> <constructor-arg name="source"> <map> <entry key="spring.redis.sentinel.master" value="${redis.master.name}" /> <entry key="spring.redis.sentinel.nodes" value="${redis.sentinel.nodes}" /> </map> </constructor-arg> </bean> </constructor-arg> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg name="sentinelConfig" ref="redisSentinelConfig" /> <constructor-arg name="poolConfig" ref="jedisPoolConfig" /> <property name="password" value="${redis.password}" /> </bean> <bean id="redisStringTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory" />
slaveof 192.168.80.33 6379
该配置说明,该从机监听IP和端口分别为192.168.80.33和6379的主机服务,从机服务上线之后会将其服务信息注册到主机。
sentinel monitor mymaster 192.168.80.33 6379 2
该配置说明,该Sentinel启动时首先会去监控IP和端口分别为192.168.80.33和6379的主机服务,2代表判断主节点服务失败至少需要2个节点。每一个Sentinal服务上线后,一方面会主动将自身的信息注册到主服务节点,另一方面也会动态地去获取主服务节点的已注册信息。因此,各个Sentinal节点可以感知到各种的存在,并且各个Sentinal节点都能获取到Redis主从服务的所有信息,并对主从服务节点进行监控。