利用工具查看JMS微服务在线情况,可直接调用远程方法,也可自动生成微服务客户端代码
如果在线的微服务是Debug模式,可以点击【获取客户端代码】,直接生成该微服务的客户端访问代码。
生成的客户端代码大概如下格式:(如果微服务编译时,生成.xml注释文件,那么客户端代码,也会伴有注释)
namespace DemoSys.WebApi.MicroServices { [InvokerInfo("UserAccountService")] public class UserAccountService : IImplInvoker { ..... } }
这样,客户端要访问微服务,则可以这么写
using (var client = new MicroServiceClient()) { var service = client.GetMicroService<MicroServices.UserAccountService>(); return service.GetUserAccount(this.UserInfo.userid , "USDT"); }
MicroServiceClient是RemoteClient的派生类,简单封装一下,用起来方便一些
public class MicroServiceClient : RemoteClient { static NetAddress[] GatewayAddresses; static IConfiguration Configuration; static MicroServiceClient() { var builder = new ConfigurationBuilder(); builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); Configuration = builder.Build(); ConfigurationChangeCallback(null); } static void ConfigurationChangeCallback(object p) { Configuration.GetReloadToken().RegisterChangeCallback(ConfigurationChangeCallback, null); GatewayAddresses = Configuration.GetSection("Gateways").Get<NetAddress[]>(); } public MicroServiceClient():base(GatewayAddresses , null , Global.ServiceProvider.GetService<ILogger<JMSClient>>()) { } }