Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
xinjiayu committed Jun 4, 2020
1 parent fd784f3 commit fa5c65c
Show file tree
Hide file tree
Showing 28 changed files with 27,925 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
bin/
log/
.DS_Store
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 rain
Copyright (c) 2020 [email protected] https://github.com/xinjiayu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
167 changes: 165 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,165 @@
# ms-weather
天气预报API数据代理服务
# MS-WEATHER

**天气预报第三方数据桥接服务**

配置不同的第三方天气数据,统一的数据格式输出,确保天气数据的有效性,并进行数据本地化存储。数据从远程获取支持本地化缓存的功能,减少远程的api调用量。
配置多个不同的api数据源,字段可以自动合并,输出最大的结果集。

系统采用微服务模式开发。

## 特点

* 微服务架构,支持多种服务注册中心(mdns、consul、eureka),默认为mdns

* API数据源数据元可以通过文件配置

* 数据可本地化存储,方便历史数据调用

## 技术栈

基于go-micro、GoFrame开发的天气预报桥接服务

微服务框架:[go-micro](https://github.com/micro/go-micro)[中文文档](https://learnku.com/docs/go-micro/2.x) 】 、 [micro](https://github.com/micro/micro)[文档](https://micro.mu/docs/)

web路由:[GoFrame](https://github.com/gogf/gf)[中文文档](https://goframe.org/index)

服务管理:[consul](https://www.consul.io/)[用户手册](https://kingfree.gitbook.io/consul/)

数据库:SQLite 【 [中文文档](https://doc.yonyoucloud.com/doc/wiki/project/sqlite/sqlite-intro.html)*GO驱动使用 github.com/mattn/go-sqlite3*[接口文档](https://godoc.org/github.com/mattn/go-sqlite3)

目录结构说明:

```
.
├── api-srv weather对外暴露api接口服务,内部与RPC服务通讯。
├── bin 统一编译后生成的执行文件,可以拷走直接使用。包括了必要的运行文件
├── config 开发环境配置文件目录
├── db 开发环境数据库目录
├── resources 相关资源
├── tools 自行编译的Micro API网关服务
└── weather-srv weather RPC服务
├── handler
├── proto
├── service
└── units
```

## 运行


**运行 Micro API网关服务**

micro api 即可启动api一个网关,默认的端口是8080
可以通过--address=0.0.0.0:8080flag或者设置环境MICRO_API_ADDRESS=0.0.0.0:8080来修改

`$ micro api --handler=proxy`

```bash

micro --registry=consul --registry_address=127.0.0.1:8500 api

```


运行 Weather Service

`$ go run main.go`

运行 Weather API

```
$ go run ./api-srv/main.go
Listening on [::]:64738
```

## 服务发现

支持多种服务发现系统。默认情况下,服务发现基于组播DNS(mDNS)机制
如果要使用consul,请按下面方式执行。

**运行consul服务**

`consul agent -dev`

**srv、api两个服务启动加参数**
```bash
--registry=mdns

```

Curl API
测试Now 天气实况
```
curl http://127.0.0.1:8080/weather/now
{
"message": "Hi, this is the Now API"
}
```
测试forecast 天气预报未来三天的天气情况
```
curl http://127.0.0.1:8080/weather/forecast
{
"msg": "测试forecast"
}
```



## 天气数据源

一、sojson.com
说明:https://www.sojson.com/blog/305.html

二、天气API
https://www.tianqiapi.com/api?version=v6&appid=93247964&appsecret=MRlcpO5o&cityid=101020100

三、国家气象信息中心
http://data.cma.cn/market/index.html



## 其它资料

天气质数说明相关的

http://aqicn.org/city/beijing/cn/


可参考的天气预报接口说明:
https://open.caiyunapp.com/%E5%BD%A9%E4%BA%91%E5%A4%A9%E6%B0%94_API_%E4%B8%80%E8%A7%88%E8%A1%A8





## API数据源配置文件说明
配置文件的修改将即时生效,无所重启服务。

now 为天气实况信息

forecast 为天气预报信息,未来1--5天

filter 为进行数据过滤的设置。如:源数据中的温度值为:高温 35度,如果想只保留数字,就需要设置为 \\\D

filter的值为正则表达。系统会自动过滤掉通过正则表达式选取的内容。

*常用正则:*


`\\D`只保留数字

`\s.* `
以空格开始的所有字符

`[u4e00-\u9fa5]`
选择所有汉字

`[^\u4e00-\u9fa5]^[-,.?:;'\"!']`
选择所有非汉字,但是不包括-,.?:;'"!'这些标点符号



`^((?!abc).)*admin((?!abc).)*$`
包含admin且不包含abc。
5 changes: 5 additions & 0 deletions api-srv/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#WEATHER API
对外暴露API服务



63 changes: 63 additions & 0 deletions api-srv/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
"github.com/gogf/gf/os/glog"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/web"
_ "github.com/micro/go-plugins/registry/consul/v2"
_ "github.com/micro/go-plugins/registry/eureka/v2"
weather "ms-weather/weather-srv/proto"
"ms-weather/weather-srv/units"
)

var (
cl weather.WeatherService
)

//定义api控制器
type Controller struct{}

//Now 获取天气实况信息的api接口
func (c *Controller) Now(r *ghttp.Request) {
cityCode := r.GetString("cityCode")
city := r.GetString("city")
dataReq := weather.DataReq{AppSecret: "", CityCode: cityCode, City: city}

//nowData, err := cl.Now(context.TODO()
nowData, err := cl.Now(r.Context(), &dataReq)

if err != nil {
units.Json(r, 1, err.Error(), "")
}

units.Json(r, 0, "", nowData.Data)
}

//Forecast 获取天气预报信息的api接口
func (c *Controller) Forecast(r *ghttp.Request) {
r.Response.Write("天气预报")
}

func main() {
// 创建服务
service := web.NewService(
web.Name("go.micro.api.weather"),
)
service.Init()
// 创建客户端链接应用服务
cl = weather.NewWeatherService("go.micro.srv.weather", client.DefaultClient)

// 创建Http API服务
s := g.Server()
c := new(Controller)
s.BindObject("/weather", c)

// 注册 Handler
service.Handle("/", s)
// 运行微服务
if err := service.Run(); err != nil {
glog.Fatal(err)
}
}
67 changes: 67 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/sh

servername=weather
function help() {
echo "$0 linux|windows|mac"
}

function linux(){
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build

}
function windows(){
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build

}
function mac(){
go build

}

cd weather-srv

if [ "$1" == "" ]; then
help
elif [ "$1" == "linux" ];then
linux
elif [ "$1" == "windows" ];then
windows
elif [ "$1" == "mac" ];then
mac
fi

cd ../

cd api-srv

if [ "$1" == "" ]; then
help
elif [ "$1" == "linux" ];then
linux
elif [ "$1" == "windows" ];then
windows
elif [ "$1" == "mac" ];then
mac
fi

cd ../

mkdir -p bin/config
mkdir -p bin/db

cp -r config/. bin/config/
cp -r db/weather.sqlite bin/db/


if [ "$1" == "windows" ]; then
cp weather-srv/weather-srv.exe bin/
cp api-srv/api-srv.exe bin/
rm -f weather-srv/weather-srv.exe
rm -f api-srv/api-srv.exe

else
cp weather-srv/weather-srv bin/
cp api-srv/api-srv bin/
rm -f weather-srv/weather-srv
rm -f api-srv/api-srv
fi
25 changes: 25 additions & 0 deletions config/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 服务系统设置
[system]
serverName = "city_weather"
#接口源配置文件存放目录,绝对路径
sourceConfigPath = "./config/source"
#启用的源文件名称,如果为空就是全部,空:[] ,赋值例值["xxxx.json","xxxx.json"]
sourceFiles = []
#数据缓存时间,单位为分钟
dataCacheTime = 1
#是否启用数据库保存
isToDb = true

[logger]
path = "./log"
level = "ALL"
stdout = false
RotateExpire = "1d"
RotateBackupLimit = 1
RotateBackupExpire = "7d"

# 数据库配置
[database]
[[database.default]]
link = "./db/weather.sqlite"
type = "sqlite"
48 changes: 48 additions & 0 deletions config/source/cmacn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"sourceCode": "cmacn",
"sourceName": "国家气像中心",
"sourceApi": "http://api.data.cma.cn:8090/api?userId={{ .userId }}&pwd={{ .pwd }}&dataFormat=json&interfaceId=getSurfEleByTimeRangeAndStaID&dataCode=SURF_CHN_MUL_HOR&timeRange=[20200604040000,20200604040000]&staIDs=54765&elements=Station_Id_C,Year,Mon,Day,Hour,PRS,PRS_Sea,PRS_Max,PRS_Min,TEM,TEM_Max,TEM_Min,RHU,RHU_Min,VAP,PRE_1h,WIN_D_INST_Max,WIN_S_Max,WIN_D_S_Max,WIN_S_Avg_2mi,WIN_D_Avg_2mi,WEP_Now,WIN_S_Inst_Max,tigan,windpower,VIS,CLO_Cov,CLO_Cov_Low,CLO_COV_LM",
"param": {
"userId": "591246327162NaSig",
"pwd": "SQGThji"
},
"now":{
"CityCode":"cityid",
"City":"city",
"Date":"DS.0.Day",
"UpdateTime":"requestTime",
"Tem":"DS.0.TEM",
"TemHigh":"DS.0.TEM_Max",
"TemLow":"DS.0.TEM_Min",
"CondCodeDay":"",
"CondTxtDay":"",
"CondCodeNight":"",
"CondTxtNight":"",
"Pm25":"air_pm25",
"Pm10":"data.pm10",
"Aqi":"air",
"Quality":"air_level",
"WindDeg":"WIN_D_Avg_2mi",
"WindDir":"win",
"WindSc":"windpower",
"WindSpd":"DS.0.WIN_S_Avg_2mi",
"Pres":"pressure",
"PresSea":"PRS_Sea",
"Vis":"DS.0.VIS",
"Type":"WEP_Now",
"Cold":"",
"Notice":"air_tips",
"Source":"",
"CreateTime":""
},
"forecast": {

},
"filter": {
"Tem": "\\D",
"TemHigh": "\\D",
"TemLow": "\\D",
"Vis": "^[0-9]+(.[0-9]{1,2})?$",
"WindSpd": "^[0-9]+(.[0-9]{1,2})?$"
}
}
Loading

0 comments on commit fa5c65c

Please sign in to comment.