基础准备


1.创建asp.net core Web 应用程序选择Api

2.appsettings.json 配置consul服务器地址,以及本机ip和端口号信息

{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"Consul": {
"IP": "127.0.0.1",
"Port": "" },
"Service": {
"Name": "zyz"
},
"ip": "localhost",
"port": "", "AllowedHosts": "*"
}

3.程序入口(program.cs)配置useurls,ip和port从配置文件(或者命令行中)读取(命令行启动方式:dotnet ConsulServer.dll --ip localhost --port 8644)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging; namespace ConsulServer
{
public class Program
{ public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
} public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
var config = new ConfigurationBuilder().AddCommandLine(args)
.Build();//获取配置信息
return WebHost.CreateDefaultBuilder(args)
.UseUrls($"http://{ config["ip"]}:{config["port"]}")//配置ip地址和端口地址
.UseStartup<Startup>();
} }
}

4.配置基础数据,并且调用注册consul接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Consul;
using ConsulServer.ConsulApi;
using ConsulServer.entity;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; namespace ConsulServer
{
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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime)
{ if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
////配置信息单例
//ConfigSingleton.setConfigSingleton(Configuration); app.UseMvc(); ConsulEntity consulEntity = new ConsulEntity
{
ip = Configuration["ip"],
port = int.Parse(Configuration["port"] ),
ServiceName = "zyz",
ConsulIP = Configuration["Consul:IP"],
ConsulPort = Convert.ToInt32(Configuration["Consul:Port"])
};
app.RegisterConsul(lifetime, consulEntity);
} }
}

5.配置注册信息

