网站全站优化智慧团建初始密码是什么

当前位置: 首页 > news >正文

网站全站优化,智慧团建初始密码是什么,390元做微信小程序 公众号 网站,电子商务网站的建设和维护论文系列文章目录 1、.Net Core微服务入门系列#xff08;一#xff09;——项目搭建 2、.Net Core微服务入门全纪录#xff08;二#xff09;——Consul-服务注册与发现#xff08;上#xff09; 3、.Net Core微服务入门全纪录#xff08;三#xff09;——Consul-服务注…系列文章目录 1、.Net Core微服务入门系列一——项目搭建 2、.Net Core微服务入门全纪录二——Consul-服务注册与发现上 3、.Net Core微服务入门全纪录三——Consul-服务注册与发现下 4、.Net Core微服务入门全纪录四——Ocelot-API网关上 5、.Net Core微服务入门全纪录五——Ocelot-API网关下 6、.Net Core微服务入门全纪录六——EventBus-事件总线 7、.Net Core微服务入门全纪录七——IdentityServer4-授权认证 8、.Net Core微服务入门全纪录八——Docker Compose与容器网络 前言 关于 微服务 的概念解释网上有很多 个人理解微服务是一种系统架构模式它和语言无关和框架无关和工具无关和服务器环境无关。 微服务思想 是将传统的单体系统按照业务拆分成多个职责单一、且可独立运行的接口服务。至于服务如何拆分没有明确的定义。几乎任何后端语言都能做微服务开发。微服务也并不是完美无缺的微服务架构会带来更多的问题增加系统的复杂度引入更多的技术栈。 上一篇【.Net Core微服务入门全纪录一——项目搭建】讲到要做到服务的灵活伸缩那么需要有一种机制来实现它这个机制就是服务注册与发现。当然这也并不是必要的如果你的服务实例很少并且很稳定那么就没有必要使用服务注册与发现。 一、服务注册与发现 服务注册简单理解就是有一个注册中心我们的每个服务实例启动时都去注册中心注册一下告诉注册中心我的地址端口等信息。同样的服务实例要删除时去注册中心删除一下注册中心负责维护这些服务实例的信息。 服务发现既然注册中心维护了各个服务实例的信息那么客户端通过注册中心就很容易发现服务的变化了。
有了服务注册与发现客户端就不用再去配置各个服务实例的地址改为从注册中心统一获取。 那注册中心又是怎么保证每个地址的可用状态呢假如某个实例挂了怎么办呢原则上挂掉的实例不应该被客户端获取到所以就要提到健康检查 。 健康检查每个服务都需要提供一个用于健康检查的接口该接口不具备业务功能。服务注册时把这个接口的地址也告诉注册中心注册中心会定时调用这个接口来检测服务是否正常如果不正常则将它移除这样就保证了服务的可用性。 常见注册中心有 Consul、ZooKeeper、etcd、Eureka。 二、Consul Consul 官网https://www.consul.io/ Consul 的主要功能有服务注册与发现、健康检查、K-V存储、多数据中心等。 Consul安装很简单直接在官网下载解压即可。Consul运行在 consul.exe 目录下打开命令行执行 consul.exe agent -dev浏览器访问http://localhost:8500/ Consul 已成功运行。 三、服务注册 首先 Nuget 安装一下 Consul 这个类库里封装了Consul的api操作方便我们直接使用。当然自己去写http调用Consul的接口也不是不行。。。接口说明https://www.consul.io/api-docs 改造一下订单服务的代码 ConsulHelper.cs public static class ConsulHelper{/// summary/// 服务注册到consul/// /summary/// param nameapp/param/// param namelifetime/parampublic static IApplicationBuilder RegisterConsul(this IApplicationBuilder app, IConfiguration configuration, IHostApplicationLifetime lifetime) {var consulClient new ConsulClient(c {//consul地址c.Address new Uri(configuration[ConsulSetting:ConsulAddress]);});var registration new AgentServiceRegistration(){ID Guid.NewGuid().ToString(),//服务实例唯一标识Name configuration[ConsulSetting:ServiceName],//服务名Address configuration[ConsulSetting:ServiceIP], //服务IPPort int.Parse(configuration[ConsulSetting:ServicePort]),//服务端口 因为要运行多个实例端口不能在appsettings.json里配置在docker容器运行时传入Check new AgentServiceCheck(){DeregisterCriticalServiceAfter TimeSpan.FromSeconds(5),//服务启动多久后注册Interval TimeSpan.FromSeconds(10),//健康检查时间间隔HTTP \(http://{configuration[ConsulSetting:ServiceIP]}:{configuration[ConsulSetting:ServicePort]}{configuration[ConsulSetting:ServiceHealthCheck]},//健康检查地址Timeout TimeSpan.FromSeconds(5)//超时时间}};//服务注册consulClient.Agent.ServiceRegister(registration).Wait();//应用程序终止时取消注册lifetime.ApplicationStopping.Register(() {consulClient.Agent.ServiceDeregister(registration.ID).Wait();});return app;}} appsettings.json {Logging: {LogLevel: {Default: Information,Microsoft: Warning,Microsoft.Hosting.Lifetime: Information}},AllowedHosts: *,ConsulSetting: {ServiceName: OrderService,ServiceIP: localhost,ServiceHealthCheck: /healthcheck,ConsulAddress: http://host.docker.internal:8500//注意docker容器内部无法使用localhost访问宿主机器如果是控制台启动的话就用localhost} } Startup.cs public class Startup{public Startup(IConfiguration configuration){Configuration configuration;}public IConfiguration Configuration { get; }// This method gets called by the runtime. Use this method to add services to the container.public void ConfigureServices(IServiceCollection services){services.AddControllers();}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseRouting();app.UseAuthorization();app.UseEndpoints(endpoints {endpoints.MapControllers();});//服务注册app.RegisterConsul(Configuration, lifetime);}} OrdersController.cs [Route([controller])][ApiController]public class OrdersController : ControllerBase{private readonly ILoggerOrdersController _logger;private readonly IConfiguration _configuration;public OrdersController(ILoggerOrdersController logger, IConfiguration configuration){_logger logger;_configuration configuration;}[HttpGet]public IActionResult Get(){string result \)【订单服务】{DateTime.Now.ToString(yyyy-MM-dd HH:mm:ss)}—— ${Request.HttpContext.Connection.LocalIpAddress}:{_configuration[ConsulSetting:ServicePort]};return Ok(result);}} HealthCheckController.cs [Route([controller])][ApiController]public class HealthCheckController : ControllerBase{/// summary/// 健康检查接口/// /summary/// returns/returns[HttpGet]public IActionResult Get(){return Ok();}} 至此就完成了服务注册取消注册健康检查等功能的代码编写。 同样的改造一下产品服务代码差不多一样就不贴了。 四、运行服务 继续在 docker 中运行服务实例不习惯 docker 的话用控制台启动也行。 –ConsulSetting:ServicePort 参数就是传入容器的端口信息。 docker build -t orderapi:1.0 -f ./Order.API/Dockerfile . docker run -d -p 9060:80 –name orderservice orderapi:1.0 –ConsulSetting:ServicePort9060 docker run -d -p 9061:80 –name orderservice1 orderapi:1.0 –ConsulSetting:ServicePort9061 docker run -d -p 9062:80 –name orderservice2 orderapi:1.0 –ConsulSetting:ServicePort9062docker build -t productapi:1.0 -f ./Product.API/Dockerfile . docker run -d -p 9050:80 –name productservice productapi:1.0 –ConsulSetting:ServicePort9050 docker run -d -p 9051:80 –name productservice1 productapi:1.0 –ConsulSetting:ServicePort9051 docker run -d -p 9052:80 –name productservice2 productapi:1.0 –ConsulSetting:ServicePort9052 至此6个服务器实例都已运行并且成功注册到 Consul。 随便停止2个服务 可以看到停止的服务已经在 Consul 中被移除。注意这个是我们停止程序时主动调用 Consul移除的。 //应用程序终止时取消注册 lifetime.ApplicationStopping.Register(() {consulClient.Agent.ServiceDeregister(registration.ID).Wait(); }); 当然程序发生异常健康检查不能正确响应的话Consul 也会移除有一点区别。 那么注册发现健康检查功能都完成了下一步就该考虑客户端如何拿到这些服务实例的地址了。