Skip to content

Commit 09527ba

Browse files
author
liguoliang
committed
修改文档
1 parent 70b65da commit 09527ba

3 files changed

Lines changed: 394 additions & 134 deletions

File tree

README.md

Lines changed: 8 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -5,141 +5,15 @@
55

66
#### 运行环境
77
<ul>
8-
<li>visual studio 2019</li>
9-
<li>.net standard 2.1</li>
10-
<li>asp.net core 3.1</li>
8+
<li>visual studio 2022</li>
9+
<li>.netstandard2.1</li>
10+
<li>.net5;.net6;.net7</li>
11+
<li>asp.net core 3.1;sp.net core 5.0;asp.net core 6.0;asp.net core 7.0</li>
1112
</ul>
1213

13-
#### Client端配置使用
14-
首先新建任意形式的.net core宿主,为了简单我使用的是Console程序,引入DotNetCoreRpc.Client包和DependencyInjection相关包
15-
```
16-
<PackageReference Include="DotNetCoreRpc.Client" Version="1.0.2" />
17-
```
18-
引入自己的服务接口包我这里是Test.IService,只需要引入interface层即可,写入如下测试代码,具体代码可参阅demo,由于DotNetCoreRpc通信是基于HttpClientFactory的,所以需要注册HttpClientFactory
19-
```cs
20-
class Program
21-
{
22-
static void Main(string[] args)
23-
{
24-
IServiceCollection services = new ServiceCollection();
25-
//*注册DotNetCoreRpcClient
26-
services.AddDotNetCoreRpcClient()
27-
//*通信基于HttpClientFactory,自行注册即可
28-
.AddHttpClient("TestServer", client => { client.BaseAddress = new Uri("http://localhost:34047/"); });
14+
#### 使用方式
15+
由于`v1.1.3之后版本`优化了一下client和server端的注册方式,所以分两个文档介绍。
16+
+ [v1.1.3及之前版本](https://github.com/softlgl/DotNetCoreRpc/blob/master/docs/1.1.3及之前版本.md)
17+
+ [v1.1.3之后版本](https://github.com/softlgl/DotNetCoreRpc/blob/master/docs/1.1.3更高版本.md)
2918

30-
IServiceProvider serviceProvider = services.BuildServiceProvider();
31-
//*RpcClient使用这个类创建具体服务代理
32-
RpcClient rpcClient = serviceProvider.GetRequiredService<RpcClient>();
3319

34-
//IPersonService是我引入的服务包interface,需要提供ServiceName,即AddHttpClient的名称
35-
IPersonService personService = rpcClient.CreateClient<IPersonService>("TestServer");
36-
PersonModel person = new PersonModel
37-
{
38-
Id = 1,
39-
Name="softlgl",
40-
IdCardNo = 5555555,
41-
BirthDay = DateTime.Now,
42-
HasMoney=false
43-
};
44-
//可以和调用本地代码一样爽了
45-
bool add = personService.Add(person);
46-
Console.WriteLine($"添加Person:{add}");
47-
person = personService.Get(1);
48-
Console.WriteLine($"获取Person,id=1,person=[{person.ToJson()}]");
49-
var persons = personService.GetPersons();
50-
Console.WriteLine($"获取Persons,id=1,persons=[{persons.ToJson()}]");
51-
personService.Delete(1);
52-
Console.WriteLine($"删除Person,id=1完成");
53-
54-
Console.ReadLine();
55-
}
56-
}
57-
```
58-
#### Server端配置使用
59-
60-
新建一个最简单的Asp.net Core项目,我这里的Demo是新建的Asp.net Core的空项目,引入DotNetCoreRpc.Server包
61-
```
62-
<PackageReference Include="DotNetCoreRpc.Server" Version="1.0.2" />
63-
```
64-
然后添加注入和相关中间件
65-
```cs
66-
public class Startup
67-
{
68-
public void ConfigureServices(IServiceCollection services)
69-
{
70-
services.AddSingleton<IPersonDal, PersonDal>()
71-
.AddSingleton<IPersonService,PersonService>()
72-
.AddSingleton(new RedisConfig { Address="127.0.0.1:6379",db=10 })
73-
.AddSingleton(new ElasticSearchConfig { Address = "127.0.0.1:9200" })
74-
//*注册DotNetCoreRpcServer
75-
.AddDotNetCoreRpcServer(options => {
76-
//*确保以下添加的服务已经被注册到DI容器
77-
78-
//添加作为服务的接口
79-
//options.AddService<IPersonService>();
80-
81-
//或添加作为服务的接口以xxx为结尾的接口
82-
//options.AddService("*Service");
83-
84-
//或添加具体名称为xxx的接口
85-
//options.AddService("IPersonService");
86-
//或具体命名空间下的接口
87-
options.AddNameSpace("Test.IService");
88-
89-
//添加全局过滤器
90-
options.AddFilter<CacheFilter>();
91-
});
92-
}
93-
94-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
95-
{
96-
//添加中间件
97-
app.UseDotNetCoreRpc();
98-
}
99-
}
100-
```
101-
如果是ASP.NET Core 6的Minimal Api则使用以下方式
102-
```cs
103-
var builder = WebApplication.CreateBuilder(args);
104-
105-
builder.Services.AddSingleton<IPersonDal, PersonDal>()
106-
.AddSingleton<IPersonService, PersonService>()
107-
.AddSingleton(new RedisConfig { Address = "127.0.0.1:6379", db = 10 })
108-
.AddSingleton(new ElasticSearchConfig { Address = "127.0.0.1:9200" })
109-
.AddDotNetCoreRpcServer(options => {
110-
//options.AddService<IPersonService>();
111-
//options.AddService("*Service");
112-
//options.AddService("IPersonService");
113-
options.AddNameSpace("Test.IService");
114-
options.AddFilter<CacheFilter>();
115-
});
116-
117-
var app = builder.Build();
118-
119-
app.UseDotNetCoreRpc();
120-
app.MapGet("/", () => "Hello World!");
121-
122-
app.Run();
123-
```
124-
过滤器的使用方式,可添加到类上或者方法上或者全局注册,优先级 方法>类>全局注册,RpcFilterAttribute是基于管道模式执行的,可支持注册多个Filter,支持属性注入。
125-
```cs
126-
public class CacheFilter : RpcFilterAttribute
127-
{
128-
private readonly ElasticSearchConfig _elasticSearchConfig;
129-
130-
//支持属性注入public private都可以
131-
[FromServices]
132-
private RedisConfig RedisConfig { get; set; }
133-
134-
public CacheFilter(ElasticSearchConfig elasticSearchConfig)
135-
{
136-
_elasticSearchConfig = elasticSearchConfig;
137-
}
138-
public override async Task InvokeAsync(RpcContext context, RpcRequestDelegate next)
139-
{
140-
Debug.WriteLine($"CacheFilter begin,Parameters={context.Parameters}");
141-
await next(context);
142-
Debug.WriteLine($"CacheFilter end,ReturnValue={context.ReturnValue.ToJson()}");
143-
}
144-
}
145-
```

