From 6e3552c86eb87d47ddcfd2a7d5a3a763d634b3d1 Mon Sep 17 00:00:00 2001 From: gaojie Date: Wed, 27 Feb 2019 12:26:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E5=8F=91=E8=B4=A7=E6=9C=BA=E7=9A=84=E5=86=85=E7=BD=91ip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/jmfy/Application.java | 7 +++- .../com/jmfy/server/ServerConfiguration.java | 21 +++++++++++ .../com/jmfy/server/ServerProperties.java | 37 +++++++++++++++++++ src/main/java/com/jmfy/util/JsonUtil.java | 12 ------ src/main/java/com/jmfy/util/RedisUtil.java | 5 +++ src/main/resources/application.properties | 3 +- 6 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/jmfy/server/ServerConfiguration.java create mode 100644 src/main/java/com/jmfy/server/ServerProperties.java diff --git a/src/main/java/com/jmfy/Application.java b/src/main/java/com/jmfy/Application.java index 6487ccc..1930b5d 100644 --- a/src/main/java/com/jmfy/Application.java +++ b/src/main/java/com/jmfy/Application.java @@ -4,6 +4,8 @@ import com.jmfy.Handler.BaseHandler; import com.jmfy.Handler.ManagerManager; import com.jmfy.controller.ManagerController; import com.jmfy.redisProperties.RedisAutoConfiguration; +import com.jmfy.server.ServerConfiguration; +import com.jmfy.server.ServerProperties; import com.jmfy.thrift.pool.ThriftPoolUtils; import com.jmfy.util.JsonUtil; import com.jmfy.util.RedisUtil; @@ -25,13 +27,16 @@ public class Application { //初使化连接池util类 ThriftPoolUtils.getInstance().initContext(configurableApplicationContext); + ServerConfiguration serverConfiguration = configurableApplicationContext.getBean(ServerConfiguration.class); + ServerProperties serverProperties = serverConfiguration.getServerProperties(); // ManagerManager handlerManager = ManagerManager.getInstance(); // KProducer kProducer = configurableApplicationContext.getBean(KProducer.class); // KProducer.init(); //redis初始化 RedisAutoConfiguration redisAutoConfiguration = configurableApplicationContext.getBean(RedisAutoConfiguration.class); RedisUtil.getInstence().init(redisAutoConfiguration.getRedisProperties()); - JsonUtil.getInstence().initServerIPandProt(); + + RedisUtil.getInstence().initServerIPandProt(serverProperties); // Map commandHanderMap = configurableApplicationContext.getBeansOfType(BaseHandler.class); // for (BaseHandler handler : commandHanderMap.values()){ // handlerManager.addHandler(handler); diff --git a/src/main/java/com/jmfy/server/ServerConfiguration.java b/src/main/java/com/jmfy/server/ServerConfiguration.java new file mode 100644 index 0000000..fd35ab9 --- /dev/null +++ b/src/main/java/com/jmfy/server/ServerConfiguration.java @@ -0,0 +1,21 @@ +package com.jmfy.server; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +/** + * Created by a on 2017/7/5. + */ +@Configuration(value = "serverConfiguration") +@EnableConfigurationProperties(ServerProperties.class) +public class ServerConfiguration { + + @Autowired + private ServerProperties serverProperties; + + public ServerProperties getServerProperties() { + return serverProperties; + } + +} diff --git a/src/main/java/com/jmfy/server/ServerProperties.java b/src/main/java/com/jmfy/server/ServerProperties.java new file mode 100644 index 0000000..f050828 --- /dev/null +++ b/src/main/java/com/jmfy/server/ServerProperties.java @@ -0,0 +1,37 @@ +package com.jmfy.server; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Created by a on 2017/7/5. + */ +@ConfigurationProperties(prefix = "server") +public class ServerProperties { + + private int id; + + private String deliveryAddress; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getDeliveryAddress() { + return deliveryAddress; + } + + public void setDeliveryAddress(String deliveryAddress) { + this.deliveryAddress = deliveryAddress; + } + + @Override + public String toString() { + return "ServerProperties{" + + "id=" + id + + ", deliveryAddress='" + deliveryAddress + '\''; + } +} diff --git a/src/main/java/com/jmfy/util/JsonUtil.java b/src/main/java/com/jmfy/util/JsonUtil.java index 12f4b15..d8baae7 100644 --- a/src/main/java/com/jmfy/util/JsonUtil.java +++ b/src/main/java/com/jmfy/util/JsonUtil.java @@ -79,16 +79,4 @@ public class JsonUtil { String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()); return dayBefore; } - - public void initServerIPandProt() { - InetAddress ia=null; - try { - ia=ia.getLocalHost(); - String deliveryAddress = ia.getHostAddress() +":"+9991; - LOGGER.info("initServerIPandProt=>deliveryAddress={}",deliveryAddress); - RedisUtil.getInstence().putMapEntry(RedisUserKey.IP_AND_PROT_MAP, String.valueOf(9991),deliveryAddress,3,-1); - } catch (UnknownHostException e) { - e.printStackTrace(); - } - } } diff --git a/src/main/java/com/jmfy/util/RedisUtil.java b/src/main/java/com/jmfy/util/RedisUtil.java index 369e0d7..f62570a 100644 --- a/src/main/java/com/jmfy/util/RedisUtil.java +++ b/src/main/java/com/jmfy/util/RedisUtil.java @@ -3,6 +3,7 @@ package com.jmfy.util; import com.google.gson.Gson; import com.jmfy.redisProperties.RedisProperties; import com.jmfy.redisProperties.RedisUserKey; +import com.jmfy.server.ServerProperties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.redis.RedisConnectionFailureException; @@ -363,4 +364,8 @@ public class RedisUtil { delObject(type, key); } } + public void initServerIPandProt(ServerProperties serverProperties) { + String deliveryAddress = serverProperties.getDeliveryAddress(); + RedisUtil.getInstence().putMapEntry(RedisUserKey.IP_AND_PROT_MAP, String.valueOf(9991),deliveryAddress,3,-1); + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index a07533c..b689adf 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,7 +2,7 @@ server.port=9991 ##contextPath server.context-path=/delivery - +server.deliveryAddress=60.1.1.12:9991 # redis config spring.redis.host = 150.116.94.74 spring.redis.port = 6379 @@ -10,6 +10,7 @@ spring.redis.password = redis.ljsd.COM@ spring.redis.expireTime = -1 spring.data.mongodb.uri = mongodb://60.1.1.14:27017/ysj_core + # #spring.data.mongodb.host=192.168.0.170 #spring.data.mongodb.port=27017