Service Tags
This tutorial will teach you the basics of creating and using service tags.
Prerequisites
- Windows 10/11
- Visual studio 2019/2022
- Phoesion Glow Blaze , with Reactor running (see getting started guide)
Sample Code
You can download the Sample Code archive from the downloads section.
This tutorial follows the 21_ServiceTags sample in the archive. (View code on GitHub)
What are Service Tags?
The Service Tagging system is a runtime routing mechanism, that allows you to route a request to a specific service instance, or group of them, based on runtime-specified tags. Tags are user-defined key-value pair strings, that can be used at runtime to group or identify service instances based on your business logic needs and requirements.
Examples
For example, as demonstrated in the 21_ServiceTags sample, we have an implementation of a cloud service for an online game called MyGame. When a user requests a new GameRoom to be created, a service tag called ServerLocation can be used to specify the desired geo-location of the server instance in order for the client to be as physically close to the server, thus reducing latency for the game session.
Creating a Service Tag
All you need to create a new service tag, is create an attribute class that derives from the abstract ServiceTagAttribute.
For example to create a ServerLocation tag :
namespace Foompany.Services.API.MyGame
{
public class ServerLocationTag : Phoesion.Glow.SDK.Firefly.ServiceTagAttribute
{
}
}
Note
The final service tag name will be the class name without the "Tag" or "TagAttribute" suffix.
Enable a Service Tag in your service
To enable a Service Tag in a service, you need to :
- Add the tag attribute at the ServiceMain class
- Add the tag attribute to your Action method
- Set a value for the tag at runtime (optional, default is null)
For example :
//ServiceMain.cs
namespace Foompany.Services.MyGame
{
[ServiceName("MyGame"), ServerLocationTag] // <-- service will use the ServerLocationTag
public class ServiceMain : FireflyService
{
protected override void Configure(IGlowApplicationBuilder app)
{
// Set server location tag value for this instance
// Notes : In a real scenario we could get this from a db or a geo-location service
ServiceTags.SetValue<ServerLocationTag>("europe"); //set location to 'europe'
}
}
}
//Modules/GameRoom.cs
namespace Foompany.Services.MyGame.Modules
{
public class GameRoom : FireflyModule
{
[Action(Methods.POST), ServerLocationTag] // <-- action will use the ServerLocationTag
public string CreateGameRoom([Required] string RoomName)
{
return $"Created game room with name {RoomName}";
}
}
}
Use a Service Tag
1. From a REST request
You can specify a tag value for a REST request using the tag:tagName=tagValue in QueryString, or set the value to a tag:tagName header.
For example :
http://localhost:16000/MyGame/GameRooms/CreateGameRoom?tag:serverlocation=europe&roomname=MyRoomName/
2. From another service using Interop
You can specify a tag value for an Interop request, using the .WithServiceTag<>() extension like so :
var result = await Call(API.MyGame.Modules.GameRooms.Actions.CreateGameRoom, room_name)
.WithServiceTag<ServerLocationTag>("europe")
.InvokeAsync();
How to test sample project
After you deploy your glow project you can test it using the following paths (assuming local deployment) :
Create a room in 'europe' :
http://localhost:16000/MyGame/GameRooms/CreateGameRoom?tag:serverlocation=europe&roomname=MyRoomName/Attempt to create a room in 'us-east'. No service instance has set this tag, so this request will fail with HTTP 502 BadGateway :
http://localhost:16000/MyGame/GameRooms/CreateGameRoom?tag:serverlocation=us-east&roomname=MyRoomName/Create a room without specifying a tag : (a world-wide random instance will be selected) :
http://localhost:16000/MyGame/GameRooms/CreateGameRoom?roomname=MyRoomName/