当前位置: 首页 > 知识库问答 >
问题:

刷新重装盘

邰德业
2023-03-14

我正在使用spring quartz job连接到Redis来执行操作。我已经将RedisTemplate配置为只连接到主节点。

package com.XXX.XXX;

import org.quartz.JobDetail;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;

import de.chandre.quartz.spring.AutowiringSpringBeanJobFactory;
import de.chandre.quartz.spring.QuartzSchedulerAutoConfiguration;

@SpringBootApplication(exclude = { QuartzSchedulerAutoConfiguration.class })
public class OrderProcessorApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderProcessorApplication.class, args);
    }

    @Bean
    public JobDetailFactoryBean jobDetail() {
        JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
        jobDetailFactory.setJobClass(OrderProcessorJob.class);
        jobDetailFactory.setDurability(true);
        return jobDetailFactory;
    }

    @Value("${job.interval}")
    private int interval;

    @Bean
    public SimpleTriggerFactoryBean trigger(JobDetail job) {
        SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
        trigger.setJobDetail(job);
        trigger.setRepeatInterval(interval);
        trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
        return trigger;
    }

    @Bean
    public SchedulerFactoryBean scheduler(Trigger trigger, JobDetail job) {
        SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
        schedulerFactory.setConfigLocation(new ClassPathResource("quartz.properties"));

        schedulerFactory.setJobFactory(springBeanJobFactory());
        schedulerFactory.setJobDetails(job);
        schedulerFactory.setTriggers(trigger);
        return schedulerFactory;
    }

    @Autowired
    private ApplicationContext ap;

    @Bean
    @Primary
    public SpringBeanJobFactory springBeanJobFactory() {
        AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
        jobFactory.setApplicationContext(ap);
        return jobFactory;
    }

    // @Bean
    // @Qualifier("sentinel")
    // public RedisTemplate<String, Object> createRedisTemplate() {
    // RedisSentinelConfiguration sc = new RedisSentinelConfiguration()
    // .master("redis-cluster")
    // .sentinel("127.0.0.1", 26179)
    // .sentinel("127.0.0.1", 26381);
    //
    // JedisConnectionFactory jcf = new JedisConnectionFactory(sc);
    // jcf.setUsePool(true);
    // jcf.afterPropertiesSet();
    //
    // RedisTemplate rt = new RedisTemplate();
    // rt.setConnectionFactory(jcf);
    // rt.afterPropertiesSet();
    // return rt;
    // }

    @Bean
    @Primary
    public RedisTemplate<String, Object> createMasterOnlyRedisTemplate() {
        JedisConnectionFactory jcf = new JedisConnectionFactory();
        jcf.setHostName("127.0.0.1");
        jcf.setPort(6379);
        jcf.setUsePool(true);
        jcf.afterPropertiesSet();

        RedisTemplate rt = new RedisTemplate();
        rt.setConnectionFactory(jcf);
        rt.afterPropertiesSet();
        return rt;
    }
}
package com.store.platform;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.kohls.store.platform.common.model.ui.UpdateRedisMaster;

@RestController
@RequestMapping(path = "/refresh", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public class Test {

    @Autowired
    private RedisTemplate<String, Object> rt;

    @RequestMapping(method = RequestMethod.POST)
    public void refresh(@RequestBody UpdateRedisMaster um) {
        JedisConnectionFactory jcf = (JedisConnectionFactory) rt.getConnectionFactory();
        jcf.setHostName(um.getIp());
        jcf.setPort(um.getPort());
        jcf.setShardInfo(null);
        jcf.afterPropertiesSet();
    }
}
org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
    at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:204) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
    at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:348) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
    at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:129) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
    at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:92) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
    at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:79) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:194) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:169) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
    at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:91) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
    at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:182) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
    at com.XXX.platform.OrderProcessorJob.execute(OrderProcessorJob.java:32) ~[classes/:na]
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[quartz-2.3.0.jar:na]
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.3.0.jar:na]
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
    at redis.clients.util.Pool.getResource(Pool.java:53) ~[jedis-2.9.0.jar:na]
    at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226) ~[jedis-2.9.0.jar:na]
    at redis.clients.jedis.JedisPool.getResource(JedisPool.java:16) ~[jedis-2.9.0.jar:na]
    at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:194) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
    ... 11 common frames omitted
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused)
    at redis.clients.jedis.Connection.connect(Connection.java:207) ~[jedis-2.9.0.jar:na]
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93) ~[jedis-2.9.0.jar:na]
    at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1767) ~[jedis-2.9.0.jar:na]
    at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:106) ~[jedis-2.9.0.jar:na]
    at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:868) ~[commons-pool2-2.4.2.jar:2.4.2]
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:435) ~[commons-pool2-2.4.2.jar:2.4.2]
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363) ~[commons-pool2-2.4.2.jar:2.4.2]
    at redis.clients.util.Pool.getResource(Pool.java:49) ~[jedis-2.9.0.jar:na]
    ... 14 common frames omitted
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_121]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_121]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_121]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_121]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_121]
    at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_121]
    at redis.clients.jedis.Connection.connect(Connection.java:184) ~[jedis-2.9.0.jar:na]
    ... 21 common frames omitted