using Consul;
using ConsulServer.entity;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace ConsulServer.ConsulApi
{
/// <summary>
/// consul
/// </summary>
public static class AppBuilderExtensions
{ /// <summary>
/// 注册consul
/// </summary>
/// <param name="app"></param>
/// <param name="lifetime"></param>
/// <param name="serviceEntity"></param>
/// <returns></returns>
public static IApplicationBuilder RegisterConsul(this IApplicationBuilder app, IApplicationLifetime lifetime, ConsulEntity serviceEntity)
{
//consul地址
Action<ConsulClientConfiguration> configClient = (consulConfig) =>
{
consulConfig.Address = new Uri($"http://{serviceEntity.ConsulIP}:{ serviceEntity.ConsulPort}");
consulConfig.Datacenter = "dc1";
};
//建立连接
var consulClient = new ConsulClient(configClient);
var httpCheck = new AgentServiceCheck()
{
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(),//服务启动多久后注册
Interval = TimeSpan.FromSeconds(),//健康监测
HTTP = string.Format($"http://{serviceEntity.ip}:{serviceEntity.port}/api/health"),//心跳检测地址
Timeout = TimeSpan.FromSeconds()
};
//注册
var registrtion = new AgentServiceRegistration()
{ Checks = new[] { httpCheck },
ID = "zyzService" + Guid.NewGuid().ToString(),//服务编号不可重复
Name = serviceEntity.ServiceName,//服务名称
Address = serviceEntity.ip,//ip地址
Port = serviceEntity.port//端口 };
//注册服务
consulClient.Agent.ServiceRegister(registrtion); return app;
} }
}

6.以命令行启动程序查看consul(dotnet ConsulServer.dll --ip localhost --port 8644)

快速入口:微服务(入门一):netcore安装部署consul

快速入口: 微服务(入门二):netcore通过consul注册服务

快速入口: 微服务(入门三):netcore ocelot api网关结合consul服务发现

快速入口:微服务(入门四):identityServer的简单使用(客户端授权)

 

微服务(入门二):netcore通过consul注册服务的更多相关文章

  1. 微服务入门二:SpringCloud(版本Hoxton SR6)

    一.什么是SpringCloud 1.官方定义 1)官方定义:springcloud为开发人员提供了在分布式系统中快速构建一些通用模式的工具(例如配置管理.服务发现.断路器.智能路由.微代理.控制总线 ...

  2. 微服务架构 | 3.4 HashiCorp Consul 注册中心

    目录 前言 1. Consul 基础知识 1.1 Consul 是什么 1.2 Consul 的特点 2. 安装并运行 Consul 服务器 2.1 下载 Consul 2.2 运行 Consul 服 ...

  3. Asp.net core 向Consul 注册服务

    Consul服务发现的使用方法:1. 在每台电脑上都以Client Mode的方式运行一个Consul代理, 这个代理只负责与Consul Cluster高效地交换最新注册信息(不参与Leader的选 ...

  4. 建立eureka服务和客户端(客户端获取已经注册服务)

    1. 新建sping boot eureka server 新建立spring starter  project 修改pom.xml文件 在parent后追加 <dependencyManage ...

  5. 1、微服务--为什么有consul,consul注册,心跳检测,服务发现

    一.为什么有consul? 在微服务,每1个服务都是集群式的,订单服务在10台服务器上都有,那么用户的请求到达,获取哪台服务器的订单服务呢?如果10台中的有的订单服务挂了怎么办?10台服务器扛不住了, ...

  6. 微服务入门三:SpringCloud Alibaba

    一.什么是SpringCloud Alibaba 1.简介 1)简介 阿里云未分布式应用开发提供了一站式解决方案.它包含了开发分布式应用程序所需的所有组件,使您可以轻松地使用springcloud开发 ...

  7. Spring Cloud Consul 实现服务注册和发现

    Spring Cloud 是一个基于 Spring Boot 实现的云应用开发工具,它为基于 JVM 的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布 ...

  8. SpringBoot系列: 使用 consul 作为服务注册组件

    本文基本上摘自纯洁的微笑的博客 http://www.ityouknow.com/springcloud/2018/07/20/spring-cloud-consul.html . 感谢作者的付出. ...

  9. 【SpringCloud】consul注册中心注册的服务为内网(局域网)IP

    一.前因 最近在做公司的一个微服务项目,技术架构为spring cloud + consul + SSM. 当我写完一个功能要在本地测试时,发现服务运行成功,但是前后端联调报500错误. 当时的第一个 ...

随机推荐

  1. java web 开发实战经典(一)

    一.jsp三种Scriptlet(脚本小程序) 1.<% %>  :定义局部变量.编写语句等. <% String str = "hello world!";// ...

  2. Java学习导航

    由于最近在系统的重新学习Java,为了便于日后复习,给个人博客中Java内容做一个目录. Java基础:Java虚拟机(JVM) Java基础:内存模型 Java基础:JVM垃圾回收算法 Java基础 ...

  3. ORC文字识别软件破解版

    下载地址:http://pan.baidu.com/s/1bnCiXdl 点击 然后可以免费用了ABBYY了!!

  4. VC++中字符串编码处理的一些相关问题

    前言 什么是tchar? 百度百科对其的定义如下": 因为C++支持两种字符串,即常规的ANSI编码(使用""包裹)和Unicode编码(使用L""包 ...

  5. SSM-Spring-17:Spring中aspectJ注解版

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- AspectJ AspectJ是一个面向切面的框架,它扩展了Java语言,定义了AOP 语法,能够在编译期提供 ...

  6. 小程序开发之图片转Base64(C#、.et)

    小程序页面代码因为某些人力不可控的代码丢失了,这里简单说明一下 调用小程序APIwx.chooseImage(OBJECT)选择相册或拍摄照片,会返回 tempFilePaths,之后通过wx.upl ...

  7. RedHat 7.0及CentOS 7.0禁止Ping的三种方法

    作者:荒原之梦 原文链接:http://zhaokaifeng.com/?p=538 前言: "Ping"属于ICMP协议(即"Internet控制报文协议") ...

  8. thymeleaf 专题

    Thymeleaf 之 内置对象.定义变量.URL参数及标签自定义属性 如标题所述,这篇文章主要讲述Thymeleaf中的内置对象(list解析.日期格式化.数字格式化等).定义变量.获取URL的参数 ...

  9. Jenkins 的安装部署

    一.Windows环境中安装Jenkins 原文:http://www.cnblogs.com/yangxia-test/p/4354328.html 在最简单的情况下,Jenkins 只需要两个步骤 ...

  10. 你不知道的JavaScript--Item28 垃圾回收机制与内存管理

    1.垃圾回收机制-GC Javascript具有自动垃圾回收机制(GC:Garbage Collecation),也就是说,执行环境会负责管理代码执行过程中使用的内存. 原理:垃圾收集器会定期(周期性 ...