Merge branch 'master' of 10.10.14.250:huanghai/dsMin

master
zhangjun 5 years ago
commit aeadab7c56

@ -1,31 +1,35 @@
[mysql] # mysql的配置项
ip = server.dsmin.com
port = 22066
database = base_db_dev
user = root
pwd = DsideaL147258369
[distribute] #发布功能的配置
ip = server.dsmin.com
port = 26611
user = root
pwd = dsideal
remotePath = /usr/local/dsMin/dsBaseRpc/
localPath = E:\Work\dsMin\dsBaseRpc\build
[redis]
ip =server.dsmin.com
port = 18890
db = 0
expireTime = 86400
# 注册rpc server
[rpcServer]
port = 8001
# 本项目名称,用于记录日志
[project]
project_name = dsBaseRpc
[kafka]
KafkaAddress = server.dsmin.com:9092
[mysql] # mysql的配置项
ip = server.dsmin.com
port = 22066
database = base_db_dev
user = root
pwd = DsideaL147258369
[distribute] #发布功能的配置
ip = server.dsmin.com
port = 26611
user = root
pwd = dsideal
remotePath = /usr/local/dsMin/dsBaseRpc/
localPath = E:\Work\dsMin\dsBaseRpc\build
[redis]
ip =server.dsmin.com
port = 18890
db = 0
expireTime = 86400
# 注册rpc server
[rpcServer]
port = 8001
# 本项目名称,用于记录日志
[project]
project_name = dsBaseRpc
[kafka]
KafkaAddress = server.dsmin.com:9092
# 数据汇集的地址
[dataExchange]
url = http://10.10.14.239:9009/v1/dataex/DataexSet

@ -1,38 +0,0 @@
package DataEx
import (
"dsBaseRpc/Const"
"dsBaseRpc/Utils/DbUtil"
"dsBaseRpc/Utils/LogUtil"
"fmt"
)
var db = DbUtil.Engine
//有同步哪些表
var sqlDict = []string{"t_base_organization", "t_base_class"}
/**
2020-07-16
*/
func DataExchange() {
for {
//本轮上报的数量如果是0休息10秒后再继续上传
count:=0
//遍历所有的配置节,进行循环
for i := range sqlDict {
//判断日志目录下的记录是不是存在?
//存在则读取last_updated_time+id_int进行分页获取
paramMap := map[string]interface{}{"last_updated_time": "2020-07-15 00:00:00"}
list, err := db.SqlMapClient(sqlDict[i], &paramMap).Query().List()
if err != nil {
LogUtil.Error(Const.DataBaseActionError, "数据上报时发生了严重错误:"+err.Error())
break
}
fmt.Println(list)
}
}
}

