开启https,接入grpc
This commit is contained in:
parent
07a6c2af8a
commit
a2cb96b9a7
1
.gitignore
vendored
1
.gitignore
vendored
@ -286,3 +286,4 @@ __pycache__/
|
|||||||
/ZRAdmin-vue/deploy.js
|
/ZRAdmin-vue/deploy.js
|
||||||
/ZR.Admin.WebApi/ZRModel.xml
|
/ZR.Admin.WebApi/ZRModel.xml
|
||||||
ZRAdmin-vue/dist.zip
|
ZRAdmin-vue/dist.zip
|
||||||
|
**/.vshistory/
|
||||||
35
ZR.Admin.Grpc/Protos/my_diary.proto
Normal file
35
ZR.Admin.Grpc/Protos/my_diary.proto
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
option csharp_namespace = "ZR.Admin.Grpc";
|
||||||
|
|
||||||
|
package my.diary;
|
||||||
|
|
||||||
|
import "google/protobuf/timestamp.proto";
|
||||||
|
|
||||||
|
// The greeting service definition.
|
||||||
|
service MyDiary {
|
||||||
|
// Sends a greeting
|
||||||
|
rpc Edit (EditRequest) returns (EditReply);
|
||||||
|
rpc Query (QueryRequest) returns (QueryReply);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The request message containing the user's name.
|
||||||
|
message EditRequest {
|
||||||
|
string content = 1;
|
||||||
|
int64 user_id = 2;
|
||||||
|
google.protobuf.Timestamp create_time = 3; // 字段名仍然建议使用 snake_case
|
||||||
|
}
|
||||||
|
|
||||||
|
// The response message containing the greetings.
|
||||||
|
message EditReply {
|
||||||
|
string result_message = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message QueryRequest {
|
||||||
|
int64 user_id = 1;
|
||||||
|
google.protobuf.Timestamp create_time = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message QueryReply {
|
||||||
|
string content = 1;
|
||||||
|
}
|
||||||
37
ZR.Admin.Grpc/Services/GreeterService.cs
Normal file
37
ZR.Admin.Grpc/Services/GreeterService.cs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
using Grpc.Core;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ZR.Admin.Grpc.Services
|
||||||
|
{
|
||||||
|
public class GreeterService : Greeter.GreeterBase
|
||||||
|
{
|
||||||
|
private readonly ILogger<GreeterService> _logger;
|
||||||
|
|
||||||
|
public GreeterService(ILogger<GreeterService> logger)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 实现 SayHello 方法
|
||||||
|
// 第一个参数是请求消息 (HelloRequest)
|
||||||
|
// 第二个参数是服务器上下文 (ServerCallContext),提供关于当前调用的信息
|
||||||
|
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
|
||||||
|
{
|
||||||
|
_logger.LogInformation($"接收到来自 {request.Name} 的请求");
|
||||||
|
|
||||||
|
// 构建并返回回复消息
|
||||||
|
var reply = new HelloReply
|
||||||
|
{
|
||||||
|
Message = $"你好,{request.Name} 来自 gRPC 服务器!"
|
||||||
|
};
|
||||||
|
|
||||||
|
// 返回一个 Task<HelloReply>
|
||||||
|
return Task.FromResult(reply);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
76
ZR.Admin.Grpc/Services/MyDiaryService.cs
Normal file
76
ZR.Admin.Grpc/Services/MyDiaryService.cs
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
using Grpc.Core;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using ZR.Service.IService;
|
||||||
|
|
||||||
|
namespace ZR.Admin.Grpc.Services
|
||||||
|
{
|
||||||
|
public class MyDiaryService(ILogger<MyDiaryService> logger, IMyDiaryService myDiaryService) : MyDiary.MyDiaryBase
|
||||||
|
{
|
||||||
|
public override async Task<EditReply> Edit(EditRequest request, ServerCallContext context)
|
||||||
|
{
|
||||||
|
logger.LogInformation($"接收到来自 {request.UserId} 的请求");
|
||||||
|
|
||||||
|
var myDiary = new Model.MyDiary()
|
||||||
|
{
|
||||||
|
Content = request.Content,
|
||||||
|
CreateTime = request.CreateTime.ToDateTime().ToLocalTime(),
|
||||||
|
UserId = request.UserId
|
||||||
|
};
|
||||||
|
|
||||||
|
var myDiaryToday = await myDiaryService.Queryable()
|
||||||
|
.Where(it => it.CreateTime.Date == myDiary.CreateTime.Date && it.UserId == myDiary.UserId)
|
||||||
|
.FirstAsync();
|
||||||
|
if (myDiaryToday != null)
|
||||||
|
{
|
||||||
|
myDiaryToday.Content = myDiary.Content;
|
||||||
|
await myDiaryService.Updateable(myDiaryToday)
|
||||||
|
.ExecuteCommandAsync();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await myDiaryService.Insertable(myDiary).ExecuteReturnSnowflakeIdAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 构建并返回回复消息
|
||||||
|
var reply = new EditReply()
|
||||||
|
{
|
||||||
|
ResultMessage = $"日记已记录 来自 gRPC 服务器!"
|
||||||
|
};
|
||||||
|
|
||||||
|
logger.LogInformation($"日记已记录");
|
||||||
|
|
||||||
|
// 返回一个 Task<HelloReply>
|
||||||
|
return reply;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task<QueryReply> Query(QueryRequest request, ServerCallContext context)
|
||||||
|
{
|
||||||
|
logger.LogInformation($"接收到来自 {request.UserId} 的请求");
|
||||||
|
|
||||||
|
var myDiary = new Model.MyDiary()
|
||||||
|
{
|
||||||
|
CreateTime = request.CreateTime.ToDateTime().ToLocalTime(),
|
||||||
|
UserId = request.UserId
|
||||||
|
};
|
||||||
|
|
||||||
|
var myDiaryToday = await myDiaryService.Queryable()
|
||||||
|
.Where(it => it.CreateTime.Date == myDiary.CreateTime.Date && it.UserId == myDiary.UserId)
|
||||||
|
.FirstAsync();
|
||||||
|
|
||||||
|
if (myDiaryToday != null)
|
||||||
|
{
|
||||||
|
myDiary.Content = myDiaryToday.Content;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建并返回回复消息
|
||||||
|
var reply = new QueryReply()
|
||||||
|
{
|
||||||
|
Content = myDiary.Content ?? ""
|
||||||
|
};
|
||||||
|
|
||||||
|
return reply;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -9,6 +9,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Protobuf Include="Protos\greet.proto" GrpcServices="Both" />
|
<Protobuf Include="Protos\greet.proto" GrpcServices="Both" />
|
||||||
<Protobuf Include="Protos\block.proto" GrpcServices="Client" />
|
<Protobuf Include="Protos\block.proto" GrpcServices="Client" />
|
||||||
|
<Protobuf Include="Protos\my_diary.proto" GrpcServices="Server" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@ -16,7 +17,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Services\" />
|
<ProjectReference Include="..\ZR.Service\ZR.Service.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@ -1,14 +1,15 @@
|
|||||||
using AspNetCoreRateLimit;
|
using AspNetCoreRateLimit;
|
||||||
using Infrastructure.Converter;
|
using Infrastructure.Converter;
|
||||||
using Microsoft.AspNetCore.DataProtection;
|
using Microsoft.AspNetCore.DataProtection;
|
||||||
using NLog.Web;
|
|
||||||
using System.Text.Json;
|
|
||||||
using Microsoft.AspNetCore.RateLimiting;
|
using Microsoft.AspNetCore.RateLimiting;
|
||||||
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
||||||
using Microsoft.Extensions.Caching.Distributed;
|
using Microsoft.Extensions.Caching.Distributed;
|
||||||
using Microsoft.Extensions.Caching.Redis;
|
using Microsoft.Extensions.Caching.Redis;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
using NLog.Web;
|
||||||
|
using System.Text.Json;
|
||||||
using ZR.Admin.Grpc;
|
using ZR.Admin.Grpc;
|
||||||
using ZR.Admin.Grpc.Extensions;
|
using ZR.Admin.Grpc.Services;
|
||||||
using ZR.Admin.WebApi.Extensions;
|
using ZR.Admin.WebApi.Extensions;
|
||||||
using ZR.Infrastructure.Cache;
|
using ZR.Infrastructure.Cache;
|
||||||
using ZR.Infrastructure.WebExtensions;
|
using ZR.Infrastructure.WebExtensions;
|
||||||
@ -107,7 +108,19 @@ builder.Services.AddSwaggerConfig();
|
|||||||
// var blockClient = new Block.BlockClient(channel);
|
// var blockClient = new Block.BlockClient(channel);
|
||||||
// var blockReply = await blockClient.GetBlockAsync(new BlockRequest { In = "asads" });
|
// var blockReply = await blockClient.GetBlockAsync(new BlockRequest { In = "asads" });
|
||||||
// Console.WriteLine("BlockReply: " + blockReply.Out);
|
// Console.WriteLine("BlockReply: " + blockReply.Out);
|
||||||
builder.Services.AddGrpcServiceConfiguration(builder.Configuration);
|
//builder.Services.AddGrpcServiceConfiguration(builder.Configuration);
|
||||||
|
|
||||||
|
// 配置 Kestrel 监听 HTTP/2 的非加密流量
|
||||||
|
//builder.WebHost.ConfigureKestrel(options =>
|
||||||
|
//{
|
||||||
|
// // 如果你只想监听一个端口,只保留其中一个 Listen
|
||||||
|
// options.ListenLocalhost(8888, o =>
|
||||||
|
// {
|
||||||
|
// o.Protocols = HttpProtocols.Http2; // 明确设置为 Http2
|
||||||
|
// });
|
||||||
|
|
||||||
|
//});
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
InternalApp.ServiceProvider = app.Services;
|
InternalApp.ServiceProvider = app.Services;
|
||||||
InternalApp.Configuration = builder.Configuration;
|
InternalApp.Configuration = builder.Configuration;
|
||||||
@ -140,7 +153,7 @@ app.UseStaticFiles();
|
|||||||
//开启路由访问
|
//开启路由访问
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
app.UseCors("Policy");//要放在app.UseEndpoints前。
|
app.UseCors("Policy");//要放在app.UseEndpoints前。
|
||||||
//app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
@ -195,4 +208,7 @@ using (var serviceScope = app.Services.CreateScope())
|
|||||||
//Console.WriteLine(helloReply);
|
//Console.WriteLine(helloReply);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.MapGrpcService<GreeterService>();
|
||||||
|
app.MapGrpcService<MyDiaryService>();
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
@ -13,7 +13,7 @@
|
|||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"dotnetRunMessages": true,
|
"dotnetRunMessages": true,
|
||||||
"launchUrl": "",
|
"launchUrl": "",
|
||||||
"applicationUrl": "http://localhost:8888",
|
"applicationUrl": "https://localhost:8888",
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
}
|
}
|
||||||
|
|||||||
20
ZR.Admin.WebApi/appsettings.Production.json
Normal file
20
ZR.Admin.WebApi/appsettings.Production.json
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
"urls": "http://[*]:8888", //项目启动url,如果改动端口前端对应devServer也需要进行修改
|
||||||
|
"dbConfigs": [
|
||||||
|
{
|
||||||
|
"Conn": "server=8.140.174.251;user=admin;pwd=admin123;database=ZrAdmin",
|
||||||
|
"DbType": 0, //数据库类型 MySql = 0, SqlServer = 1, Oracle = 3,PgSql = 4
|
||||||
|
"ConfigId": "0", //多租户唯一标识
|
||||||
|
"IsAutoCloseConnection": true
|
||||||
|
}
|
||||||
|
//...下面添加更多的数据库源
|
||||||
|
],
|
||||||
|
//redis服务配置
|
||||||
|
"RedisServer": {
|
||||||
|
"open": 1, //是否启用redis
|
||||||
|
"Cache": "8.140.174.251:6379,password=Wyd210213,defaultDatabase=0,poolsize=50,ssl=false,writeBuffer=10240,prefix=cache:",
|
||||||
|
// "Cache": "127.0.0.1:6379,password=Wyd210213,defaultDatabase=0,poolsize=50,ssl=false,writeBuffer=10240,prefix=cache:",
|
||||||
|
"Session": "8.140.174.251:6379,password=Wyd210213,defaultDatabase=0,poolsize=50,ssl=false,writeBuffer=10240,prefix=session:"
|
||||||
|
// "Session": "127.0.0.1:6379,password=Wyd210213,defaultDatabase=0,poolsize=50,ssl=false,writeBuffer=10240,prefix=session:"
|
||||||
|
},
|
||||||
|
}
|
||||||
@ -8,7 +8,7 @@
|
|||||||
},
|
},
|
||||||
"dbConfigs": [
|
"dbConfigs": [
|
||||||
{
|
{
|
||||||
"Conn": "server=8.140.174.251;user=admin;pwd=admin123;database=ZrAdmin;port=33060",
|
"Conn": "server=127.0.0.1;user=admin;pwd=admin123;database=ZrAdmin;port=3306",
|
||||||
"DbType": 0, //数据库类型 MySql = 0, SqlServer = 1, Oracle = 3,PgSql = 4
|
"DbType": 0, //数据库类型 MySql = 0, SqlServer = 1, Oracle = 3,PgSql = 4
|
||||||
"ConfigId": "0", //多租户唯一标识
|
"ConfigId": "0", //多租户唯一标识
|
||||||
"IsAutoCloseConnection": true
|
"IsAutoCloseConnection": true
|
||||||
@ -18,12 +18,12 @@
|
|||||||
//代码生成数据库配置
|
//代码生成数据库配置
|
||||||
"CodeGenDbConfig": {
|
"CodeGenDbConfig": {
|
||||||
//代码生成连接字符串,注意{dbName}为固定格式,不要填写数据库名
|
//代码生成连接字符串,注意{dbName}为固定格式,不要填写数据库名
|
||||||
"Conn": "Data Source=LAPTOP-STKF2M8H\\SQLEXPRESS;User ID=admin;Password=admin123;Initial Catalog={dbName};",
|
"Conn": "Data Source=127.0.0.1;user=admin;pwd=admin123;database=ZrAdmin;port=3306",
|
||||||
"DbType": 1,
|
"DbType": 0,
|
||||||
"IsAutoCloseConnection": true,
|
"IsAutoCloseConnection": true,
|
||||||
"DbName": "ZrAdmin" //代码生成默认连接数据库,Oracle库是实例的名称
|
"DbName": "ZrAdmin" //代码生成默认连接数据库,Oracle库是实例的名称
|
||||||
},
|
},
|
||||||
"urls": "http://localhost:8888", //项目启动url,如果改动端口前端对应devServer也需要进行修改
|
"urls": "https://localhost:8888", //项目启动url,如果改动端口前端对应devServer也需要进行修改
|
||||||
"corsUrls": [ "http://localhost:8887", "http://localhost:8886" ], //跨域地址(前端启动项目,前后端分离单独部署需要设置),多个用","隔开
|
"corsUrls": [ "http://localhost:8887", "http://localhost:8886" ], //跨域地址(前端启动项目,前后端分离单独部署需要设置),多个用","隔开
|
||||||
"JwtSettings": {
|
"JwtSettings": {
|
||||||
"Issuer": "ZRAdmin.NET", //即token的签发者。
|
"Issuer": "ZRAdmin.NET", //即token的签发者。
|
||||||
@ -102,10 +102,8 @@
|
|||||||
//redis服务配置
|
//redis服务配置
|
||||||
"RedisServer": {
|
"RedisServer": {
|
||||||
"open": 1, //是否启用redis
|
"open": 1, //是否启用redis
|
||||||
"Cache": "8.140.174.251:6379,password=Wyd210213,defaultDatabase=0,poolsize=50,ssl=false,writeBuffer=10240,prefix=cache:",
|
"Cache": "127.0.0.1:6379,defaultDatabase=0,poolsize=50,ssl=false,writeBuffer=10240,prefix=cache:",
|
||||||
// "Cache": "127.0.0.1:6379,password=Wyd210213,defaultDatabase=0,poolsize=50,ssl=false,writeBuffer=10240,prefix=cache:",
|
"Session": "127.0.0.1:6379,defaultDatabase=0,poolsize=50,ssl=false,writeBuffer=10240,prefix=session:"
|
||||||
"Session": "8.140.174.251:6379,password=Wyd210213,defaultDatabase=0,poolsize=50,ssl=false,writeBuffer=10240,prefix=session:"
|
|
||||||
// "Session": "127.0.0.1:6379,password=Wyd210213,defaultDatabase=0,poolsize=50,ssl=false,writeBuffer=10240,prefix=session:"
|
|
||||||
},
|
},
|
||||||
//验证码配置
|
//验证码配置
|
||||||
"CaptchaOptions": {
|
"CaptchaOptions": {
|
||||||
|
|||||||
23
ZR.Model/MyDiary.cs
Normal file
23
ZR.Model/MyDiary.cs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ZR.Model
|
||||||
|
{
|
||||||
|
[SugarTable("my_diary")]
|
||||||
|
public class MyDiary
|
||||||
|
{
|
||||||
|
[SugarColumn(IsPrimaryKey = true)]
|
||||||
|
[JsonConverter(typeof(ValueToStringConverter))]
|
||||||
|
public long Id { get; set; }
|
||||||
|
|
||||||
|
public string Content { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(ValueToStringConverter))]
|
||||||
|
public long UserId { get; set; }
|
||||||
|
|
||||||
|
public DateTime CreateTime { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
14
ZR.Service/IService/IMyDiaryService.cs
Normal file
14
ZR.Service/IService/IMyDiaryService.cs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using ZR.Model;
|
||||||
|
|
||||||
|
namespace ZR.Service.IService
|
||||||
|
{
|
||||||
|
public interface IMyDiaryService : IBaseService<MyDiary>
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
15
ZR.Service/MyDiaryService.cs
Normal file
15
ZR.Service/MyDiaryService.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using ZR.Model;
|
||||||
|
using ZR.Service.IService;
|
||||||
|
|
||||||
|
namespace ZR.Service
|
||||||
|
{
|
||||||
|
[AppService(ServiceType = typeof(IMyDiaryService), ServiceLifetime = LifeTime.Transient)]
|
||||||
|
public class MyDiaryService : BaseService<MyDiary>, IMyDiaryService
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user