我希望获得一致的数据,这就是为什么我希望使用master进行读写,使用slave进行冗余和故障转移。

两天来我一直在思考这个问题。任何帮助都是非常感谢的。

共有1个答案

刘英彦
2023-03-14

我能够通过创建自己的RedisconnectionFactory来解决我的问题。

public class SPRedisConnectionFactory implements InitializingBean, RedisConnectionFactory, DisposableBean {

    private Jedis jedis;

    private String[] nodes;

    private Map<String, String[]> nodeMap;

    @Value("${master.node}")
    private String masterNode;

    @Override
    public void afterPropertiesSet() throws Exception {
        nodeMap = Arrays.stream(this.nodes)
                .map(n -> n.split(":"))
                .collect(toMap(n -> n[0] + ":" + n[1], n -> n));
        System.out.println(nodeMap);
    }

    @Override
    public RedisConnection getConnection() {
        String[] master = nodeMap.get(masterNode);
        jedis = new Jedis(master[0], parseInt(master[1]), 2000);
        jedis.connect();

        JedisConnection c = new JedisConnection(jedis);
        return c;
    }

    @Override
    public RedisClusterConnection getClusterConnection() {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean getConvertPipelineAndTxResults() {
        return false;
    }

    @Override
    public RedisSentinelConnection getSentinelConnection() {
        throw new UnsupportedOperationException();
    }

    @Override
    public DataAccessException translateExceptionIfPossible(RuntimeException e) {
        return null;
    }

    @Override
    public void destroy() throws Exception {
        jedis.disconnect();
    }
}

从使用Spring Data Redis连接到多个Redis服务器中得到了这个想法。

 类似资料:
  • 如何使用Javascript重新加载或刷新剑道网格? 通常需要在某个时间或用户操作之后重新加载或刷新网格。

  • 问题内容: 这是一个非常基本的问题-但我无法通过在线搜索找到答案。 我正在使用python控制ArcGIS,并且有一个简单的python脚本,该脚本调用了一些预先编写的代码。 但是,当我对预写代码进行更改时,它似乎没有导致任何更改。我导入了此模块,并尝试刷新它,但是没有任何反应。 我什至将它调用的文件移到了另一个位置,脚本仍然可以正常工作。昨天我做的一件事是我将所有python文件都添加到sys路

  • 编辑:当我拖动边框时,某种刷新被发送,我需要弄清楚并手动发送相同的刷新。 请注意,我已经尝试使用revalidate()和repaint()。 当使用JFrame和JPanel来显示一个框架时,我试图使框架的大小易于改变。 null 此外,一个重要的注意事项是,当您拖动边框时,白条会消失,并正确地重新加载/刷新内容(即使您只是少量拖动)

  • 问题内容: 如果将新文档索引到Elasticsearch索引,则可在索引操作后1秒钟左右搜索新文档。但是,可以通过调用或对索引进行操作来强制使该文档可立即搜索。这两个操作之间有什么区别- 结果似乎对他们来说是相同的,可以立即搜索文档。 这些操作中的每一项到底是什么? ES文档似乎并未深入解决此问题。 问题答案: 您得到的答案是正确的,但我认为值得详细说明。 刷新有效地调用了Lucene索引读取器上

  • 问题内容: 我正在尝试实现一些功能,即单击屏幕上的按钮将导致我的[QuerydataTable刷新(因为自创建dataTable以来服务器端数据源可能已更改)。 这是我所拥有的: 但是当我运行它时,它什么也没做。单击按钮时刷新dataTable的正确方法是什么?提前致谢! 问题答案: 您可以尝试以下方法:

  • 问题内容: 我正在使用Java进行项目,因此需要在单击特定按钮后重新加载整个程序。这该怎么做? 问题答案: 尝试 如果仍然无法使用,请在完成上述步骤后尝试