@ -0,0 +1,272 @@
package DataExchange
import (
"bytes"
"dsBaseRpc/Utils/CommonUtil"
"dsBaseRpc/Utils/ConfigUtil"
"dsBaseRpc/Utils/DbUtil"
"dsBaseRpc/Utils/FileUtil"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
)
var db = DbUtil.Engine
// 日志文件路径
var progressFilePath = "/usr/local/SyncDataLogs/"
//建立与汇集中心的主题映射关系结构
type tableStruct struct {
TableName string `json:"table_name"`
PrimaryKey string `json:"primary_key"`
DataSource string `json:"data_source"`
}
//有同步哪些表,之所以不遍历文件的名称进行上报,是因为需要控制上传的顺序,如果只是文件名,就丢失了顺序
var sqlDict = []tableStruct{
{TableName: "t_base_organization", PrimaryKey: "org_id", DataSource: "org_school"},
{TableName: "t_base_class", PrimaryKey: "class_id", DataSource: "org_class"},
}
// 数据上报的结构体
type postStruct struct {
AuthToken string `json:"auth_token"`
DataSource string `json:"data_source"`
SystemId string `json:"system_id"`
Datas []dataStruct `json:"datas"`
}
type dataStruct struct {
Data string `json:"data"`
DataId string `json:"data_id"`
DelFlag int64 `json:"del_flag"`
OrgId string `json:"org_id"`
}
// 日志文件对应的结构体
type logStruct struct {
StartUpdateTs string `json:"start_update_ts"`
IdInt int64 `json:"id_int"`
}
/**
*/
func init() {
if !FileUtil.PathExists(progressFilePath) {
os.MkdirAll(progressFilePath, os.ModePerm)
}
}
/**
2020-07-16
*/
func DataExchange() {
for {
//本轮上报的数量如果是0休息5秒后再继续上传
postCount := UploadData()
if postCount == 0 {
fmt.Println(CommonUtil.GetCurrentTime() + " 同步本轮没有可以上报的数据将休息5秒")
time.Sleep(5 * 1e9)
}
}
}
/**
*
*
* 2019-01-02
*/
func UploadData() int {
var postCount = 0
//遍历所有的配置节,进行循环
for i := range sqlDict {
count := 0
//默认的开始时间
startUpdateTs := "1970-01-01 00:00:00"
//默认的整数主键值
var idInt int64 = 0
//查询结果集
var list []map[string]interface{}
//表名
tableName := sqlDict[i].TableName
//日志文件位置
logName := progressFilePath + tableName + ".log"
//判断文件是不是存在
if FileUtil.PathExists(logName) {
//读取配置信息
var logstruct logStruct
if err := json.Unmarshal([]byte(FileUtil.ReadFileContent(logName)), &logstruct); err == nil {
startUpdateTs = logstruct.StartUpdateTs
idInt = logstruct.IdInt
} else {
fmt.Println(CommonUtil.GetCurrentTime() + " 解析JSON文件失败:" + err.Error())
}
}
//一直循环
for {
//获取数据
startUpdateTs, idInt, list = getRecord(tableName, startUpdateTs, idInt)
if len(list) == 0 {
break
}
//上报到Http Api--->Body--->Post
var ps postStruct
ps.DataSource = sqlDict[i].DataSource
ps.AuthToken = "DSDataex_Token_eb4ab2fea87161dc08fa794a648584c4"
ps.SystemId = "BASE_GO"
var dsMap = make([]dataStruct, 0)
for k := range list {
var ds dataStruct
ds.Data, _ = CommonUtil.MapToJson(list[k])
ds.DataId = list[k][sqlDict[i].PrimaryKey].(string)
ds.DelFlag = list[k]["del_flag"].(int64)
ds.OrgId = list[k]["bureau_id"].(string)
dsMap = append(dsMap, ds)
}
ps.Datas = dsMap
//将Struct转为json
jsonBytes, _ := json.Marshal(ps)
msg := string(jsonBytes)
//提交到汇集中心
p := httpDo("POST", ConfigUtil.DataExchangeUrl, msg)
if !p.Success {
fmt.Println(CommonUtil.GetCurrentTime() + " 同步上报到数据汇集中心失败将休息5秒后重试错误原因"+p.Message)
time.Sleep(5 * 1e9)
continue
}
//记录上报数量
count = count + len(list)
//记录日志
var l logStruct
l.IdInt = idInt
l.StartUpdateTs = startUpdateTs
jsonBytes, err := json.Marshal(l)
if err != nil {
fmt.Println(err.Error())
}
FileUtil.WriteContent(logName, string(jsonBytes))
//提示信息
fmt.Println(CommonUtil.GetCurrentTime() + " 同步:" + tableName + ",上报" + CommonUtil.ConvertIntToString(count) +
"个start_update_ts=" + startUpdateTs + ",id_int=" + CommonUtil.ConvertInt64ToString(idInt))
}
postCount = postCount + count
}
return postCount
}
/**
*
*
* 2019-01-16
*/
func getRecord(tableName string, lastUpdatedTime string, idInt int64) (string, int64, []map[string]interface{}) {
//每次获取的条数
limit := 100
//SQL内容
sqlPublic := FileUtil.ReadFileContent("./Sql/" + tableName + ".sql")
//时间戳相等的SQL
eqSql := sqlPublic + " where t1.last_updated_time =? and t1.id_int>? order by t1.last_updated_time,t1.id_int limit ?"
//时间戳大于的SQL
gtSql := sqlPublic + " where t1.last_updated_time >? order by t1.last_updated_time,t1.id_int limit ?"
//(1)计算相等的
var rsEq = make([]map[string]interface{}, 0)
var rsGt = make([]map[string]interface{}, 0)
lastUpdatedTime, idInt, rsEq = getRecordEq(eqSql, lastUpdatedTime, idInt, limit)
if len(rsEq) < limit {
//尝试一次gt操作(组合一下)
lastUpdatedTime, idInt, rsGt = getRecordGt(gtSql, lastUpdatedTime, idInt, limit-len(rsEq))
if len(rsGt) > 0 {
//拼装一下,合并两个结果集
for i := range rsGt {
rsEq = append(rsEq, rsGt[i])
}
//更改一下
lastUpdatedTime = rsGt[len(rsGt)-1]["last_updated_time"].(string)
idInt = rsGt[len(rsGt)-1]["id_int"].(int64)
}
}
return lastUpdatedTime, idInt, rsEq
}
/**
2020-7-17
*/
func getRecordEq(eqSql string, lastUpdatedTime string, idInt int64, limit int) (string, int64, []map[string]interface{}) {
rs, _ := db.SQL(eqSql, lastUpdatedTime, idInt, limit).Query().List()
if rs != nil && len(rs) > 0 {
//取出最后一行的数据进行回写
lastUpdatedTime = rs[len(rs)-1]["last_updated_time"].(string)
idInt = rs[len(rs)-1]["id_int"].(int64)
return lastUpdatedTime, idInt, rs
} else {
return lastUpdatedTime, idInt, nil
}
}
/**
2020-7-17
*/
func getRecordGt(gtSql string, lastUpdatedTime string, idInt int64, limit int) (string, int64, []map[string]interface{}) {
rs, _ := db.SQL(gtSql, lastUpdatedTime, limit).Query().List()
if rs != nil && len(rs) > 0 {
//取出最后一行的数据进行回写
lastUpdatedTime = rs[len(rs)-1]["last_updated_time"].(string)
idInt := rs[len(rs)-1]["id_int"].(int64)
return lastUpdatedTime, idInt, rs
} else {
return lastUpdatedTime, idInt, nil
}
}
type FailResultStruct struct {
FailId string `json:"fail_id"`
FailReason string `json:"fail_reason"`
}
type ResultStruct struct {
Message string `json:"message"`
Success bool `json:"success"`
FailResults []FailResultStruct `json:"fail_results"`
}
// 基础方法这里多用于访问webapi配合上json转换。此方法可以运行但是不算完善。
func httpDo(method string, url string, msg string) ResultStruct {
var p ResultStruct
p.Success=false
p.Message="上报到汇集系统失败!"
client := &http.Client{}
body := bytes.NewBuffer([]byte(msg))
req, err := http.NewRequest(method,
url,
body)
if err != nil {
// handle error
}
req.Header.Set("Content-Type", "application/json;charset=utf-8")
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return p
}
defer resp.Body.Close()
resultBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return p
}
json.Unmarshal(resultBody, &p)
return p
}

