Skip to content

Commit

Permalink
Merge pull request #13 from yileicn/grpc/reflection
Browse files Browse the repository at this point in the history
Grpc/reflection
  • Loading branch information
yileicn authored Aug 25, 2021
2 parents b0b55be + 772cf65 commit e5f76f7
Show file tree
Hide file tree
Showing 24 changed files with 3,766 additions and 45 deletions.
23 changes: 23 additions & 0 deletions src/FM.GrpcDashboard/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 使用两种方法对接DashBoard
## 方法1:实现FMGrpcExtensions.proto协议
### dotnet对接GrpcDashBoard
使用FM.Grpc.Extensions或者FM.Droplet

### 其它语言对接GrpcDashBoard
需要自己实现FMGrpcExtensions.proto协议

## 方法2:使用GrpcReflection
### dotnet对接GrpcDashBoard
netcore1.x-2.x
https://github.com/grpc/grpc/blob/master/doc/csharp/server_reflection.md
netcore3.x-5.x
https://docs.microsoft.com/en-us/aspnet/core/grpc/test-tools?view=aspnetcore-3.0

### java对接GrpcDashBoard
https://github.com/grpc/grpc-java/blob/master/documentation/server-reflection-tutorial.md

### golang对接GrpcDashBoard
https://github.com/grpc/grpc-go/blob/master/Documentation/server-reflection-tutorial.md

### python对接GrpcDashBoard
https://github.com/grpc/grpc/blob/master/doc/python/server_reflection.md
3 changes: 2 additions & 1 deletion src/FM.GrpcDashboard/dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM microsoft/aspnetcore:2.0.0
WORKDIR /app
COPY ./ /app
COPY ./ /app
COPY ./grpcurl /usr/bin/
ENTRYPOINT ["dotnet", "FM.GrpcDashboard.dll"]
34 changes: 34 additions & 0 deletions src/FM.GrpcDashboard/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#sln init
PROJECT = FM.GrpcDashboard
SOLUTION_NAME = ${PROJECT_NAME}.sln

#service build
SRV_NAME = cs-grpc_dashboard-web
CURRENT_VERSION := `head -1 CHANGELOG `
TIME = $(shell date +%y%m%d%H%M)

SRV_FULL_NAME := $(SRV_NAME):$(CURRENT_VERSION).$(TIME)

build:clean
dotnet publish ./src/$(PROJECT)/$(PROJECT).csproj -c Release -o ./dist

clean:
rm -rf dist/

# 仅编译镜像,不推
build-image:build
cp Dockerfile ./dist/Dockerfile
docker build -f ./dist/Dockerfile --no-cache --rm -t $(SRV_FULL_NAME) ./dist

# 发布项目
publish:build-image
docker tag $(SRV_FULL_NAME) $(DH_URL)$(SRV_FULL_NAME)
docker login -u $(DH_USER) -p $(DH_PASS) $(DH_URL) && docker push $(DH_URL)$(SRV_FULL_NAME)

test:
echo $(DH_USER)
echo $(DH_PASS)
echo $(DH_URL)

.PHONY: gen build build-image publish clean test

Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ public static class JsonSerialization
/// <summary>
/// 使用json序列化为字符串
/// </summary>
/// <param name="input"></param>
/// <param name="dateTimeFormat">默认null,即使用json.net默认的序列化机制,如:"\/Date(1439335800000+0800)\/"</param>
/// <param name="ignoreNullValue"></param>
/// <returns></returns>
public static string ToJson(this object input, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true)
{
var settings = new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
PreserveReferencesHandling = PreserveReferencesHandling.Objects
PreserveReferencesHandling = PreserveReferencesHandling.None
};
if (ignoreNullValue)
{
settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
settings.NullValueHandling = NullValueHandling.Ignore;
};

