Skip to content

Commit

Permalink
增加了 根据通过httpclint方式根据ip获取环境功能
Browse files Browse the repository at this point in the history
  • Loading branch information
licc168 committed Jan 4, 2018
1 parent 4e2ee4a commit f9688cb
Show file tree
Hide file tree
Showing 18 changed files with 522 additions and 53 deletions.
2 changes: 1 addition & 1 deletion dove-config-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.licc.dove</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.licc.dove.security;

import com.licc.dove.domain.User;
import com.licc.dove.service.UserService;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.GrantedAuthority;
Expand All @@ -15,6 +12,9 @@
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.licc.dove.domain.User;
import com.licc.dove.service.UserService;


/**
* @author Jonsy
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.licc.dove.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.licc.dove.dao.CommonDao;
import com.licc.dove.dao.Page;
import com.licc.dove.dao.ParamMap;
import com.licc.dove.domain.DoveConfigApp;
import com.licc.dove.param.DoveConfigAppParam;
import com.licc.dove.param.PageParam;
import com.licc.dove.service.base.impl.BaseServiceImpl;
import com.licc.dove.util.BeanMapper;
import com.licc.dove.vo.MenuVO;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
* @author lichangchao
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package com.licc.dove.service;

import com.licc.dove.dao.ParamMap;
import com.licc.dove.domain.DoveConfigEnvIp;
import com.licc.dove.param.DoveConfigEnvIpParam;
import com.licc.dove.vo.DoveConfigEnvIpVO;
import com.licc.dove.vo.DoveConfigPropertiesVO;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.licc.dove.dao.CommonDao;
import com.licc.dove.dao.Page;
import com.licc.dove.domain.DoveConfigEnv;
import com.licc.dove.param.DoveConfigEnvParam;
import com.licc.dove.dao.ParamMap;
import com.licc.dove.domain.DoveConfigEnvIp;
import com.licc.dove.param.DoveConfigEnvIpParam;
import com.licc.dove.service.base.impl.BaseServiceImpl;
import com.licc.dove.util.BeanMapper;
import com.licc.dove.vo.DoveConfigEnvIpVO;

/**
* @author lichangchao
Expand All @@ -36,4 +30,6 @@ public Page<DoveConfigEnvIpVO> page(DoveConfigEnvIpParam param) {
}




}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.licc.dove.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.licc.dove.dao.CommonDao;
import com.licc.dove.dao.Page;
import com.licc.dove.dao.ParamMap;
import com.licc.dove.domain.DoveConfigApp;
import com.licc.dove.domain.DoveConfigPropertie;
import com.licc.dove.param.DoveConfigAppParam;
import com.licc.dove.param.DoveConfigPropertieParam;
import com.licc.dove.service.base.impl.BaseServiceImpl;
import com.licc.dove.util.BeanMapper;
import com.licc.dove.vo.DoveConfigPropertiesVO;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
* 配置中心-属性管理
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.licc.dove.service;

import com.licc.dove.dao.Order.Direction;
import com.licc.dove.dao.ParamMap;
import java.util.List;

import javax.annotation.Resource;
Expand All @@ -12,7 +10,9 @@
import com.google.common.collect.Lists;
import com.licc.dove.dao.CommonDao;
import com.licc.dove.dao.Order;
import com.licc.dove.dao.Order.Direction;
import com.licc.dove.dao.Page;
import com.licc.dove.dao.ParamMap;
import com.licc.dove.domain.Menu;
import com.licc.dove.param.PageParam;
import com.licc.dove.service.base.impl.BaseServiceImpl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.licc.dove.service;


import com.licc.dove.dao.CommonDao;
import java.util.Date;
import java.util.List;

Expand All @@ -12,6 +11,7 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.licc.dove.dao.CommonDao;
import com.licc.dove.domain.User;
import com.licc.dove.param.UserParam;
import com.licc.dove.service.base.impl.BaseServiceImpl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public class BaseServiceImpl<T> implements IBaseService<T> {
CommonDao commonDao;
@Override
public void deleteById(Long id) {
commonDao.delete(id);
T t = commonDao.get(DTOClass,id);
commonDao.delete(t);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ public class DoveConfigPropertiesVO {

String appName;
String appProfile;

String appLable;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<result column="pro_value" property="proValue" jdbcType="VARCHAR"/>
<result column="appName" property="appName" jdbcType="VARCHAR"/>
<result column="appProfile" property="appProfile" jdbcType="VARCHAR"/>
<result column="appLable" property="appLable" jdbcType="VARCHAR"/>

</resultMap>
<sql id="Base_Column_List">
t.id , t.config_app_id , t.pro_key, t.pro_value
Expand All @@ -17,7 +19,7 @@
<select id="findList" parameterType="Object" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
,t1.application as appName,t1.profile as appProfile
,t1.application as appName,t1.profile as appProfile,t1.lable as appLable
from dove_config_propertie t,dove_config_app t1
where t.delete_flag = 0 and t1.delete_flag = 0 and t.config_app_id = t1.id
<if test="appName != null and appName != ''">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var options = {
"aoColumns": [
{ data: 'appName' },
{ data: 'appProfile' },
{ data: 'appLable' },
{ data: 'proKey' },
{ data: 'proValue' },
{ data: null,"bSortable": false, "mRender":function(data,type,full){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ <h3 class="header smaller lighter blue"></h3>
<tr>
<th>应用名称</th>
<th>应用环境</th>
<th>标签</th>
<th>属性名</th>
<th>属性值</th>
<th>操作</th>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.licc.dove;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* 自定义属性
*
* @author lichangchao
* @version 1.0.0
* @date 2018/1/4 9:55
* @see
*/
@ConfigurationProperties(prefix = "config", ignoreUnknownFields = false)
public class ConfigProperties {
private final Mail mail = new Mail();

public Mail getMail() {
return mail;
}

public static class Mail {

private String from = "tuxAdmin@localhost";

public String getFrom() {
return from;
}

public void setFrom(String from) {
this.from = from;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class SpringBootWebApplication {

public static void main(String[] args) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.licc.dove.config.service;

import com.licc.dove.config.domin.DoveConfigEnv;
import com.licc.dove.config.domin.DoveConfigEnvIp;
import com.licc.dove.dao.CommonDao;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
*
* @author lichangchao
* @version 1.0.0
* @date 2018/1/4 13:45
* @see
*/
@Service
@Transactional
public class ApiService {
@Resource
CommonDao commonDao;
/**
* 通过IP获取当前环境
* @param ip
* @return
*/
public String getProfileByIp(String ip){
DoveConfigEnvIp env = new DoveConfigEnvIp();
env.setDeleteFlag(false);
env.setIp(ip);
List<DoveConfigEnvIp> list = commonDao.listByExample(env);
if(CollectionUtils.isEmpty(list))return "dev";
DoveConfigEnvIp envIp = list.get(0);
Long envId = envIp.getConfigEnvId();
DoveConfigEnv doveConfigEnv = commonDao.get(DoveConfigEnv.class,envId);
if(doveConfigEnv==null) return "dev";
return doveConfigEnv.getProfile();
}
}
14 changes: 10 additions & 4 deletions dove-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>




Expand All @@ -48,6 +45,15 @@
<version>4.3.5.RELEASE</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>


</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,60 @@
import org.springframework.core.env.PropertySource;
import org.springframework.web.context.support.StandardServletEnvironment;

import com.licc.dove.loader.util.HttpClientUtil;

/**
* 通过当前机器的IP自动改变该项目的环境变量
*/
@Configuration
@Order(-1)
@ConditionalOnProperty("spring.cloud.config.custom")
public class CustomPropertySourceLocator implements PropertySourceLocator {
private final static String Cloud_Config = "spring.cloud.config.";
private final static String Cloud_Config = "spring.cloud.config.";

@Override
public PropertySource<?> locate(Environment environment) {

MutablePropertySources propertySources = ((StandardServletEnvironment) environment).getPropertySources();
updateBootstrap(propertySources);
Map<String,Object> customMap = new HashMap<>();
return new MapPropertySource("custom",customMap);
Map<String, Object> customMap = new HashMap<>();
return new MapPropertySource("custom", customMap);
}

/**
* @author lichangchao
* @time 2017-12-22
* 修改默认的cloud config配置
* 通过当前服务器IP获取环境
*
* @param propertySources
* @return
*/
private void updateBootstrap(MutablePropertySources propertySources){
PropertySource<?> bootstartp = propertySources.get("applicationConfig: [classpath:/bootstrap.yml]");
propertySources.remove("applicationConfig: [classpath:/bootstrap.yml]");
Map<String, Object> source = new HashMap<>();
source.put(Cloud_Config+"profile", "test");
source.put(Cloud_Config+"name",bootstartp.getProperty(Cloud_Config+"name"));
source.put(Cloud_Config+"uri",bootstartp.getProperty(Cloud_Config+"uri"));
source.put(Cloud_Config+"label",bootstartp.getProperty(Cloud_Config+"label"));
propertySources.addLast(new MapPropertySource("applicationConfig: [classpath:/bootstrap.yml]",source));
}
private String getProfileByIp(MutablePropertySources propertySources, String serverUri) {
PropertySource<?> bootstartp = propertySources.get("springCloudClientHostInfo");
String ip = (String) bootstartp.getProperty("spring.cloud.client.ipAddress");
try {
Map<String, String> params = new HashMap<>();
params.put("ip", ip);
String profile = HttpClientUtil.get(serverUri + "/api/getProfileByIp", params);
return profile;
} catch (Exception e) {
e.printStackTrace();
return "";
}

}

/**
* @author lichangchao
* @time 2017-12-22 修改默认的cloud config配置
*/
private void updateBootstrap(MutablePropertySources propertySources) {
PropertySource<?> bootstartp = propertySources.get("applicationConfig: [classpath:/bootstrap.yml]");
propertySources.remove("applicationConfig: [classpath:/bootstrap.yml]");
Map<String, Object> source = new HashMap<>();
source.put(Cloud_Config + "name", bootstartp.getProperty(Cloud_Config + "name"));
source.put(Cloud_Config + "uri", bootstartp.getProperty(Cloud_Config + "uri"));
source.put(Cloud_Config + "label", bootstartp.getProperty(Cloud_Config + "label"));
source.put(Cloud_Config + "profile", getProfileByIp(propertySources, (String) bootstartp.getProperty(Cloud_Config + "uri")));
propertySources.addLast(new MapPropertySource("applicationConfig: [classpath:/bootstrap.yml]", source));
}

}
Loading

0 comments on commit f9688cb

Please sign in to comment.