.net framework 4.5 +steeltoe+ springcloud(一) 实现服务注册功能
首先得先了解并熟悉一下springcloud,并手动去搭建一个服务中心,也可参照eurake官方示例。
如果是.net core的话,实现注册也是没有问题的,网上教程很多,可自行度娘。
最难的就是基于Framework的项目怎么实现注册,跟core的实现方式区别还是蛮大的,研究过程中也有不少坑。
下面就分享一下我踩完坑之后初步总结的实现步骤:
1.用VS2017或者其他创建NF4.5+的webAPI项目(webAPI的结构基本是保持MVC一致的,所以MVC项目基本也能按这个步骤来的)
2.在项目根目录内新建文件appsettings.json(这个文件主要是配置服务中心eurke地址等信息的):
{ "spring": { "application": { "name": "demoService" } }, "eureka": { "client": { "serviceUrl": "http://localhost:8761/eureka/",//服务中心地址 "shouldFetchRegistry": false, "shouldRegisterWithEureka": true,//是否允许注册到服务中心 "validate_certificates": false }, "instance": { "port": 3001 } }, "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Debug", "Pivotal": "Debug", "Steeltoe": "Debug" } } }
3.在项目文件夹App_Start下创建ApplicationConfig.cs:
public static class ApplicationConfig { public static IConfigurationRoot Configuration { get; set; } public static void RegisterConfig(string environment) { // Set up configuration sources. var builder = new ConfigurationBuilder() .SetBasePath(GetContentRoot()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: false) .AddJsonFile($"appsettings.{environment}.json", optional: true) .AddCloudFoundry() .AddEnvironmentVariables(); Configuration = builder.Build(); } public static string GetContentRoot() { var basePath = (string)AppDomain.CurrentDomain.GetData("APP_CONTEXT_BASE_DIRECTORY") ?? AppDomain.CurrentDomain.BaseDirectory; return Path.GetFullPath(basePath); } }
注意这里需要添加的引用:
using Microsoft.Extensions.Configuration; using Steeltoe.Extensions.Configuration.CloudFoundry;
这两个是需要在引用点右键选择NuGet程序包里添加的。
4.修改Global.asax文件,在Application_Start()内加入代码:
GlobalConfiguration.Configure(WebApiConfig.Register); var config = GlobalConfiguration.Configuration; // Build application configuration ApplicationConfig.RegisterConfig("development"); var builder = new ContainerBuilder(); // Add Microsoft Options to container builder.RegisterOptions(); // Add Microsoft Logging to container builder.RegisterLogging(ApplicationConfig.Configuration); // Add Console logger to container builder.RegisterConsoleLogging(); // Register your Web API controllers. builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); // Register IDiscoveryClient, etc. builder.RegisterDiscoveryClient(ApplicationConfig.Configuration); // Initialize and Register FortuneContext builder.RegisterInstance(SampleData.InitializeFortunes()).SingleInstance(); // Register FortuneRepository builder.RegisterType<FortuneRepository>().As<IFortuneRepository>().SingleInstance(); var container = builder.Build(); config.DependencyResolver = new AutofacWebApiDependencyResolver(container); // Get a logger from container var logger = container.Resolve <ILogger<WebApiApplication>>(); logger.LogInformation("Finished container build, starting background services"); // Start the Discovery client background thread container.StartDiscoveryClient(); logger.LogInformation("Finished starting background services");
添加引用:
using Autofac; using Autofac.Integration.WebApi; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console; using Pivotal.Discovery.Client; using Steeltoe.Common.Discovery; using Steeltoe.Common.Logging.Autofac; using Steeltoe.Common.Options.Autofac; using System; using System.Diagnostics; using System.Reflection; using System.Web.Http;