Dapr - actors之构建块(7)
Important
重要
Make sure your class does not contain an call to redirect clients to the HTTPS endpoint. This will not work with actors. By design, a Dapr sidecar sends requests over unencrypted HTTP by default. The HTTPS middleware will block these requests when enabled.Startup
app.UseHttpsRedirection
请确保你的 Startup
类不包含 app.UseHttpsRedirection
将客户端重定向到 HTTPS 终结点的调用。 这不适用于Actor。 按照设计,默认情况下,Dapr 挎斗通过未加密的 HTTP 发送请求。 启用后,HTTPS 中间件会阻止这些请求。
The class is also the place to register the specific actor types. In the example below, registers the using :Startup
ConfigureServices
ScoreActor
services.AddActors
Startup
类也是用于注册特定执行组件类型的位置。 在下面的示例中, ConfigureServices
ScoreActor
使用注册 services.AddActors
:
C#
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddActors(options =>
{
options.Actors.RegisterActor<ScoreActor>();
});
}
At this point, the ASP.NET Core service is ready to host the and accept incoming requests. Client applications use actor proxies to invoke operations on actors. The following example shows how a console client application invokes the operation on a instance:ScoreActor
IncrementScoreAsync
ScoreActor
此时,ASP.NET Core 服务已准备好承载 ScoreActor
和接受传入的请求。 客户端应用程序使用Actor代理来调用Actor上的操作。 下面的示例演示了控制台客户端应用程序如何 IncrementScoreAsync
对实例调用操作 ScoreActor
:
C#
static async Task MainAsync(string[] args)
{
var actorId = new ActorId("scoreActor1");
var proxy = ActorProxy.Create<IScoreActor>(actorId, "ScoreActor");
var score = await proxy.IncrementScoreAsync();
Console.WriteLine($"Current score: {score}");
}
The above example uses the Dapr.Actors
package to call the actor service. To invoke an operation on an actor, you need to be able to address it. You'll need two parts for this:
上面的示例使用 Dapr.Actors
包来调用Actor 服务。 若要在Actor 上调用操作,需要能够解决该操作。 此操作需要两个部分:
- The actor type uniquely identifies the actor implementation across the whole application. By default, the actor type is the name of the implementation class (without namespace). You can customize the actor type by adding an to the implementation class and setting its property.
ActorAttribute
TypeName
- The uniquely identifies an instance of an actor type. You can also use this class to generate a random actor id by calling .
ActorId
ActorId.CreateRandom
- actor type 在整个应用程序中唯一标识执行组件实现。 默认情况下,actor 类型是 (没有命名空间) 的实现类的名称。 可以通过将添加
ActorAttribute
到实现类并设置其属性,自定义参与者类型TypeName
。 ActorId
唯一标识actor 类型的实例。 还可以通过调用来使用此类生成随机执行组件 idActorId.CreateRandom
。
The example uses to create a proxy instance for the . The method takes two arguments: the identifying the specific actor and the actor type. It also has a generic type parameter to specify the actor interface that the actor type implements. As both the server and client applications need to use the actor interfaces, they're typically stored in a separate shared project.ActorProxy.Create
ScoreActor
Create
ActorId
该示例使用 ActorProxy.Create
为ScoreActor
创建代理实例 。 Create
方法采用两个参数:标识特定Actor和actor ActorId
类型。 它还具有一个泛型类型参数,用于指定actor类型所实现的actor接口。 由于服务器和客户端应用程序都需要使用actor 接口,它们通常存储在单独的共享项目中。
The final step in the example calls the method on the actor and outputs the result. Remember that the Dapr placement service distributes the actor instances across the Dapr sidecars. Therefore, expect an actor call to be a network call to another node.IncrementScoreAsync
该示例中的最后一个步骤调用 Actor上的方法IncrementScoreAsync
并输出结果。 请记住,Dapr placement 服务跨 Dapr 分支分发actor 实例。 因此,需要将actor 调用作为对另一个节点的网络调用。
Call actors from ASP.NET Core clients
从 ASP.NET Core 客户端调用参与者
The console client example in the previous section uses the static method directly to get an actor proxy instance. If the client application is an ASP.NET Core application, you should use the interface to create actor proxies. The main benefit is that it allows you to manage configuration centrally in the method. The method takes a delegate that allows you to specify actor runtime options, such as the HTTP endpoint of the Dapr sidecar. The following example specifies custom to use for actor state persistence and message deserialization:ActorProxy.Create
IActorProxyFactory
ConfigureServices
AddActors
JsonSerializerOptions
上一部分中的控制台客户端示例直接使用静态 ActorProxy.Create
方法获取Actor 代理实例。 如果客户端应用程序是 ASP.NET Core 应用程序,则应使用 IActorProxyFactory
接口创建Actor 代理。 主要优点是它允许您集中管理方法中的配置 ConfigureServices
. AddActors
方法采用一个委托,该委托允许指定actor 运行时选项,如 Dapr 挎斗的 HTTP 终结点。
- 点赞
- 收藏
- 关注作者
评论(0)