yoursyun

nginx - centos9, redis - centos9, springboot3 - windows session clustering 본문

program/spring

nginx - centos9, redis - centos9, springboot3 - windows session clustering

yoursyun 2025. 3. 5. 13:56

1. nginx, redis install in centos 9

bash

     dnf update -y
     dnf install -y nginx
     systemctl start nginx
     systemctl enable nginx
     firewall-cmd --permanent --add-service=http
     firewall-cmd --permanent --add-service=https

     dnf install -y redis

     systemctl start redis
     systemctl enable redis

     firewall-cmd --permanent --add-port=6379/tcp

     firewall-cmd --reload

 

# sulinux가 활성화 되어 있다면 외부 host 와 통신이 안된다.

 

bash

     setsebool -P httpd_can_network_connect 1

     setsebool -P redis_enable_networking on

 

2. nginx setting ( bash vi /etc/nginx/nginx.conf )

http {
    upstream myapp1 {
        server 톰캣서버주소1:포트;
        server 톰캣서버주소2:포트;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
}

 

bash 

    systemctl restart nginx

3. redis setting ( bash : vi /etc/redis/redis.conf )

# 기본 local 에서만 접근이 가능하도록 구성되어 있으므로, 이를 수정하여 특정 아이피가 접근가능하도록 한다.

bind 127.0.0.1 -::1  

# 아래와 같이 수정한다.

bind 127.0.0.1 111.111.111.111 (was 서버아이피)

 

bash 

    systemctl restart redis

 

4. SpringBoot setting

build.gradle

// redis
implementation 'org.springframework.session:spring-session-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'

 

application.yml

spring:
  data:
    redis:
      host: 레디스서버아이피
      port: 6379

 

RedisConfig.java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@Configuration
@EnableRedisHttpSession // 세션을 Redis에 저장
public class RedisConfig {

    @Value("${spring.data.redis.host}")
    private String host;

    @Value("${spring.data.redis.port}")
    private int port;

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(host, port);
        return new LettuceConnectionFactory(config);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        return template;
    }
}

 

User Repository 는 직렬화가 가능하도록

private static final long serialVersionUID = 1L; 선언을 반드시 추가 한다.

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

@Getter
@Setter
public class myMember implements Serializable {
    private static final long serialVersionUID = 1L;
    private String username;
    private String password;    
}

 

* 추가

다른 호스트에서 레디스가 접근 거부 오류 발생시 아래 명령어를 통해 테스트 할수 있다. ( 6379 : 기본 포트 )

redis-cli download url ( for windows ) https://github.com/microsoftarchive/redis/releases/tag/win-3.2.100

 

bash

redis-cli -h 레디스서버아이피 -p 6379 ping

PONG

반응형