Преобразование универсального c кода Redis в Azure Кэш для Redis - PullRequest
0 голосов
/ 18 февраля 2020

У меня есть следующий код Spring Session:

@Bean
public LettuceConnectionFactory connectionFactory() {
    String hostName = AcmeProperty.getProperty("spring.redis.host", "localhost");
    int port = Integer.parseInt(AcmeProperty.getProperty("spring.redis.port", "6379"));
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(hostName, port);

    return new LettuceConnectionFactory(redisStandaloneConfiguration);
}

Он прекрасно работает со следующими настройками, когда я запускаю redis локально:

spring.redis.host=localhost
spring.redis.port=6379

Однако, когда я использую Azure Кэш для Redis со следующей настройкой:

spring.redis.host=acmedev.redis.cache.windows.net
spring.redis.port=6380

Я получаю это предупреждение, и приложение зависает:

WARN (org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration:166) || - Unable to obtain SessionCookieConfig: Section 4.4 of the Servlet 3.0 specification does not permit this method to be called from a ServletContextListener that was not defined in web.xml, a web-fragment.xml file nor annotated with @WebListener

Что мне нужно изменить, чтобы заставить его работать с Azure кеш для редиса?

Я пробовал этот код , но происходит то же самое:

@Bean
public JedisConnectionFactory connectionFactory() {
    String hostName = AcmeProperty.getProperty("spring.redis.host", "localhost");
    int port = Integer.parseInt(AcmeProperty.getProperty("spring.redis.port", "6379"));
    String password = AcmeProperty.getProperty("spring.redis.password");

    SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLParameters sslParameters = new SSLParameters();
    sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
    sslParameters.setProtocols(new String[]{"TLSv1.2"});

    String uriStr = String.format("rediss://%s:%s", hostName, port);
    URI uri = URI.create(uriStr);
    JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, null);

    shardInfo.setPassword(password);

    return new JedisConnectionFactory(shardInfo);
}

1 Ответ

0 голосов
/ 19 февраля 2020

Вам необходимо использовать XML конфигурацию Spring; Java конфигурация не будет работать. См. «Невозможно получить SessionCookieConfig» .

<util:constant id="configureRedisAction"
               static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>

<context:annotation-config/>
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"
      p:configureRedisAction-ref="configureRedisAction"/>

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxTotal" value="200" />
    <property name="maxIdle" value="50" />
    <property name="maxWaitMillis" value="30000" />
    <property name="minIdle" value="10"/>
</bean>

<bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <property name="hostName" value="${spring.redis.host}" />
    <property name="port" value="${spring.redis.port}" />
    <property name="poolConfig" ref="jedisPoolConfig" />
    <property name="usePool" value="true" />
    <property name="useSsl" value="${spring.redis.ssl}"/>
    <property name="password" value="${spring.redis.password}"/>
</bean>
...