@ -1,79 +0,0 @@
<sqlMap>
<sql id="t_base_organization">
SELECT
cast( `t_base_organization`.`org_id` AS CHAR ( 36 ) charset utf8 ) AS `org_id`,
`t_base_organization`.`id_int` AS `id_int`,
`t_base_organization`.`org_code` AS `org_code`,
`t_base_organization`.`org_name` AS `org_name`,
cast( `t_base_organization`.`parent_id` AS CHAR ( 36 ) charset utf8 ) AS `parent_id`,
cast( `t_base_organization`.`bureau_id` AS CHAR ( 36 ) charset utf8 ) AS `bureau_id`,
`t_base_organization`.`org_type` AS `org_type`,
`t_base_organization`.`edu_assist_type` AS `edu_assist_type`,
`t_base_organization`.`main_school_type` AS `main_school_type`,
cast( `t_base_organization`.`main_school_id` AS CHAR ( 36 ) charset utf8 ) AS `main_school_id`,
cast( `t_base_organization`.`manage_org_id` AS CHAR ( 36 ) charset utf8 ) AS `manage_org_id`,
`t_base_organization`.`directly_under_type` AS `directly_under_type`,
`t_base_organization`.`xxbbm` AS `xxbbm`,
`t_base_organization`.`xxbxlxm` AS `xxbxlxm`,
`t_base_organization`.`szdcxlxm` AS `szdcxlxm`,
`t_base_organization`.`xxjbzm` AS `xxjbzm`,
cast( `t_base_organization`.`fzr` AS CHAR ( 36 ) charset utf8 ) AS `fzr`,
`t_base_organization`.`fddbr` AS `fddbr`,
`t_base_organization`.`fddbrdh` AS `fddbrdh`,
`t_base_organization`.`address` AS `address`,
`t_base_organization`.`lxdh` AS `lxdh`,
`t_base_organization`.`org_lng` AS `org_lng`,
`t_base_organization`.`org_lat` AS `org_lat`,
`t_base_organization`.`sort_id` AS `sort_id`,
cast( `t_base_organization`.`b_use` AS signed ) AS `b_use`,
`t_base_organization`.`province_code` AS `province_code`,
`t_base_organization`.`city_code` AS `city_code`,
`t_base_organization`.`district_code` AS `district_code`,
`t_base_organization`.`area_code` AS `area_code`,
`t_base_organization`.`last_updated_time` AS `last_updated_time`,
DATE_FORMAT(`t_base_organization`.`create_time`,'%Y/%m/%d %H:%i:%s') AS `create_time`,
case `t_base_organization`.`b_use` when -1 then 1 else 0 end AS `del_flag`
FROM
`t_base_organization`
WHERE
`t_base_organization`.`last_updated_time`>?last_updated_time
ORDER BY
`t_base_organization`.`last_updated_time`
</sql>
<sql id="t_base_class">
SELECT
cast( `t_base_class`.`class_id` AS CHAR ( 36 ) charset utf8 ) AS `class_id`,
`t_base_class`.`id_int` AS `id_int`,
`t_base_class`.`bh` AS `bh`,
`t_base_class`.`class_code` AS `class_code`,
`t_base_class`.`class_name` AS `class_name`,
`t_base_class`.`class_alias` AS `class_alias`,
`t_base_class`.`rxnf` AS `rxnf`,
`t_base_class`.`rxjj` AS `rxjj`,
`t_base_class`.`schooling_length` AS `schooling_length`,
`t_base_class`.`stage_id` AS `stage_id`,
cast( `t_base_class`.`teacher_id` AS CHAR ( 36 ) charset utf8 ) AS `teacher_id`,
cast( `t_base_class`.`org_id` AS CHAR ( 36 ) charset utf8 ) AS `org_id`,
cast( `t_base_class`.`bureau_id` AS CHAR ( 36 ) charset utf8 ) AS `bureau_id`,
cast( `t_base_class`.`b_use` AS signed ) AS `b_use`,
`t_base_class`.`province_code` AS `province_code`,
`t_base_class`.`city_code` AS `city_code`,
`t_base_class`.`district_code` AS `district_code`,
cast( `t_base_class`.`main_school_id` AS CHAR ( 36 ) charset utf8 ) AS `main_school_id`,
DATE_FORMAT(`t_base_class`.`create_time`,'%Y/%m/%d %H:%i:%s') AS `create_time`,
`t_base_class`.`last_updated_time` AS `last_updated_time`,
`t_dm_stage`.`stage_name` AS `stage_name`,
`t_base_organization`.`org_name` AS `org_name` ,
case `t_base_class`.`b_use` when -1 then 1 else 0 end AS `del_flag`
FROM
((
`t_base_class`
JOIN `t_dm_stage` ON ( `t_base_class`.`stage_id` = `t_dm_stage`.`stage_id` ))
JOIN `t_base_organization` ON ( `t_base_class`.`bureau_id` = `t_base_organization`.`org_id` ))
WHERE
`t_base_class`.`last_updated_time`>?last_updated_time
ORDER BY
`t_base_class`.`last_updated_time`
</sql>
</sqlMap>

