How do you set up two microservices that both use

2019-08-27 02:13发布

问题:

I have one microservice that is a Asp.Net Core stateless service. The service runs fine and i can access at https://myazureresource.eastus.cloudapp.azure.com/controller/method.

My ServiceManifest.xml is:

<Resources>
  <Endpoints>
    <Endpoint Protocol="https" Name="EndpointHttps" Type="Input" Port="443" />      
  </Endpoints>
</Resources>

And MyService.cs is:

protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
    return new ServiceInstanceListener[]
    {
                new ServiceInstanceListener(serviceContext =>
                    new KestrelCommunicationListener(serviceContext, "EndpointHttps", (url, listener) =>
                    {
                        var transportCertificate = GetCertificate(ConfigurationManager.Instance["mycert"]);

                        return new WebHostBuilder()
                                    .UseKestrel(x =>
                                    {
                                        int port = serviceContext.CodePackageActivationContext.GetEndpoint("EndpointHttps").Port;
                                        x.Listen(IPAddress.IPv6Any, port, listenOptions =>
                                        {
                                            listenOptions.UseHttps(transportCertificate);
                                            listenOptions.NoDelay = true;
                                        });
                                    })
                                    .ConfigureServices(
                                        services => services
                                            .AddSingleton<StatelessServiceContext>(serviceContext))
                                    .UseContentRoot(Directory.GetCurrentDirectory())
                                    .UseStartup<Startup>()
                                    .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
                                    .UseUrls(url)
                                    .UseSerilog(Logger.Serilog)
                                    .Build();
                    }))
    };
}

Everything works fine however now I am not trying to create another microservice. I get an error that it can't bind to 443 obviously because it is already being used. Somehow I have to tell it to use the name of my service in the url.

I've tried many things including appending to the url:

.UserUrls(url + "/myservice")

but i get an exception that it can't bind because it is overriding and using UseKestrel endpoints. I then tried PreferHostingUrls(true) but then got an error that you have to use UsePathBase.

I then try:

.UsePathBase("myservice")

and I then get an error that it does not have a valid certificate for https. But it works with one microservice.

1. Does anyone know what you have to do to get two microservices working at the same time?

2. On a separate note (and just for curiosity) I always wondered why you would ever use dynamic ports. If the service is selecting the port how would you ever know what url to give to your clients?

Thanks for any help.