Consul

  是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置。

  关键特性:

    服务注册/发现

    数据强一致性保证

    多数据中心

    健康检查

    key/value存储

1.下载 https://www.consul.io/downloads.html

  eg:下载的文件consul_1.6.1_windows_amd64.zip

  解压

  在path添加consul.exe所在路径

2.启动

  启动就是运行一个Consul Agent实例

  可以作为Server或Client角色运行

consul agent -dev

 

  -dev 表示开发模式运行, -server 表示服务模式运行

  说明:

    启动dev模式需要使用到的端口有

8300 服务间通信(tcp)
8301 lan cossip的端口
8302 wan gossip的端口
8500 web ui界面的端口
8600 dns通信

浏览器打开http://localhost:8500

3.spring boot 整合

spring boot 版本 2.2.1.RELEASE

(1)添加依赖

修改pom.xml

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

(2)修改配置

application.properties

server.port=8010

spring.application.name=provider
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.health-check-path=/actuator/health
spring.cloud.consul.discovery.service-name=service-provider
spring.cloud.consul.discovery.heartbeat.enabled=true management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

说明:

  spring.cloud.consul.discovery.service-name是指注册到 Consul 的服务名称,后期客户端会根据这个名称来进行服务调用

(3)修改启动类

开启服务发现支持,添加注解 @EnableDiscoveryClient

package com.xyz.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication { public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
} }

(4)添加测试方法

package com.xyz.provider.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class demoController {
@RequestMapping("/hello")
public String Hello(){
return "hello,provider";
} }

浏览器打开http://localhost:8500

spring boot2X整合Consul一服务注册与发现的更多相关文章

  1. spring boot2X整合Consul一使用RestTemplate实现服务调用

    Consul可以用于实现分布式系统的服务发现与配置 服务调用有两种方式: A.使用RestTemplate 进行服务调用 负载均衡——通过Ribbon注解RestTemplate B.使用Feign ...

  2. 一起来学Spring Cloud | 第二章:服务注册和发现组件 (Eureka)

    本篇文章,很浅显的一步步讲解如何搭建一个能运行的springcloud项目(带所有操作截图).相信!看完本篇之后,你会觉得springcloud搭建如此简单~~~~ 一. Eureka简介: 1.1  ...

  3. .netcore consul实现服务注册与发现-集群完整版

    原文:.netcore consul实现服务注册与发现-集群完整版 一.Consul的集群介绍    Consul Agent有两种运行模式:Server和Client.这里的Server和Clien ...

  4. .netcore consul实现服务注册与发现-单节点部署

    原文:.netcore consul实现服务注册与发现-单节点部署 一.Consul的基础介绍     Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其他分 ...

  5. .netcore consul实现服务注册与发现-集群部署

    一.Consul的集群介绍 Consul Agent有两种运行模式:Server和Client.这里的Server和Client只是Consul集群层面的区分,与搭建在Cluster之上的应用服务无关 ...

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

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

  7. Spring Cloud 入门教程 - Eureka服务注册与发现

    简介 在微服务中,服务注册与发现对管理各个微服务子系统起着关键作用.随着系统水平扩展的越来越多,系统拆分为微服务的数量也会相应增加,那么管理和获取这些微服务的URL就会变得十分棘手,如果我们每新加一个 ...

  8. Consul初探-服务注册和发现

    前言 经过上一篇的学习,现在已经来到了服务注册发现环节:Consul 的核心功能就是服务注册和发现,Consul 客户端通过将自己注册到 Consul 服务器集群,然后等待调用方去发现服务,实现代理转 ...

  9. 一个故事,一段代码告诉你如何使用不同语言(Golang&C#)提供相同的能力基于Consul做服务注册与发现

    目录 引言 什么是微服务 传统服务 微服务 什么是服务注册与服务发现 为什么要使用不同的语言提供相同的服务能力 服务协调器 服务注册 Golang C#(.NetCore3.1) 服务发现 通过Htt ...

随机推荐

  1. wordpress默认css样式class和id集合

    你是否想过如何设计WordPress主题的不同元素?每个主题都不一样,但是有一些CSS的class和id是由WordPress生成的.我们将逐一介绍一些最重要的默认WordPress样式,方便初学者快 ...

  2. 浏览器性能监控performance使用

    浏览器中有一个performance的性能监控,平时我也没有用到,接手了一个大数据的项目,发现页面打开的比较慢,使用浏览器的performance分析可以看到各个步骤花费的时间. 关于项目的性能分析如 ...

  3. materialize 读取单选按钮

    $('input[name='xxx']:checked')

  4. nginx中的url转发

    公司老项目是python做的,作为一个学java的,现在让我去重构这个项目的一部分页面,所以决定用java来重做,然后通过nginx url转发来实现两个项目的无缝衔接,好了 接下来看如何配置URL转 ...

  5. PHP - register globals

    It seems that the developper often leaves backup files around... 似乎开发人员经常把备份文件放在… 直接下载网站备份: index.ph ...

  6. Arduino OV7670 live image over USB to PC

    https://www.youtube.com/watch?v=L9DTW1ulsT0 https://www.youtube.com/watch?v=Dp3RMb0e1eA

  7. python 判断元素是否在一个列表中

    import random val= data=[,,,,] : find=False val=int(input('请输入查找键值(1-9),输入-1离开:')) for i in data: if ...

  8. [Beta]Scrum Meeting#6

    github 本次会议项目由PM召开,时间为5月11日晚上10点30分 时长15分钟 任务表格 人员 昨日工作 下一步工作 木鬼 撰写博客整理文档 撰写博客整理文档 swoip 改进界面 为适应新功能 ...

  9. Guava集合工具

    JDK提供了一系列集合类,如下所示,极大的方便了开发工作,并针对这些类提供了一个工具类java.util.Collections,Guava在此基础上添加了一些常用工具类方法,相比于java.util ...

  10. Salt-Formulas的使用

    Saltstack自0.17.x版本开始引进Formulas的概念,旨在通过简化State和集成数据来实现State的友好管理.根据SALT FORMULAS的官方文档,在完成手动添加formula目 ...