@ -0,0 +1,28 @@
SELECT
cast( t1.`class_id` AS CHAR ( 36 ) charset utf8 ) AS `class_id`,
t1.`id_int` AS `id_int`,
t1.`bh` AS `bh`,
t1.`class_code` AS `class_code`,
t1.`class_name` AS `class_name`,
t1.`class_alias` AS `class_alias`,
t1.`rxnf` AS `rxnf`,
t1.`rxjj` AS `rxjj`,
t1.`schooling_length` AS `schooling_length`,
t1.`stage_id` AS `stage_id`,
cast( t1.`teacher_id` AS CHAR ( 36 ) charset utf8 ) AS `teacher_id`,
cast( t1.`org_id` AS CHAR ( 36 ) charset utf8 ) AS `org_id`,
cast( t1.`bureau_id` AS CHAR ( 36 ) charset utf8 ) AS `bureau_id`,
cast( t1.`b_use` AS signed ) AS `b_use`,
t1.`province_code` AS `province_code`,
t1.`city_code` AS `city_code`,
t1.`district_code` AS `district_code`,
cast( t1.`main_school_id` AS CHAR ( 36 ) charset utf8 ) AS `main_school_id`,
DATE_FORMAT(t1.`create_time`,'%Y/%m/%d %H:%i:%s') AS `create_time`,
t1.`last_updated_time` AS `last_updated_time`,
`t_dm_stage`.`stage_name` AS `stage_name`,
`t_base_organization`.`org_name` AS `org_name` ,
case t1.`b_use` when -1 then 1 else 0 end AS `del_flag`
FROM
((t_base_class as t1 JOIN `t_dm_stage` ON ( t1.`stage_id` = `t_dm_stage`.`stage_id` ))
JOIN `t_base_organization` ON ( t1.`bureau_id` = `t_base_organization`.`org_id` ))

