# getweather **Repository Path**: phpjishu/getweather ## Basic Information - **Project Name**: getweather - **Description**: 每小时获取门店实时天气 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-03-12 - **Last Updated**: 2026-03-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 门店天气采集服务 每小时采集门店当地天气数据和每日采集天气预报数据的 Go 程序。 ## 功能特性 - 每小时自动采集所有门店当前天气 - 每天0点自动采集未来5天/3小时天气预报 - 门店列表内存缓存,每天0点自动刷新 - 多 API Key 轮询,解决调用额度限制 - 支持 Docker 部署 ## 环境要求 - Go 1.26+ - MySQL 5.7+ - OpenWeatherMap API Key ## 快速开始 ### 1. 克隆项目 ```bash git clone cd getweather ``` ### 2. 配置环境变量 ```bash cp .env.example .env ``` 编辑 `.env` 文件,配置数据库和 API Key: ```env # 数据库配置 MYSQL_HOST=localhost MYSQL_PORT=3306 MYSQL_USER=root MYSQL_PASSWORD=your_password MYSQL_DATABASE=weather_db # 天气API配置 (OpenWeatherMap 2.5 免费版本) # 多个API Key用逗号分隔,会自动轮询使用 # 免费版每个Key每天1000次调用,300家店 * 24次/天 = 7200次请求,建议至少配置8个Key WEATHER_API_KEYS=your_api_key_1,your_api_key_2 WEATHER_API_URL=https://api.openweathermap.org/data/2.5/weather WEATHER_API_URL_FORECAST=https://api.openweathermap.org/data/2.5/forecast WEATHER_API_TIMEOUT=30 ``` ### 3. 创建数据库 ```sql CREATE DATABASE weather_db CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; ``` ### 4. 创建数据表 执行以下 SQL 创建数据表: ```sql -- 天气日志表 CREATE TABLE `qc_weather_log` ( `weather_log_id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID', `store_id` int unsigned NOT NULL COMMENT '门店ID,关联qc_store表', `store_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '门店名称,冗余存储便于查询', `city_id` int DEFAULT NULL COMMENT '城市ID', `temperature` decimal(5,2) DEFAULT NULL COMMENT '当前温度,单位:摄氏度', `feels_like` decimal(5,2) DEFAULT NULL COMMENT '体感温度,单位:摄氏度', `humidity` int DEFAULT NULL COMMENT '空气湿度,单位:百分比(0-100)', `pressure` int DEFAULT NULL COMMENT '大气压强,单位:hPa(百帕)', `wind_speed` decimal(5,2) DEFAULT NULL COMMENT '风速,单位:米/秒', `wind_direction` int DEFAULT NULL COMMENT '风向,单位:度(0-360,0=北,90=东,180=南,270=西)', `weather_main` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '天气主要类型', `weather_desc` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '天气详细描述', `visibility` int DEFAULT NULL COMMENT '能见度,单位:米', `cloudiness` int DEFAULT NULL COMMENT '云量,单位:百分比(0-100)', `recorde_day` date NOT NULL COMMENT '天气数据记录日期', `recorde_hour` int NOT NULL COMMENT '天气数据记录小时', `create_time` datetime NOT NULL COMMENT '记录创建时间', `update_time` datetime DEFAULT NULL COMMENT '记录更新时间', PRIMARY KEY (`weather_log_id`), KEY `idx_store_id` (`store_id`), KEY `recorde_day` (`recorde_day`), KEY `recorde_hour` (`recorde_hour`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='天气日志表'; -- 天气预报表 CREATE TABLE `qc_weather_forecast` ( `forecast_id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID', `store_id` int unsigned NOT NULL COMMENT '门店ID,关联qc_store表', `store_name` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '门店名称,冗余存储便于查询', `city_id` int DEFAULT NULL COMMENT '城市ID', `forecast_time` datetime NOT NULL COMMENT '预报时间', `temperature` decimal(5,2) DEFAULT NULL COMMENT '温度,单位:摄氏度', `feels_like` decimal(5,2) DEFAULT NULL COMMENT '体感温度,单位:摄氏度', `humidity` int DEFAULT NULL COMMENT '湿度,单位:百分比(0-100)', `pressure` int DEFAULT NULL COMMENT '压强,单位:hPa', `wind_speed` decimal(5,2) DEFAULT NULL COMMENT '风速,单位:米/秒', `wind_direction` int DEFAULT NULL COMMENT '风向,单位:度', `weather_main` varchar(50) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '天气类型', `weather_desc` varchar(200) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '天气描述', `cloudiness` int DEFAULT NULL COMMENT '云量,单位:百分比', `create_time` datetime NOT NULL COMMENT '创建时间', `update_time` datetime DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`forecast_id`), KEY `idx_store_id` (`store_id`), KEY `idx_forecast_time` (`forecast_time`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='天气预报表'; ``` ### 5. 运行程序 #### 本地运行 ```bash go run . ``` #### Docker 运行 ```bash docker-compose up -d ``` ## 使用说明 ### 命令行参数 - `-run-once`: 立即执行一次天气采集,然后退出 ```bash ./getweather.exe -run-once ``` ### 定时任务 - 每小时:采集所有门店当前天气 - 每天0点:刷新门店缓存 + 采集天气预报 ## 项目结构 ``` getweather/ ├── config/ # 配置管理 ├── model/ # 数据模型 ├── scheduler/ # 定时任务调度 ├── service/ # 业务逻辑 ├── store/ # 门店缓存 ├── main.go # 程序入口 ├── Dockerfile # Docker 构建文件 ├── docker-compose.yml ├── .env.example └── .gitignore ``` ## API Key 配置说明 OpenWeatherMap 免费版每个 API Key 每天有 1000 次调用限制。 计算公式: - 当前天气:300家店 × 24次/天 = 7200次/天 - 天气预报:300家店 × 8次/天 = 2400次/天 - 总计:约 10000次/天 建议:至少配置 **10个** API Key ## 许可证 MIT