博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.net framework 4.5 +steeltoe+ springcloud(一) 实现服务注册功能
阅读量:4707 次
发布时间:2019-06-10

本文共 3707 字,大约阅读时间需要 12 分钟。

首先得先了解并熟悉一下springcloud,并手动去搭建一个服务中心,也可参照 。
如果是.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
().As
().SingleInstance();​ var container = builder.Build(); config.DependencyResolver = new AutofacWebApiDependencyResolver(container);​// Get a logger from container var logger = container.Resolve
>();​ 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;

 

 
 
 

转载于:https://www.cnblogs.com/troytian/p/8575444.html

你可能感兴趣的文章
数字图像处理 博客目录索引
查看>>
nodejs+redis使用
查看>>
prime算法的使用
查看>>
Jedis - hello world
查看>>
Ehcache(2.9.x) - API Developer Guide, Cache Eviction Algorithms
查看>>
把最近这些安全的问题整理一下
查看>>
【转】如何避免OOM总结
查看>>
java 类与对象
查看>>
git push 每次都要输入用户名密码
查看>>
远程桌面无法复制粘贴
查看>>
对错排认识。
查看>>
js高级程序设计——数据属性和访问器属性
查看>>
C# App.config 自定义 配置节
查看>>
Windows PowerShell
查看>>
几道前端的面试题
查看>>
进程间通信的四种方式
查看>>
Sentinel系统监控Redis主从节点
查看>>
Java设计模式之《享元模式》及应用场景
查看>>
TX2安装QT
查看>>
PHP使用curl替代file_get_contents
查看>>