@ -0,0 +1,36 @@
SELECT
cast( t1.`org_id` AS CHAR ( 36 ) charset utf8 ) AS `org_id`,
t1.`id_int` AS `id_int`,
t1.`org_code` AS `org_code`,
t1.`org_name` AS `org_name`,
cast( t1.`parent_id` AS CHAR ( 36 ) charset utf8 ) AS `parent_id`,
cast( t1.`bureau_id` AS CHAR ( 36 ) charset utf8 ) AS `bureau_id`,
t1.`org_type` AS `org_type`,
t1.`edu_assist_type` AS `edu_assist_type`,
t1.`main_school_type` AS `main_school_type`,
cast( t1.`main_school_id` AS CHAR ( 36 ) charset utf8 ) AS `main_school_id`,
cast( t1.`manage_org_id` AS CHAR ( 36 ) charset utf8 ) AS `manage_org_id`,
t1.`directly_under_type` AS `directly_under_type`,
t1.`xxbbm` AS `xxbbm`,
t1.`xxbxlxm` AS `xxbxlxm`,
t1.`szdcxlxm` AS `szdcxlxm`,
t1.`xxjbzm` AS `xxjbzm`,
cast( t1.`fzr` AS CHAR ( 36 ) charset utf8 ) AS `fzr`,
t1.`fddbr` AS `fddbr`,
t1.`fddbrdh` AS `fddbrdh`,
t1.`address` AS `address`,
t1.`lxdh` AS `lxdh`,
t1.`org_lng` AS `org_lng`,
t1.`org_lat` AS `org_lat`,
t1.`sort_id` AS `sort_id`,
cast( t1.`b_use` AS signed ) AS `b_use`,
t1.`province_code` AS `province_code`,
t1.`city_code` AS `city_code`,
t1.`district_code` AS `district_code`,
t1.`area_code` AS `area_code`,
t1.`last_updated_time` AS `last_updated_time`,
DATE_FORMAT(t1.`create_time`,'%Y/%m/%d %H:%i:%s') AS `create_time`,
case t1.`b_use` when -1 then 1 else 0 end AS `del_flag`
FROM
`t_base_organization` as t1

@ -631,3 +631,26 @@ func CopyFields(sourceStruct interface{}, targetStruct interface{}, fields ...st
}
return
}
// Convert json string to map
func JsonToMap(jsonStr string) (map[string]string, error) {
m := make(map[string]string)
err := json.Unmarshal([]byte(jsonStr), &m)
if err != nil {
fmt.Printf("Unmarshal with error: %+v\n", err)
return nil, err
}
for k, v := range m {
fmt.Printf("%v: %v\n", k, v)
}
return m, nil
}
// Convert map json string
func MapToJson(m map[string]interface{}) (string, error) {
jsonByte, err := json.Marshal(m)
if err != nil {
fmt.Printf("Marshal with error: %+v\n", err)
return "", nil
}
return string(jsonByte), nil
}

@ -38,6 +38,9 @@ var (
ProjectName string
//kafka地址
KafkaAddress string
//数据汇集中心地址
DataExchangeUrl string
)
func init() {
@ -92,6 +95,9 @@ func init() {
ProjectName = iniParser.GetString("project", "project_name")
//kafka地址
KafkaAddress = iniParser.GetString("kafka", "KafkaAddress")
//数据汇集中心地址
DataExchangeUrl=iniParser.GetString("dataExchange","url")
}
type IniParser struct {

@ -1,7 +1,7 @@
package main
import (
"dsBaseRpc/DataEx"
"dsBaseRpc/DataExchange"
"dsBaseRpc/RpcService/BaseClass/BaseClassProto"
"dsBaseRpc/RpcService/BaseClass/BaseClassService"
"dsBaseRpc/RpcService/BaseGlobal/BaseGlobalProto"
@ -99,12 +99,12 @@ func main() {
//五、开启一个数据上报的协程
go func() {
DataEx.DataExchange()
DataExchange.DataExchange()
}()
//六、 注册反射服务 这个服务是CLI使用的 跟服务本身没有关系
reflection.Register(s)
//七、启动
fmt.Printf("服务发布成功,端口:%s", ConfigUtil.RpcServerPort)
fmt.Printf("服务发布成功,端口:%s\n", ConfigUtil.RpcServerPort)
if err := s.Serve(lis); err != nil {
fmt.Printf("failed to serve: %v\n", err)
}

Loading…
Cancel
Save