Skip to content

Commit fe747e9

Browse files
committed
1
1 parent a9c36a6 commit fe747e9

File tree

1 file changed

+208
-75
lines changed

1 file changed

+208
-75
lines changed

README.md

Lines changed: 208 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,240 @@
1+
2+
3+
#### 微服务统计,分析,图表,监控一体化的HttpReports项目在.Net Core 中的使用
4+
15

26
# HttpReports
3-
### 简单介绍
4-
HttpReports 是 .Net Core下的一个Web组件,适用于 WebAPI 项目和 API 网关项目,通过中间件的形式集成到您的项目中, 通过HttpReports,可以让开发人员快速的搭建出一个 API 性能分析的基础报表网站。
7+
### 简单介绍
8+
HttpReports 是 .Net Core 下的一个Web组件, 适用于WebAPI,Ocelot网关应用,MVC项目,非常适合针对微服务应用使用,通过中间件的形式集成到您的项目中,可以让开发人员快速的搭建出一个 数据统计,分析,图表,监控 一体化的 Web站点。
9+
10+
[](https://images.cnblogs.com/cnblogs_com/myshowtime/1627540/o_a1.png)
11+
12+
[](https://images.cnblogs.com/cnblogs_com/myshowtime/1627540/o_a2.png)
13+
14+
[](https://images.cnblogs.com/cnblogs_com/myshowtime/1627540/o_a3.png)
15+
516

6-
![](https://raw.githubusercontent.com/SpringLeee/HttpReportsWeb/master/HttpReports.Web/wwwroot/Content/img/git/a3.png)
17+
#### 主要模块
718

8-
主要包含 HttpReports 中间件 和 HttpReports.Web 报表项目:
19+
主要包含HttpReports 中间件 和 HttpReports.Web 的MVC项目;
920

10-
HttpReports: https://github.com/SpringLeee/HttpReports
21+
HttpReports: https://github.com/SpringLeee/HttpReports
1122

12-
HttpReports.Web: https://github.com/SpringLeee/HttpReportsWeb
23+
HttpReports.Web: https://github.com/SpringLeee/HttpReportsWeb
24+
25+
在线预览: http://175.102.11.117:8801 账号 admin 密码admin
26+
27+
#### 支持项目类型
28+
29+
😂 单个WebAPI应用
30+
😆 多个独立WebAPI应用
31+
😊 Ocelot 网关应用
32+
😛 单个MVC项目
33+
😃 多个MVC项目
34+
35+
36+
### 如何使用
1337

14-
### 如何使用
38+
##### 1.添加 HttpReports 中间件
1539

16-
##### 1.运行 HttpReports.Web
17-
在github下载 HttpReports.Web 项目,项目地址:https://github.com/SpringLeee/HttpReportsWeb, Web项目是.Net Core MVC 项目,使用三层实现。
40+
Nuget 包安装 HttpReports, 打开Startup.cs, 修改 ConfigureServices(IServiceCollection services) 方法,添加以下代码,放在 services.AddMvc() 之前都可以。
41+
42+
选择您的应用类型:
1843

19-
![](https://raw.githubusercontent.com/SpringLeee/HttpReportsWeb/master/HttpReports.Web/wwwroot/Content/img/git/a1.png)
44+
😆 **单个WebAPI应用 或者 使用Ocelot网关的应用**
2045

46+
修改 ConfigureServices 方法 ,
2147

48+
```csharp
49+
public void ConfigureServices(IServiceCollection services)
50+
{
51+
// 添加HttpReports中间件
52+
services.AddHttpReportsMiddlewire(WebType.API, DBType.SqlServer);
53+
54+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
55+
}
56+
```
57+
😆 ** 多个独立的WebAPI应用 **
58+
59+
假设有一个 授权(Auth)API应用,和一个支付(Pay)API应用,并且没有使用网关,需要分别在两个项目的Startup.cs文件的 ConfigureServices 方法中分别添加以下代码:
2260

23-
下载完成后,在VS中打开,然后还原NuGet程序包,完成后首先 appsettings.json
24-
#### appsettings.json
61+
###### 授权API应用(Auth)
62+
```csharp
63+
services.AddHttpReportsMiddlewire(WebType.API, DBType.SqlServer,"Auth");
64+
```
65+
###### 支付Pay应用(Pay)
66+
```csharp
67+
services.AddHttpReportsMiddlewire(WebType.API, DBType.SqlServer,"Pay");
2568
```
26-
{
27-
"ConnectionStrings": {
28-
"HttpReports": "Max Pool Size = 512;server=.;uid=sa;pwd=123456;database=HttpReports;"
29-
},
30-
"HttpReportsConfig": {
31-
"DBType": "SqlServer",
32-
"UserName": "admin",
33-
"Password": "123456"
34-
}
35-
}
3669

37-
```
38-
主要参数:
39-
- HttpReports:配置一个可用的连接字符串;
40-
- DBType:数据库类型,支持SqlServer和MySql;
41-
- UserName: Web项目的登录名;
42-
- Password: Web项目的登录密码;
70+
😆 **单个MVC应用**
4371

44-
假设我们使用的是SqlServer 数据库,需要先配置ConnectionStrings,然后手动创建数据库 HttpReports(Web项目会根据数据库自动创建表,并且在第一次运行的时候Mock一些数据 ),我们直接F5运行项目, 没有问题的话,会直接跳到登录页面,输入用户名密码 admin 123456,登录后,应该可以看到下面的页面
72+
```csharp
73+
public void ConfigureServices(IServiceCollection services)
74+
{
75+
// 添加HttpReports中间件
76+
services.AddHttpReportsMiddlewire(WebType.MVC, DBType.SqlServer);
4577

46-
![](https://raw.githubusercontent.com/SpringLeee/HttpReportsWeb/master/HttpReports.Web/wwwroot/Content/img/git/a3.png)
47-
48-
现在可以看到项目有 auth,payment,sms 三个服务节点,服务节点的定义如下:
49-
50-
请求地址 | 服务节点 | 说明
51-
-|-|-
52-
https://www.abc.com/auth/api/user/login | auth |
53-
https://www.abc.com/log/api/user/login | log |
54-
https://www.abc.com/api/user/login | default | 如果没有前缀的话,就是default节点
78+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
79+
}
80+
```
81+
82+
😆 **多个MVC应用**
83+
84+
假设有一个 电商(Mall)应用,和一个支付(Pay)应用,需要分别在两个项目的Startup.cs文件的 ConfigureServices 方法中分别添加以下代码:
85+
86+
###### 电商MVC应用 (Mall)
87+
```csharp
88+
services.AddHttpReportsMiddlewire(WebType.MVC, DBType.SqlServer,"Mall");
89+
```
90+
###### 支付MVC应用 (Pay)
91+
```csharp
92+
services.AddHttpReportsMiddlewire(WebType.MVC, DBType.SqlServer,"Pay");
93+
```
94+
😆 **切换数据库**
95+
96+
使用MySql数据库
97+
```csharp
98+
services.AddHttpReportsMiddlewire(WebType.API, DBType.MySql);
99+
```
100+
使用SqlServer数据库
101+
```csharp
102+
services.AddHttpReportsMiddlewire(WebType.API, DBType.SqlServer);
103+
```
104+
105+
##### 2.使用 HttpReports 中间件
55106

56-
如果你的项目是单个WebAPI项目,那么服务节点只有一个 default,如果你的项目是 GateWay 网关项目,那么Web项目就可以读取到多个服务节点,比如 auth 认证,payment支付等。
107+
修改 StartUp.cs 的 Configure 方法
57108

109+
.Net Core 2.2
58110

59-
##### 2.在API项目中使用
111+
```csharp
112+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
113+
{
114+
//使用HttpReports
115+
app.UseHttpReportsMiddlewire();
60116

61-
首先要删除 Web 项目的Mock数据,打开数据库 HttpReports,打开表 RequestInfo,清空数据,执行Sql
117+
app.UseMvc();
118+
}
62119
```
63-
Delete * From [HttpReports].[dbo].[RequestInfo]
120+
必须要放在 UseMVC() 方法和其他中间件的前边,否则不生效。
121+
122+
.Net Core 3.0 和以上版本
123+
124+
```csharp
125+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
126+
{
127+
//使用HttpReports
128+
app.UseHttpReportsMiddlewire();
129+
130+
app.UseRouting();
131+
132+
app.UseAuthorization();
133+
134+
app.UseEndpoints(endpoints =>
135+
{
136+
endpoints.MapControllers();
137+
});
138+
}
139+
```
140+
必须要放在 UseEndpoints() 方法和其他中间件的前边,否则不生效。
141+
142+
##### 3. appsettings.json 配置连接字符串
143+
144+
打开 appsetting.json, 添加数据库连接字符串, 需要手动创建数据库 HttpReports
145+
146+
```csharp
147+
"ConnectionStrings": {
148+
"HttpReports": "Max Pool Size = 512;server=.;uid=sa;pwd=123456;database=HttpReports;"
149+
}
64150
```
65-
###### 配置数据库连接字符串
66-
HttpReports 适用单个API项目和网关项目,这里使用 Ocelot网关项目为例.
151+
##### 4. 运行Web应用
152+
到这一步,已经配置完成了, 直接运行Web应用,如果中间有报错的话,可能是因为数据库的连接问题,请检查后再重试,如果没有报错的话,打开数据库 [HttpReports].[dbo].[RequestInfo], 如果能看到有数据记录,就说明 HttpReports 中间件的部分配置完成了,数据有了,下边开始配置 HttpReportsWeb 站点。
153+
154+
------------
155+
156+
#### HttpReports.Web部分
67157

68-
我们打开appsetting.json, 配置数据库连接字符串,需要和Web项目一致
158+
github源码:https://github.com/SpringLeee/HttpReportsWeb 有需要的也可以下载源码后编译,默认的git分支是Core 2.2 版本,还有一个 core 3.0的分支;
69159

70-
![](https://raw.githubusercontent.com/SpringLeee/HttpReportsWeb/master/HttpReports.Web/wwwroot/Content/img/git/a6.png)
71160

72-
###### Nuget引用HttpReports
73161

74-
安装nuget包 **HttpReports** ,打开StartUp
162+
这里提供 core2.2 和 3.0 的发布版本下载:
163+
164+
Core 2.2 发布版本: https://files.cnblogs.com/files/myshowtime/HttpReports2.2.zip
165+
Core 3.0 发布版本:https://files.cnblogs.com/files/myshowtime/HttpReports3.0.zip
166+
167+
这里以 .Net Core2.2 版本为例, 下载发布版本后,解压文件, 找到 appsettings.json文件,并修改
75168

76-
在ConfigureServices 方法下添加:
77169
```csharp
78-
services.AddHttpReportsMiddlewire();
170+
{
171+
"ConnectionStrings": {
172+
"HttpReports": "Max Pool Size = 512;server=.;uid=sa;pwd=123456;database=HttpReports;"
173+
},
174+
"HttpReportsConfig": {
175+
"DBType": "SqlServer", // MySql Or SqlServer
176+
"UserName": "admin",
177+
"Password": "123456"
178+
}
179+
}
79180
```
80-
如果是MySql数据库,则添加:
181+
| 字段 | 说明 |
182+
| ------------ | ------------ |
183+
| HttpReports | 数据库连接字符串,要和上边配置的中间件的数据库一致 |
184+
| DBType | 数据库类型 SqlServer MySql , 注意没有空格 |
185+
| UserName | Web站点后台登录名,可修改 |
186+
| Password | Web站点后台登录密码,可修改 |
187+
188+
修改数据库类型和连接字符串, 然后打开命令行,启动程序,或者部署到站点也可以
81189
```csharp
82-
services.AddHttpReportsMiddlewire(options =>{
83-
options.DBType = DBType.MySql;
84-
});
190+
dotnet HttpReports.Web.dll
85191
```
192+
跳到登录页,输入默认账号 admin 密码 123456,登录到系统,看一下主要的几个页面
193+
194+
#### 主页面
86195

87-
加入到 Configure 方法 ,需要放在 app.UseMVC() 或者 app.UseOcelot().Wait() 的前面,要不然不生效
88-
```csharp
89-
app.UseHttpReportsMiddlewire();
90-
```
91-
ConnectionStrings 配置的连接字符串和数据库类型要一致,全部完成了以后,我们就可以使用 Web 项目了。
196+
主要是Web应用 请求次数, 请求时间, 请求错误,错误率TOP, 响应最快和响应最慢等, 按天,月,年进行趋势分析, 服务节点 点击可以选中和取消,并且可以切换亮色和暗色主题
92197

93-
### 项目环境基本要求
198+
![](https://images.cnblogs.com/cnblogs_com/myshowtime/1627540/o_a5.png)
94199

95-
WebAPI或者网关项目支持的.Net Core 版本 2.2, 3.0, 3.1;
200+
#### 预警监控
96201

97-
HttpReports.Web 的core版本为 2.2
202+
![](https://images.cnblogs.com/cnblogs_com/myshowtime/1627540/o_a6.png)
98203

99-
### 性能事项
204+
HttpReports 监控预警主要针对以下几点:
100205

101-
HttpReports 中间件是异步操作,所以对api接口请求的时间可以忽略,但是由于实质使用的是数据库存储,所以要注意直接请求到数据库的压力。
206+
😃 响应超时
207+
😃 请求错误
208+
😃 IP异常
209+
😃 请求量监控
210+
211+
**如何添加监控:**
212+
213+
![](https://images.cnblogs.com/cnblogs_com/myshowtime/1627540/o_a7.png)
214+
215+
这里演示添加一个监控,监控频率 选1小时,也就是1个小时 运行一次, 然后填入预警的收件邮箱,可填写多个邮箱, 服务节点 可以选中单个和多个节点,默认的话,下边 4个监控都是关闭状态,这里 我打开 响应超时 和 请求错误监控
102216

103-
下面是用PostMan做的一个简单测试:
217+
![](https://images.cnblogs.com/cnblogs_com/myshowtime/1627540/o_a8.png)
218+
219+
220+
![](https://images.cnblogs.com/cnblogs_com/myshowtime/1627540/o_a9.png)
221+
222+
保存任务,任务自动运行,如果数据达到预警值时,您就会收到HttpReports 发送给您的通知邮件
223+
224+
225+
### 项目环境基本要求
226+
227+
WebAPI或者网关项目支持的.Net Core 版本 2.2, 3.0, 3.1;
104228

105-
WebAPI内的方法:
229+
HttpReports.Web 的core版本为 2.2
230+
231+
### 性能事项
232+
233+
HttpReports 中间件是异步操作,所以对api接口请求的时间可以忽略,但是由于实质使用的是数据库存储,所以要注意直接请求到数据库的压力。
234+
235+
下面是用PostMan做的一个简单测试:
236+
237+
WebAPI内的方法:
106238

107239
```csharp
108240
public string Sql1()
@@ -119,22 +251,23 @@ WebAPI
119251
return list1.Count().ToString();
120252
}
121253
```
122-
PostMan分别对添加中间件和不添加中间件的 API请求 1000次,每300ms请求一次
254+
PostMan分别对添加中间件和不添加中间件的 API请求 1000次,每300ms请求一次
123255

124-
说明 | 请求次数 | 平均响应时间
256+
说明 | 请求次数 | 平均响应时间
125257
-|-|-
126-
原生API|1000|32.535
127-
使用中间件|1000|32.899
258+
原生API|1000|32.535
259+
使用中间件|1000|32.899
260+
261+
### 总结
128262

129-
### 总结
263+
HttpReports 的实现原理并不复杂,如果你想给你的.Net Core 应用,快速的添加一套图表监控系统 ,那么使用HttpReports 是一个不错的选择,如果能帮助到您的话,还请希望给个Star, 感谢
130264

131-
HttpReports 的实现原理并不复杂,如果你想给你的 WebAPI项目,快速的添加一套分析系统 ,那么使用HttpReports 是一个不错的选择
132265

266+
### 问题反馈
133267

134-
### 联系作者
268+
如果您在使用过程中遇到了什么问题或者有好的建议的话,可以反馈到QQ群,也可以添加我的微信, 希望可以帮助到您
135269

136-
如果您在使用过程中遇到了什么问题或者有好的建议的话,可以添加我的微信,希望可以帮助到您
137-
![](https://raw.githubusercontent.com/SpringLeee/HttpReportsWeb/master/HttpReports.Web/wwwroot/Content/img/git/a9.jpg)
270+
![](https://images.cnblogs.com/cnblogs_com/myshowtime/1627540/o_a13.png)
138271

139272

140273

0 commit comments

Comments
 (0)