Commit c0fac0180abdf9efead3365b57f66260b1d45624
1 parent
266f63d194
Exists in
master
and in
6 other branches
mysql数据同步
Showing 12 changed files with 231 additions and 30 deletions
- platform-biz-service/src/main/java/com/lyms/platform/permission/dao/master/MasterOrganizationMapper.java
- platform-biz-service/src/main/java/com/lyms/platform/permission/service/OrganizationService.java
- platform-biz-service/src/main/java/com/lyms/platform/permission/service/impl/OrganizationServiceImpl.java
- platform-biz-service/src/main/resources/mainOrm/master/MasterOrganization.xml
- platform-common/src/main/java/com/lyms/platform/common/utils/IpUtils.java
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/controller/SyncDataController.java
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/inteceptor/MybatisSqlInterceptor.java
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/request/SqlRequest.java
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/utils/HttpClientUtil.java
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/utils/SendMysqlSyncDatUtil.java
- platform-operate-api/src/main/resources/config.properties
- platform-operate-api/src/main/resources/mybatis.xml
platform-biz-service/src/main/java/com/lyms/platform/permission/dao/master/MasterOrganizationMapper.java
View file @
c0fac01
platform-biz-service/src/main/java/com/lyms/platform/permission/service/OrganizationService.java
View file @
c0fac01
platform-biz-service/src/main/java/com/lyms/platform/permission/service/impl/OrganizationServiceImpl.java
View file @
c0fac01
| ... | ... | @@ -77,6 +77,11 @@ |
| 77 | 77 | } |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | + @Override | |
| 81 | + public void execSql(String sql) { | |
| 82 | + masterOrganizationMapper.execSql(sql); | |
| 83 | + } | |
| 84 | + | |
| 80 | 85 | private void addDefaultRoles(Organization obj) { |
| 81 | 86 | List<Roles> rolesList = new ArrayList<>(); |
| 82 | 87 | rolesList.add(rolesService.getRoles(87)); |
platform-biz-service/src/main/resources/mainOrm/master/MasterOrganization.xml
View file @
c0fac01
platform-common/src/main/java/com/lyms/platform/common/utils/IpUtils.java
View file @
c0fac01
| ... | ... | @@ -2,6 +2,7 @@ |
| 2 | 2 | |
| 3 | 3 | import org.apache.commons.collections.CollectionUtils; |
| 4 | 4 | |
| 5 | +import javax.servlet.http.HttpServletRequest; | |
| 5 | 6 | import java.util.HashSet; |
| 6 | 7 | import java.util.Set; |
| 7 | 8 | |
| 8 | 9 | |
| ... | ... | @@ -22,10 +23,9 @@ |
| 22 | 23 | } |
| 23 | 24 | } |
| 24 | 25 | |
| 25 | - public static boolean isWhite(String ip) { | |
| 26 | - return ipSet.contains(ip); | |
| 26 | + public static boolean isWhite(HttpServletRequest request) { | |
| 27 | + return ipSet.contains(request.getRemoteHost()); | |
| 27 | 28 | } |
| 28 | - | |
| 29 | 29 | |
| 30 | 30 | } |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/controller/SyncDataController.java
View file @
c0fac01
| ... | ... | @@ -3,16 +3,21 @@ |
| 3 | 3 | import com.lyms.platform.biz.service.SyncDataService; |
| 4 | 4 | import com.lyms.platform.common.base.BaseController; |
| 5 | 5 | import com.lyms.platform.common.pojo.SyncDataModel; |
| 6 | -import com.lyms.platform.common.utils.Config; | |
| 7 | -import com.lyms.platform.common.utils.JsonUtil; | |
| 8 | -import com.lyms.platform.common.utils.LymsEncodeUtil; | |
| 6 | +import com.lyms.platform.common.utils.*; | |
| 7 | +import com.lyms.platform.operate.web.request.BabyCheckRequest; | |
| 8 | +import com.lyms.platform.operate.web.request.SqlRequest; | |
| 9 | +import com.lyms.platform.permission.service.OrganizationService; | |
| 9 | 10 | import com.lyms.platform.query.SyncDataQuery; |
| 11 | +import org.apache.commons.codec.binary.Base64; | |
| 10 | 12 | import org.apache.commons.lang.StringUtils; |
| 11 | 13 | import org.springframework.beans.factory.annotation.Autowired; |
| 12 | 14 | import org.springframework.stereotype.Controller; |
| 15 | +import org.springframework.web.bind.annotation.RequestBody; | |
| 16 | +import org.springframework.web.bind.annotation.RequestHeader; | |
| 13 | 17 | import org.springframework.web.bind.annotation.RequestMapping; |
| 14 | 18 | import org.springframework.web.bind.annotation.RequestMethod; |
| 15 | 19 | |
| 20 | +import javax.servlet.http.HttpServletRequest; | |
| 16 | 21 | import javax.servlet.http.HttpServletResponse; |
| 17 | 22 | import java.util.List; |
| 18 | 23 | |
| ... | ... | @@ -27,6 +32,9 @@ |
| 27 | 32 | @Autowired |
| 28 | 33 | private SyncDataService syncDataService; |
| 29 | 34 | |
| 35 | + @Autowired | |
| 36 | + private OrganizationService organizationService; | |
| 37 | + | |
| 30 | 38 | public static String mongo_crypto_key = Config.getItem("mongo_crypto_key", "0"); |
| 31 | 39 | |
| 32 | 40 | @RequestMapping(value = "/findSyncData", method = RequestMethod.POST) |
| ... | ... | @@ -63,6 +71,40 @@ |
| 63 | 71 | writeString(response,"updateSyncData success"); |
| 64 | 72 | } else { |
| 65 | 73 | writeString(response, "updateSyncData fail"); |
| 74 | + } | |
| 75 | + } | |
| 76 | + | |
| 77 | + | |
| 78 | + /** | |
| 79 | + * 同步mysql的数据 | |
| 80 | + * 把线上执行的sql拿到区域上面执行 | |
| 81 | + * @param response | |
| 82 | + * @param sqlRequest | |
| 83 | + * @param request | |
| 84 | + */ | |
| 85 | + @RequestMapping(value = "/syncMysqlData", method = RequestMethod.POST) | |
| 86 | + public void updateSyncData(HttpServletResponse response, @RequestBody SqlRequest sqlRequest,HttpServletRequest request, | |
| 87 | + @RequestHeader("Authorization") String token) { | |
| 88 | + | |
| 89 | + if (!"3d19960bf3e81e7d816c4f26051c49ba".equals(token)) | |
| 90 | + { | |
| 91 | + ExceptionUtils.catchException("The request token is "+ token); | |
| 92 | + writeString(response, "Token is error"); | |
| 93 | + return; | |
| 94 | + } | |
| 95 | + | |
| 96 | + if (sqlRequest != null) { | |
| 97 | + String base64Sql = sqlRequest.getSql(); | |
| 98 | + if (StringUtils.isNotEmpty(base64Sql)) | |
| 99 | + { | |
| 100 | + String sqlId = sqlRequest.getSqlId(); | |
| 101 | + String sql = new String(Base64.decodeBase64(base64Sql)); | |
| 102 | + System.out.println("sqlId = " + sqlId+" : "+sql); | |
| 103 | + organizationService.execSql(sql); | |
| 104 | + } | |
| 105 | + writeString(response,"execSyncMysqlData success"); | |
| 106 | + } else { | |
| 107 | + writeString(response, "execSyncMysqlData fail"); | |
| 66 | 108 | } |
| 67 | 109 | } |
| 68 | 110 |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/inteceptor/MybatisSqlInterceptor.java
View file @
c0fac01
| ... | ... | @@ -7,6 +7,7 @@ |
| 7 | 7 | import java.util.Locale; |
| 8 | 8 | import java.util.Properties; |
| 9 | 9 | |
| 10 | +import com.lyms.platform.operate.web.utils.SendMysqlSyncDatUtil; | |
| 10 | 11 | import org.apache.ibatis.executor.Executor; |
| 11 | 12 | import org.apache.ibatis.mapping.BoundSql; |
| 12 | 13 | import org.apache.ibatis.mapping.MappedStatement; |
| ... | ... | @@ -18,8 +19,6 @@ |
| 18 | 19 | import org.apache.ibatis.plugin.Signature; |
| 19 | 20 | import org.apache.ibatis.reflection.MetaObject; |
| 20 | 21 | import org.apache.ibatis.session.Configuration; |
| 21 | -import org.apache.ibatis.session.ResultHandler; | |
| 22 | -import org.apache.ibatis.session.RowBounds; | |
| 23 | 22 | import org.apache.ibatis.type.TypeHandlerRegistry; |
| 24 | 23 | |
| 25 | 24 | /** |
| 26 | 25 | |
| 27 | 26 | |
| ... | ... | @@ -40,28 +39,16 @@ |
| 40 | 39 | String sqlId = mappedStatement.getId(); |
| 41 | 40 | BoundSql boundSql = mappedStatement.getBoundSql(parameter); |
| 42 | 41 | Configuration configuration = mappedStatement.getConfiguration(); |
| 43 | - Object returnValue = null; | |
| 44 | - long start = System.currentTimeMillis(); | |
| 45 | - returnValue = invocation.proceed(); | |
| 46 | - long end = System.currentTimeMillis(); | |
| 47 | - long time = (end - start); | |
| 48 | - if (time > 1) { | |
| 49 | - String sql = getSql(configuration, boundSql, sqlId, time); | |
| 50 | - System.err.println(sql); | |
| 51 | - } | |
| 42 | + Object returnValue = invocation.proceed(); | |
| 43 | + String sql = getSql(configuration, boundSql, sqlId); | |
| 44 | + System.out.println("mysql sql ======"+sql); | |
| 52 | 45 | return returnValue; |
| 53 | 46 | } |
| 54 | 47 | |
| 55 | - public static String getSql(Configuration configuration, BoundSql boundSql, String sqlId, long time) { | |
| 48 | + public static String getSql(Configuration configuration, BoundSql boundSql, String sqlId) { | |
| 56 | 49 | String sql = showSql(configuration, boundSql); |
| 57 | - StringBuilder str = new StringBuilder(100); | |
| 58 | - str.append(sqlId); | |
| 59 | - str.append(":"); | |
| 60 | - str.append(sql); | |
| 61 | - str.append(":"); | |
| 62 | - str.append(time); | |
| 63 | - str.append("ms"); | |
| 64 | - return str.toString(); | |
| 50 | + SendMysqlSyncDatUtil.sendSql(sql,sqlId); | |
| 51 | + return sql; | |
| 65 | 52 | } |
| 66 | 53 | |
| 67 | 54 | private static String getParameterValue(Object obj) { |
| 68 | 55 | |
| ... | ... | @@ -105,9 +92,13 @@ |
| 105 | 92 | } |
| 106 | 93 | } |
| 107 | 94 | } |
| 108 | - System.out.println("========"+sql); | |
| 95 | + | |
| 109 | 96 | return sql; |
| 110 | 97 | } |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 111 | 102 | |
| 112 | 103 | public Object plugin(Object target) { |
| 113 | 104 | return Plugin.wrap(target, this); |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/request/SqlRequest.java
View file @
c0fac01
| 1 | +package com.lyms.platform.operate.web.request; | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Created by Administrator on 2017-04-25. | |
| 5 | + */ | |
| 6 | +public class SqlRequest { | |
| 7 | + | |
| 8 | + private String sql; | |
| 9 | + | |
| 10 | + private String sqlId; | |
| 11 | + | |
| 12 | + public String getSqlId() { | |
| 13 | + return sqlId; | |
| 14 | + } | |
| 15 | + | |
| 16 | + public void setSqlId(String sqlId) { | |
| 17 | + this.sqlId = sqlId; | |
| 18 | + } | |
| 19 | + | |
| 20 | + public String getSql() { | |
| 21 | + return sql; | |
| 22 | + } | |
| 23 | + | |
| 24 | + public void setSql(String sql) { | |
| 25 | + this.sql = sql; | |
| 26 | + } | |
| 27 | +} |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/utils/HttpClientUtil.java
View file @
c0fac01
| ... | ... | @@ -3,17 +3,23 @@ |
| 3 | 3 | /** |
| 4 | 4 | * Created by Administrator on 2017-01-18. |
| 5 | 5 | */ |
| 6 | +import java.io.IOException; | |
| 6 | 7 | import java.util.*; |
| 7 | 8 | import java.util.Map.Entry; |
| 8 | 9 | |
| 9 | 10 | import com.lyms.platform.common.pojo.SyncDataModel; |
| 11 | +import com.lyms.platform.common.utils.ExceptionUtils; | |
| 10 | 12 | import com.lyms.platform.common.utils.JsonUtil; |
| 11 | 13 | import org.apache.http.HttpEntity; |
| 12 | 14 | import org.apache.http.HttpResponse; |
| 15 | +import org.apache.http.HttpStatus; | |
| 13 | 16 | import org.apache.http.NameValuePair; |
| 14 | 17 | import org.apache.http.client.HttpClient; |
| 18 | +import org.apache.http.client.config.RequestConfig; | |
| 15 | 19 | import org.apache.http.client.entity.UrlEncodedFormEntity; |
| 16 | 20 | import org.apache.http.client.methods.HttpPost; |
| 21 | +import org.apache.http.entity.StringEntity; | |
| 22 | +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | |
| 17 | 23 | import org.apache.http.message.BasicNameValuePair; |
| 18 | 24 | import org.apache.http.util.EntityUtils; |
| 19 | 25 | |
| ... | ... | @@ -22,6 +28,30 @@ |
| 22 | 28 | */ |
| 23 | 29 | public class HttpClientUtil { |
| 24 | 30 | |
| 31 | + | |
| 32 | + private static PoolingHttpClientConnectionManager connMgr; | |
| 33 | + private static RequestConfig requestConfig; | |
| 34 | + private static final int MAX_TIMEOUT = 7000; | |
| 35 | + | |
| 36 | + static { | |
| 37 | + // 设置连接池 | |
| 38 | + connMgr = new PoolingHttpClientConnectionManager(); | |
| 39 | + // 设置连接池大小 | |
| 40 | + connMgr.setMaxTotal(100); | |
| 41 | + connMgr.setDefaultMaxPerRoute(connMgr.getMaxTotal()); | |
| 42 | + | |
| 43 | + RequestConfig.Builder configBuilder = RequestConfig.custom(); | |
| 44 | + // 设置连接超时 | |
| 45 | + configBuilder.setConnectTimeout(MAX_TIMEOUT); | |
| 46 | + // 设置读取超时 | |
| 47 | + configBuilder.setSocketTimeout(MAX_TIMEOUT); | |
| 48 | + // 设置从连接池获取连接实例的超时 | |
| 49 | + configBuilder.setConnectionRequestTimeout(MAX_TIMEOUT); | |
| 50 | + // 在提交请求之前 测试连接是否可用 | |
| 51 | + configBuilder.setStaleConnectionCheckEnabled(true); | |
| 52 | + requestConfig = configBuilder.build(); | |
| 53 | + } | |
| 54 | + | |
| 25 | 55 | public static String doPost(String url,Map<String,String> map,String charset){ |
| 26 | 56 | HttpClient httpClient = null; |
| 27 | 57 | HttpPost httpPost = null; |
| ... | ... | @@ -51,6 +81,54 @@ |
| 51 | 81 | ex.printStackTrace(); |
| 52 | 82 | } |
| 53 | 83 | return result; |
| 84 | + } | |
| 85 | + | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * 发送 SSL POST 请求(HTTPS),JSON形式 | |
| 89 | + * @param apiUrl API接口URL | |
| 90 | + * @param json JSON对象 | |
| 91 | + * @return | |
| 92 | + */ | |
| 93 | + public static String doPostSSL(String apiUrl, Object json,String token) { | |
| 94 | + | |
| 95 | + HttpClient httpClient = null; | |
| 96 | + HttpPost httpPost = new HttpPost(apiUrl); | |
| 97 | + HttpResponse response = null; | |
| 98 | + String httpStr = null; | |
| 99 | + | |
| 100 | + try { | |
| 101 | + httpClient = new SSLClient(); | |
| 102 | + httpPost.addHeader("Authorization", token); | |
| 103 | + httpPost.setConfig(requestConfig); | |
| 104 | + StringEntity stringEntity = new StringEntity(json.toString(),"UTF-8");//解决中文乱码问题 | |
| 105 | + stringEntity.setContentEncoding("UTF-8"); | |
| 106 | + stringEntity.setContentType("application/json"); | |
| 107 | + httpPost.setEntity(stringEntity); | |
| 108 | + response = httpClient.execute(httpPost); | |
| 109 | + int statusCode = response.getStatusLine().getStatusCode(); | |
| 110 | + if (statusCode != HttpStatus.SC_OK) { | |
| 111 | + return null; | |
| 112 | + } | |
| 113 | + HttpEntity entity = response.getEntity(); | |
| 114 | + if (entity == null) { | |
| 115 | + return null; | |
| 116 | + } | |
| 117 | + httpStr = EntityUtils.toString(entity, "utf-8"); | |
| 118 | + } catch (Exception e) { | |
| 119 | + ExceptionUtils.catchException(e,"error https post "); | |
| 120 | + e.printStackTrace(); | |
| 121 | + } finally { | |
| 122 | + if (response != null) { | |
| 123 | + try { | |
| 124 | + EntityUtils.consume(response.getEntity()); | |
| 125 | + } catch (IOException e) { | |
| 126 | + ExceptionUtils.catchException(e, "error https post "); | |
| 127 | + e.printStackTrace(); | |
| 128 | + } | |
| 129 | + } | |
| 130 | + } | |
| 131 | + return httpStr; | |
| 54 | 132 | } |
| 55 | 133 | |
| 56 | 134 | public static void main(String[] args) throws Exception { |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/utils/SendMysqlSyncDatUtil.java
View file @
c0fac01
| 1 | +package com.lyms.platform.operate.web.utils; | |
| 2 | + | |
| 3 | +import com.lyms.platform.common.utils.JsonUtil; | |
| 4 | +import com.lyms.platform.common.utils.PropertiesUtils; | |
| 5 | +import com.lyms.platform.common.utils.StringUtils; | |
| 6 | +import com.lyms.platform.operate.web.request.SqlRequest; | |
| 7 | +import org.apache.commons.codec.binary.Base64; | |
| 8 | +import org.apache.commons.collections.CollectionUtils; | |
| 9 | + | |
| 10 | +import java.util.HashSet; | |
| 11 | +import java.util.Set; | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * Created by Administrator on 2017-04-25. | |
| 15 | + */ | |
| 16 | +public class SendMysqlSyncDatUtil { | |
| 17 | + | |
| 18 | + private static Set<String> urls = new HashSet<>(); | |
| 19 | + static | |
| 20 | + { | |
| 21 | + String sync_mysql_data_url = PropertiesUtils.getPropertyValue("sync_mysql_data_url"); | |
| 22 | + if (StringUtils.isNotEmpty(sync_mysql_data_url)) | |
| 23 | + { | |
| 24 | + String[] arrs = sync_mysql_data_url.split(","); | |
| 25 | + if (arrs != null && arrs.length > 0) | |
| 26 | + { | |
| 27 | + CollectionUtils.addAll(urls, arrs); | |
| 28 | + } | |
| 29 | + } | |
| 30 | + | |
| 31 | + } | |
| 32 | + | |
| 33 | + public static void sendSql(String sql ,String sqlId) | |
| 34 | + { | |
| 35 | + SqlRequest request = new SqlRequest(); | |
| 36 | + request.setSql(Base64.encodeBase64String(sql.getBytes())); | |
| 37 | + request.setSqlId(sqlId); | |
| 38 | + String json = JsonUtil.obj2Str(request); | |
| 39 | + if (CollectionUtils.isNotEmpty(urls)) | |
| 40 | + { | |
| 41 | + for(String url : urls) | |
| 42 | + { | |
| 43 | + System.out.println("sync mysql url = " + url); | |
| 44 | + HttpClientUtil.doPostSSL(url,json,"3d19960bf3e81e7d816c4f26051c49ba"); | |
| 45 | + } | |
| 46 | + } | |
| 47 | + | |
| 48 | + } | |
| 49 | +} |
platform-operate-api/src/main/resources/config.properties
View file @
c0fac01
| ... | ... | @@ -27,9 +27,8 @@ |
| 27 | 27 | #区域统计地址 |
| 28 | 28 | area_count_url=119.90.57.26:1522 |
| 29 | 29 | |
| 30 | -#app服务器端的ip白名单多个用逗号隔开 127.0.0.1,192.168.1.1 | |
| 31 | -app_server_ip_white= | |
| 32 | - | |
| 30 | +#同步mysql数据到各个区域的地址,多个用逗号隔开 如:https://area-qhd-api.healthbaby.com.cn:18019/syncMysqlData,https://area-dz-api.healthbaby.com.cn:12356/syncMysqlData | |
| 31 | +sync_mysql_data_url= | |
| 33 | 32 | |
| 34 | 33 | #数据源相关配置 |
| 35 | 34 | jdbc.0.driver=oracle.jdbc.driver.OracleDriver |
platform-operate-api/src/main/resources/mybatis.xml
View file @
c0fac01
| ... | ... | @@ -11,6 +11,7 @@ |
| 11 | 11 | <setting name="autoMappingBehavior" value="FULL" /> |
| 12 | 12 | <setting name="defaultExecutorType" value="SIMPLE" /> |
| 13 | 13 | <setting name="defaultStatementTimeout" value="25000" /> |
| 14 | + <setting name="callSettersOnNulls" value="true"/> | |
| 14 | 15 | </settings> |
| 15 | 16 | <plugins> |
| 16 | 17 | <!-- mybatis写出sql记录控件(拦截器) --> |