if (!string.IsNullOrWhiteSpace(dateTimeFormat))
Expand All @@ -30,7 +32,7 @@ public static string ToJson(this object input, string dateTimeFormat = "yyyy-MM-
};
settings.Converters = jsonConverter;
}
var format = Newtonsoft.Json.Formatting.Indented;
var format = Formatting.Indented;
var json = JsonConvert.SerializeObject(input, format, settings);
return json;
}
Expand All @@ -41,6 +43,7 @@ public static string ToJson(this object input, string dateTimeFormat = "yyyy-MM-
/// <typeparam name="T"></typeparam>
/// <param name="input"></param>
/// <param name="dateTimeFormat">默认null,即使用json.net默认的序列化机制</param>
/// <param name="ignoreNullValue"></param>
/// <returns></returns>
public static T TryFromJson<T>(this string input, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true)
{
Expand Down Expand Up @@ -69,7 +72,7 @@ public static T FromJson<T>(this string input, string dateTimeFormat = "yyyy-MM-
};
if (ignoreNullValue)
{
settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
settings.NullValueHandling = NullValueHandling.Ignore;
}

if (!string.IsNullOrWhiteSpace(dateTimeFormat))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using ProtoBuf;

namespace System
{
/// <summary>
/// ProtobufExtensions
/// </summary>
public class ProtobufExtensions
{
/// <summary>
/// 序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="input"></param>
/// <returns></returns>
public static byte[] Serialize<T>(T input)
{
using (MemoryStream memoryStream = new MemoryStream())
{
Serializer.Serialize<T>((Stream)memoryStream, input);
return memoryStream.ToArray();
}
}

/// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <returns></returns>
public static T Deserialize<T>(byte[] data)
{
using (MemoryStream memoryStream = new MemoryStream(data))
return Serializer.Deserialize<T>((Stream)memoryStream);
}
}
}
75 changes: 75 additions & 0 deletions src/FM.GrpcDashboard/src/FM.GrpcDashboard/Common/ShellHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace System
{
public static class ShellHelper
{
public static string Bash(this string cmd)
{
var escapedArgs = cmd.Replace("\"", "\\\"");

var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "bash",
Arguments = $"-c \"{escapedArgs}\" 2>&1",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
string result = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();

process.WaitForExit();
return !string.IsNullOrWhiteSpace(result) ? result : error;
}

public static string Cmd(this string cmd)
{
var escapedArgs = cmd.Replace("\"", "\\\"");

var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd",
Arguments = $"/c \"{escapedArgs}\" 2>&1",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
string result = process.StandardOutput.ReadToEnd();

process.WaitForExit();
return result;
}

public static string Execute(this string exeFileName, string arguments)
{
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = exeFileName,
Arguments = arguments,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
<ItemGroup>
<PackageReference Include="Consul" Version="0.7.2.6" />
<PackageReference Include="Google.Protobuf" Version="3.6.1" />
<PackageReference Include="Grpc" Version="1.16.0" />
<PackageReference Include="Grpc" Version="1.19.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.3" />
<PackageReference Include="protobuf-net" Version="2.4.0" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0-dev-00023" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
syntax = "proto3";
option csharp_namespace = "Grpc";
package grpc;


service BaseService {
//服务基本信息(方法列表)
rpc Info(InfoRQ) returns(InfoRS);
//服务方法的详细信息
rpc MethodInfo(MethodInfoRQ) returns(MethodInfoRS);
//服务方法调用
rpc MethodInvoke(MethodInvokeRQ) returns(MethodInvokeRS);

//添加删除截流的method
rpc AddDelThrottle(AddDelThrottleRQ) returns(CmdRS);
//添加删除是否允许保存响应的method
rpc AddDelSaveResponseEnable(AddDelSaveResponseEnableRQ) returns(CmdRS);
}

message AddDelThrottleRQ {
string MethodName = 1;
bool IsDel = 2;
}

message AddDelSaveResponseEnableRQ {
string MethodName = 1;
bool IsDel = 2;
}

message InfoRQ {
string MethodName = 1;
}

message MethodInfoRQ {
string FullName = 1;
}

message MethodInvokeRQ {
string FullName = 1;
string RequestJson = 2;
}

message CmdRS {
bool Result = 1;
string Message = 2;
}

message InfoRS {
string IpAndPort = 1;
int64 StartTime = 2;
repeated MethodInfo MethodInfos = 3;
}

message MethodInfo {
bool IsThrottled = 1;
string Name = 2;
bool SaveResponseEnable = 3;
}

message MethodInfoRS {
string RequestJson = 1;
string ResponseJson = 2;
}

message MethodInvokeRS {
string ResponseJson = 1;
}
Loading

0 comments on commit e5f76f7

Please sign in to comment.