Spring Cloud
  • Introduction
  • 网站架构演变过程
    • 微服务架构
  • Spring Cloud 简介
  • 服务的注册与发现
    • 其他知识
      • Zookeeper与Eureka区别
      • DiscoveryClient 与@EnableEurekaServer
    • Zookeeper
    • Consul
    • Eureka
      • 搭建注册中心
      • 高可用注册中心
      • Eureka详解
  • 服务消费者
    • RestTemplate
    • 服务消费者(rest+ribbon)
      • 此时架构
    • 服务消费者(Feign)
  • 断路器(Hystrix)
    • 基础入门
  • API Gateway(Zuul)
    • 基础入门
  • 分布式配置中心(Config)
    • 基础入门
  • SpringCloud Bus
  • 链路跟踪(Sleuth)
  • 数据流(Stream)
  • Dubbo
    • Untitled
Powered by GitBook
On this page
  • Consul简介
  • Consul环境搭建
  • Consul客户端
  • Maven依赖信息
  • 客户端配置文件
  • 资料

Was this helpful?

  1. 服务的注册与发现

Consul

PreviousZookeeperNextEureka

Last updated 4 years ago

Was this helpful?

Consul简介

Consul 是一套开源的分布式服务发现和配置管理系统,由 HashiCorp 公司用 Go 语言开发。 它具有很多优点。包括: 基于 raft 协议,比较简洁; 支持健康检查, 同时支持 HTTP 和 DNS 协议 支持跨数据中心的 WAN 集群 提供图形界面 跨平台,支持 Linux、Mac、Windows Consul 下载地址

Consul环境搭建

官方下载地址下载window版,解压得到一个可执行文件。 设置环境变量,让我们直接在cmd里可直接使用consul使命。在path后面添加consul所在目录例如D:\soft\consul_1.1.0_windows_amd64

启动consul命令 consul agent -dev -ui -node=cy -dev开发服务器模式启动,-node结点名为cy,-ui可以用界面访问,默认能访问。

测试访问地址:

docker run -d -p 8500:8500 -h node1 --name consul consul agent -dev -bootstrap-expect=1 -node=node1 -client 0.0.0.0 -ui

Consul客户端

Maven依赖信息

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
	</parent>
	<!-- 管理依赖 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.M7</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<!-- SpringBoot整合Web组件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--SpringCloud consul-server -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-consul-discovery</artifactId>
		</dependency>
	</dependencies>
	<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/libs-milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

客户端配置文件

生产者

###eureka 服务端口号
server:
  port: 8502
spring:
  application:
    name: consul-member
####consul注册中心地址
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        hostname: 192.168.18.220

消费者

###eureka 服务端口号
server:
  port: 8503
spring:
  application:
    name: consul-order
####consul注册中心地址
  cloud:
    consul:
    #consul地址
      host: localhost
    #consul端口号
      port: 8500
      discovery:
        #注册到consul 上的ip地址,默认是生成一个随机的标识符
        hostname: 192.168.18.220

资料

https://springcloud.cc/spring-cloud-consul.html
http://localhost:8500
基于Docker的Consul服务发现集群搭建
Docker部署Consul集群
SpringCloud笔记(四)使用Consul代替Eureka作为注册中心