docs/1.1.3及之前版本.md

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
#### Client端配置使用
2+
首先新建任意形式的.net core宿主,为了简单我使用的是Console程序,引入DotNetCoreRpc.Client包和DependencyInjection相关包
3+
```
4+
<PackageReference Include="DotNetCoreRpc.Client" Version="1.1.3" />
5+
```
6+
引入自己的服务接口包我这里是Test.IService,只需要引入interface层即可,写入如下测试代码,具体代码可参阅demo,由于DotNetCoreRpc通信是基于HttpClientFactory的,所以需要注册HttpClientFactory
7+
```cs
8+
class Program
9+
{
10+
//TestServer服务名称
11+
const string TestServerName = "TestServer";
12+
13+
static async Task Main(string[] args)
14+
{
15+
var builder = new ConfigurationBuilder()
16+
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
17+
var configuration = builder.Build();
18+
19+
IServiceCollection services = new ServiceCollection();
20+
services.AddLogging().AddDotNetCoreRpcClient()
21+
//单机版Httpclient配置
22+
.AddHttpClient(TestServerName, client => { client.BaseAddress = new Uri("http://localhost:34047/Test.Server6"); });
23+
//基于Nacos注册中心
24+
//.AddNacosV2Naming(configuration)
25+
//.AddScoped<NacosDiscoveryDelegatingHandler>()
26+
//.AddHttpClient(TestServerName, client =>
27+
//{
28+
// client.BaseAddress = new Uri($"http://{TestServerName}/Test.Server6");
29+
//}).AddHttpMessageHandler<NacosDiscoveryDelegatingHandler>();
30+
31+
IServiceProvider serviceProvider = services.BuildServiceProvider();
32+
RpcClient rpcClient = serviceProvider.GetRequiredService<RpcClient>();
33+
IPersonService personService = rpcClient.CreateClient<IPersonService>(TestServerName);
34+
35+
PersonModel person = new PersonModel
36+
{
37+
Id = 1,
38+
Name = "softlgl",
39+
IdCardNo = 5555555,
40+
BirthDay = DateTime.Now,
41+
HasMoney = false
42+
};
43+
bool add = await personService.Add(person);
44+
Console.WriteLine($"添加Person1:{add}");
45+
person = personService.Get(1);
46+
Console.WriteLine($"获取Person,id=1,person=[{person.ToJson()}]");
47+
person = new PersonModel
48+
{
49+
Id = 2,
50+
Name = "yi念之间",
51+
IdCardNo = 666888,
52+
BirthDay = DateTime.Now,
53+
HasMoney = false
54+
};
55+
add = await personService.Add(person);
56+
Console.WriteLine($"添加Person2:{add}");
57+
var persons = await personService.GetPersons();
58+
Console.WriteLine($"获取Persons,persons=[{persons.ToJson()}]");
59+
await personService.Edit(1);
60+
Console.WriteLine($"修改Person,id=1完成");
61+
personService.Delete(1);
62+
Console.WriteLine($"删除Person,id=1完成");
63+
persons = await personService.GetPersons();
64+
Console.WriteLine($"最后获取Persons,persons=[{persons.ToJson()}]");
65+
66+
IProductService productService = rpcClient.CreateClient<IProductService>(TestServerName);
67+
ProductDto product = new ProductDto
68+
{
69+
Id = 1000,
70+
Name="抗原",
71+
Price = 158.22M
72+
};
73+
int productAddResult = await productService.Add(product);
74+
Console.WriteLine($"添加Product1:{productAddResult==1}");
75+
product = productService.Get(1000);
76+
Console.WriteLine($"获取添加Product1,id=1000,person=[{product.ToJson()}]");
77+
product = new ProductDto
78+
{
79+
Id = 2000,
80+
Name = "N95口罩",
81+
Price = 35.5M
82+
};
83+
productAddResult = await productService.Add(product);
84+
Console.WriteLine($"添加Product2:{productAddResult == 1}");
85+
product = productService.Get(2000);
86+
Console.WriteLine($"获取添加Product2,id=2000,person=[{product.ToJson()}]");
87+
var products = await productService.GetProducts();
88+
Console.WriteLine($"products=[{products.ToJson()}]");
89+
Task editTask = productService.Edit(1);
90+
await editTask;
91+
Console.WriteLine($"修改Product,id=1完成");
92+
93+
Console.ReadLine();
94+
}
95+
}
96+
```
97+
#### Server端配置使用
98+
99+
新建一个最简单的Asp.net Core项目,我这里的Demo是新建的Asp.net Core的空项目,引入DotNetCoreRpc.Server包
100+
```
101+
<PackageReference Include="DotNetCoreRpc.Server" Version="1.1.3" />
102+
```
103+
然后添加注入和相关中间件
104+
```cs
105+
public class Startup
106+
{
107+
public void ConfigureServices(IServiceCollection services)
108+
{
109+
services.AddSingleton<IPersonDal, PersonDal>()
110+
.AddSingleton<IPersonService, PersonService>()
111+
.AddSingleton<IProductDal, ProductDal>()
112+
.AddSingleton<IProductService, ProductService>()
113+
.AddSingleton(new RedisConfig { Address="127.0.0.1:6379",db=10 })
114+
.AddSingleton(new ElasticSearchConfig { Address = "127.0.0.1:9200" })
115+
//*注册DotNetCoreRpcServer
116+
.AddDotNetCoreRpcServer(options => {
117+
//*确保以下添加的服务已经被注册到DI容器
118+
119+
//添加作为服务的接口
120+
//options.AddService<IPersonService>();
121+
122+
//或添加作为服务的接口以xxx为结尾的接口
123+
//options.AddService("*Service");
124+
125+
//或添加具体名称为xxx的接口
126+
//options.AddService("IPersonService");
127+
//或具体命名空间下的接口
128+
options.AddNameSpace("Test.IService");
129+
130+
//添加全局过滤器
131+
options.AddFilter<CacheFilter>();
132+
});
133+
}
134+
135+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
136+
{
137+
//通过中间件的方式引入
138+
//app.UseDotNetCoreRpc();
139+
app.UseRouting();
140+
app.UseEndpoints(endpoint => {
141+
endpoint.Map("/", async context=>await context.Response.WriteAsync("server start!"));
142+
//通过endpoint的方式引入
143+
endpoint.MapDotNetCoreRpc();
144+
//endpoint.MapDotNetCoreRpc("/Test.Server6");
145+
});
146+
}
147+
}
148+
```
149+
如果是ASP.NET Core 6的Minimal Api则使用以下方式
150+
```cs
151+
var builder = WebApplication.CreateBuilder(args);
152+
153+
builder.Services.AddSingleton<IPersonDal, PersonDal>()
154+
.AddSingleton<IPersonService, PersonService>()
155+
.AddSingleton<IProductDal, ProductDal>()
156+
.AddSingleton<IProductService, ProductService>()
157+
.AddSingleton(new RedisConfig { Address = "127.0.0.1:6379", db = 10 })
158+
.AddSingleton(new ElasticSearchConfig { Address = "127.0.0.1:9200" })
159+
.AddDotNetCoreRpcServer(options => {
160+
//options.AddService<IPersonService>();
161+
//options.AddService("*Service");
162+
//options.AddService("IPersonService");
163+
options.AddNameSpace("Test.IService");
164+
options.AddFilter<CacheFilter>();
165+
});
166+
167+
app.UseDotNetCoreRpc();
168+
//app.UseDotNetCoreRpc("/Test.Server6");
169+
app.MapGet("/", () => "Hello World!");
170+
171+
app.Run();
172+
```
173+
过滤器的使用方式,可添加到类上或者方法上或者全局注册,优先级 方法>类>全局注册,RpcFilterAttribute是基于管道模式执行的,可支持注册多个Filter,支持属性注入。
174+
```cs
175+
public class CacheFilter : RpcFilterAttribute
176+
{
177+
private readonly ElasticSearchConfig _elasticSearchConfig;
178+
179+
[FromServices]
180+
private RedisConfig RedisConfig { get; set; }
181+
182+
[FromServices]
183+
private ILogger<CacheFilter> Logger { get; set; }
184+
185+
public CacheFilter(ElasticSearchConfig elasticSearchConfig)
186+
{
187+
_elasticSearchConfig = elasticSearchConfig;
188+
}
189+
public override async Task InvokeAsync(RpcContext context, RpcRequestDelegate next)
190+
{
191+
Logger.LogInformation($"CacheFilter begin,Parameters={context.Parameters}");
192+
await next(context);
193+
Logger.LogInformation($"CacheFilter end,ReturnValue={context.ReturnValue.ToJson()}");
194+
}
195+
}
196+
```

0 commit comments

Comments
 (0)