redis - remote service
This commit is contained in:
@@ -30,7 +30,9 @@ subprojects {
|
||||
|
||||
dependencies {
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
implementation group: 'org.redisson', name: 'redisson', version: '3.16.1'
|
||||
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"
|
||||
@@ -44,14 +46,24 @@ subprojects {
|
||||
}
|
||||
}
|
||||
|
||||
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(':redis-server') {
|
||||
project(':remote-server') {
|
||||
dependencies {
|
||||
compileOnly project(':remote-interface')
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.example.redisserver;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.config.Config;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.example.redisserver;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class RedisServerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RedisServerApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.example.redisserver;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Component
|
||||
public class RemoteService implements RemoteServiceInterface {
|
||||
|
||||
@Override
|
||||
public void sayHello(String name) {
|
||||
|
||||
String url = "http://localhost:8080/hi/" + name;
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
String forObject = restTemplate.getForObject(url, String.class);
|
||||
System.out.println("forObject = " + forObject);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.example.redisserver;
|
||||
|
||||
public interface RemoteServiceInterface {
|
||||
|
||||
void sayHello(String name);
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.example.web;
|
||||
package com.example.remoteinterface.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.config.Config;
|
||||
@@ -9,11 +8,9 @@ import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
@Configuration
|
||||
public class RedisConfig {
|
||||
|
||||
@Value("${spring.redis.host}")
|
||||
String REDIS_HOST;
|
||||
|
||||
@@ -33,4 +30,5 @@ public class RedisConfig {
|
||||
.setConnectionPoolSize(5);
|
||||
return Redisson.create(redisConfig);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.example.remoteinterface.service;
|
||||
|
||||
public interface RemoteServiceInterface {
|
||||
|
||||
String sayHello(String name);
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.example.redisserver;
|
||||
package com.example.remoteserver;
|
||||
|
||||
import com.example.remoteserver.service.RemoteService;
|
||||
import com.example.remoteinterface.service.RemoteServiceInterface;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.redisson.api.RAtomicLong;
|
||||
import org.redisson.api.RRemoteService;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.api.RemoteInvocationOptions;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -33,5 +33,6 @@ public class ApplicationStartup implements ApplicationListener<ApplicationReadyE
|
||||
|
||||
RemoteService remote = new RemoteService();
|
||||
remoteService.register(RemoteServiceInterface.class, remote);
|
||||
log.info("Remote service is registered!!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.example.remoteserver;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication(scanBasePackages = {"com.example.remoteserver","com.example.remoteinterface" })
|
||||
public class RemoteServerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RemoteServerApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.example.remoteserver.service;
|
||||
|
||||
import com.example.remoteinterface.service.RemoteServiceInterface;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class RemoteService implements RemoteServiceInterface {
|
||||
|
||||
@Override
|
||||
public String sayHello(String name) {
|
||||
log.info("run sayHello");
|
||||
return "hello " + name;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,6 @@ server:
|
||||
|
||||
spring:
|
||||
redis:
|
||||
host: redis://127.0.0.1
|
||||
host: redis://localhost
|
||||
port: 6379
|
||||
password: 1234
|
||||
password: 1234
|
||||
@@ -1,4 +1,5 @@
|
||||
rootProject.name = 'redis'
|
||||
include 'web'
|
||||
include 'redis-server'
|
||||
include 'remote-server'
|
||||
include 'remote-interface'
|
||||
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.example.web;
|
||||
|
||||
public interface RemoteServiceInterface {
|
||||
|
||||
void sayHello(String name);
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.example.web;
|
||||
|
||||
import com.example.remoteinterface.service.RemoteServiceInterface;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.redisson.api.RRemoteService;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.api.RemoteInvocationOptions;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@@ -16,16 +16,10 @@ public class TestController {
|
||||
|
||||
@GetMapping("/hello")
|
||||
public String callRemoteService() {
|
||||
RemoteInvocationOptions remoteOptions = RemoteInvocationOptions.defaults().noAck().noResult();
|
||||
RemoteInvocationOptions remoteOptions = RemoteInvocationOptions.defaults().expectAckWithin(5000);
|
||||
RRemoteService remoteService = redissonClient.getRemoteService();
|
||||
RemoteServiceInterface service = remoteService.get(RemoteServiceInterface.class, remoteOptions);
|
||||
service.sayHello("kim");
|
||||
return "success";
|
||||
}
|
||||
|
||||
@GetMapping("/hi/{name}")
|
||||
public String receiveRemoteService(@PathVariable String name) {
|
||||
System.out.println("hello " + name);
|
||||
return "hi " + name;
|
||||
return service.sayHello("kim");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.example.web;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
@SpringBootApplication(scanBasePackages = {"com.example.web","com.example.remoteinterface" })
|
||||
public class WebApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -3,6 +3,6 @@ server:
|
||||
|
||||
spring:
|
||||
redis:
|
||||
host: redis://127.0.0.1
|
||||
host: redis://localhost
|
||||
port: 6379
|
||||
password: 1234
|
||||
Reference in New Issue
Block a user