brew install redis
brew services start redis
brew services stop redis
brew services restart redis
vi /usr/local/etc/redis.conf
requirepass <패스워드>
port <포트번호>
bind <허용할 IP>
Redisson 을 이용하여 remote service를 등록하고 불러 올때 사용하는 RemoteServiceInterface는 package 가 동일해야 해서 공통으로 사용할 모듈을 분리했다.
@EnableCaching
@Configuration
public class RedisConfig {
@Value("${spring.redis.host}")
String REDIS_HOST;
@Value("${spring.redis.port}")
String REDIS_PORT;
@Value("${spring.redis.password}")
String REDIS_PASSWORD;
@Bean
public RedissonClient redissonClient() {
Config redisConfig = new Config();
redisConfig.useSingleServer()
.setAddress(REDIS_HOST + ":" + REDIS_PORT)
.setPassword(REDIS_PASSWORD)
.setConnectionMinimumIdleSize(5)
.setConnectionPoolSize(5);
return Redisson.create(redisConfig);
}
}
public interface RemoteServiceInterface {
String sayHello(String name);
}
@Slf4j
@Component
public class RemoteService implements RemoteServiceInterface {
@Override
public String sayHello(String name) {
log.info("run sayHello");
return "hello " + name;
}
}
@Slf4j
@Component
@RequiredArgsConstructor
public class ApplicationStartup implements ApplicationListener<ApplicationReadyEvent> {
private final RedissonClient redissonClient;
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
log.info("onApplicationReady time={}", event.getTimestamp());
try {
registerRemoteService();
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
private void registerRemoteService() throws Exception {
log.info("RedissonClient config={}", redissonClient.getConfig());
RRemoteService remoteService = redissonClient.getRemoteService();
RemoteService remote = new RemoteService();
remoteService.register(RemoteServiceInterface.class, remote);
log.info("Remote service is registered!!");
}
}
실행 시 두 모듈을 스캔 해야 하므로 스캔할 패키지를 적어 준다.
@SpringBootApplication(scanBasePackages = {"com.example.remoteworker","com.example.remoteinterface" })
public class RemoteWorkerApplication {
public static void main(String[] args) {
SpringApplication.run(RemoteWorkerApplication.class, args);
}
}
@RestController
@RequiredArgsConstructor
public class TestController {
private final RedissonClient redissonClient;
@GetMapping("/hello")
public String callRemoteService() {
RemoteInvocationOptions remoteOptions = RemoteInvocationOptions.defaults().expectAckWithin(5000);
RRemoteService remoteService = redissonClient.getRemoteService();
RemoteServiceInterface service = remoteService.get(RemoteServiceInterface.class, remoteOptions);
return service.sayHello("kim");
}
}
buildscript {
ext {
springBootVersion = '2.5.3'
}
repositories {
mavenCentral()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
classpath "io.spring.gradle:dependency-management-plugin:1.0.4.RELEASE"
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group 'org.example'
version '1.0-SNAPSHOT'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
compileJava.options.encoding = 'UTF-8'
repositories {
mavenCentral()
}
dependencies {
compileOnly 'org.projectlombok:lombok'
implementation group: 'org.redisson', name: 'redisson-spring-boot-starter', version: '3.16.1'
compileOnly "io.netty:netty-resolver-dns-native-macos:4.1.59.Final:osx-x86_64"
annotationProcessor 'org.projectlombok:lombok'
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}
test {
useJUnitPlatform()
}
}
project(':remote-interface') {
bootJar.enabled = false
jar.enabled = true
dependencies {
}
}
project(':web') {
dependencies {
compileOnly project(':remote-interface')
implementation 'org.springframework.boot:spring-boot-starter-web'
}
}
project(':remote-worker') {
dependencies {
compileOnly project(':remote-interface')
}
}
spring:
redis:
host: redis://localhost
port: 6379
password: 1234
remote-worker 실행
web 실행
remote-worker