update: 从application中读取配置, 1.内部redis启用 2.内部es启用

V0.5.x
jay 2024-02-26 15:02:01 +08:00
parent 3ab219c182
commit debbc8e836
4 changed files with 45 additions and 31 deletions

View File

@ -9,8 +9,6 @@
*/
package cc.iotkit;
import cc.iotkit.config.EmbeddedElasticSearchConfig;
import cc.iotkit.config.EmbeddedRedisConfig;
import com.gitee.starblues.loader.DevelopmentMode;
import com.gitee.starblues.loader.launcher.SpringBootstrap;
import com.gitee.starblues.loader.launcher.SpringMainBootstrap;
@ -31,14 +29,6 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
public class Application implements SpringBootstrap {
public static void main(String[] args) {
// System.setProperty("disabledEmbeddedEs", "true");
// System.setProperty("disabledEmbeddedRedis", "true");
if (EmbeddedElasticSearchConfig.embeddedEnable()) {
EmbeddedElasticSearchConfig.startEmbeddedElasticSearch();
}
if (EmbeddedRedisConfig.embeddedEnable()) {
EmbeddedRedisConfig.startEmbeddedRedisServer();
}
SpringMainBootstrap.launch(Application.class, args);
log.info("server start success!");

View File

@ -14,36 +14,41 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.InternalSettingsPreparer;
import org.elasticsearch.node.Node;
import org.elasticsearch.transport.Netty4Plugin;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Collections;
@Component
public class EmbeddedElasticSearchConfig {
static {
System.setProperty("es.set.netty.runtime.available.processors", "false");
}
@Value("${spring.elasticsearch.embedded.enabled}")
private boolean embeddedElasticsearchEnabled;
public static boolean embeddedEnable() {
try {
Class.forName("cc.iotkit.temporal.es.config.ElasticsearchConfiguration");
} catch (ClassNotFoundException e) {
return false;
@Value("${spring.elasticsearch.embedded.port}")
private Integer esPort;
@PostConstruct
private void start(){
if (embeddedElasticsearchEnabled) {
startEmbeddedElasticSearch(esPort);
}
return !"true".equals(System.getProperty("disabledEmbeddedEs"));
}
@SneakyThrows
public static void startEmbeddedElasticSearch() {
EmbeddedElasticSearch embeddedElasticSearch = new EmbeddedElasticSearch(new ConfigProperty());
public void startEmbeddedElasticSearch(Integer port) {
EmbeddedElasticSearch embeddedElasticSearch = new EmbeddedElasticSearch(new ConfigProperty(), port);
embeddedElasticSearch.start();
}
public static class ConfigProperty {
public class ConfigProperty {
public Settings.Builder applySetting(Settings.Builder settings) {
String dataPath = "./data/elasticsearch";
String homePath = "./";
int port = 9200;
int port = esPort;
String host = "0.0.0.0";
return settings.put("network.host", host)
.put("http.port", port)
@ -56,7 +61,7 @@ public class EmbeddedElasticSearchConfig {
public static class EmbeddedElasticSearch extends Node {
@SneakyThrows
public EmbeddedElasticSearch(ConfigProperty properties) {
public EmbeddedElasticSearch(ConfigProperty properties, Integer port) {
super(InternalSettingsPreparer.prepareEnvironment(
properties.applySetting(
Settings.builder()
@ -65,7 +70,7 @@ public class EmbeddedElasticSearchConfig {
.put("transport.type", Netty4Plugin.NETTY_TRANSPORT_NAME)
.put("http.type", Netty4Plugin.NETTY_HTTP_TRANSPORT_NAME)
.put("network.host", "0.0.0.0")
.put("http.port", 9200)
.put("http.port", port)
).build(), Collections.emptyMap(), null, () -> "default"),
Collections.singleton(Netty4Plugin.class), false);
}

View File

@ -10,26 +10,39 @@
package cc.iotkit.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import redis.embedded.RedisServer;
import javax.annotation.PostConstruct;
@Slf4j
@Component
public class EmbeddedRedisConfig {
public static boolean embeddedEnable() {
return !"true".equals(System.getProperty("disabledEmbeddedRedis"));
}
@Value("${spring.redis.embedded.enabled}")
private boolean embeddedRedisEnabled;
@Value("${spring.redis.port}")
private Integer redisPort;
public static void startEmbeddedRedisServer() {
@PostConstruct
private void start(){
if (embeddedRedisEnabled) {
startEmbeddedRedisServer(redisPort);
}
}
public static void startEmbeddedRedisServer(Integer port) {
RedisServer redisServer;
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("windows")) {
redisServer = RedisServer.builder().setting("maxheap 200m")
.port(6378)
.port(port)
.setting("bind localhost")
.build();
} else {
redisServer = RedisServer.builder()
.port(6378)
.port(port)
.setting("bind localhost")
.build();
}

View File

@ -87,6 +87,10 @@ spring:
#<<================es时序数据配置开始===============
elasticsearch:
embedded:
# 是否启用内部es
enabled: true
port: 9200
rest:
#使用内置es的配置
#uris: http://elasticsearch:9200
@ -115,6 +119,8 @@ spring:
redis:
#使用内置redis的配置
embedded:
enabled: true
#host: redis
host: 127.0.0.1
port: 6378