【consul】使用学习

转载:https://www.cnblogs.com/yangchongxing/p/10653791.html

1、下载 consul

https://www.consul.io

2、Window系统使用

开发模式启动,启动后以前配置会丢失

consul agent -dev

导出配置

consul kv export > kv.json

导入配置

consul kv import @kv.json

 3、将 Spring Boot 为服务注册到服务代理

bootstrap.yml 文件

server:
port: 0
spring:
application:
name: consul-client
cloud:
consul:
host: dev.consul.ycx
port: 8500
config:
format: yaml
discovery:
prefer-ip-address: true
health-check-interval: 3s
health-check-path: /actuator/health
instance-id: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}

consul Key / Value < config < application

如果在 config 目录下有应用名目录 consul-client 也就是 spring.application.name=consul-client 指定的目录下data中有相同的配置,就会覆盖 application 下的配置

pom 依赖

 
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>consulclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consulclient</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

@EnableDiscoveryClient 在Edgware版本是可选的。

package com.example.consulclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
@RestController
// @EnableDiscoveryClient 在Edgware版本是可选的
public class ConsulclientApplication { public static void main(String[] args) {
SpringApplication.run(ConsulclientApplication.class, args);
} @Value("${ycx.info}")
private String info; @RequestMapping("/consul")
public Object index() {
return info;
}
}

bootstrap.yml  参考配置

spring:
application:
name: ${project.name}
profiles:
active: dev
cloud:
consul:
host: dev.consul.ycx
port:
config:
# enabled为true表示启用配置管理功能
enabled: true
# watch选项为配置监视功能,主要监视配置的改变
watch:
enabled: true
delay:
wait-time:
# 表示如果没有发现配置,是否抛出异常,true为是,false为否,当为false时,consul会打印warn级别的日志信息
fail-fast: false
# 表示使用的配置格式
format: yaml
# 服务发现配置
discovery:
# 启用服务发现
enabled: true
# 启用服务注册
register: true
# 服务停止时取消注册
deregister: true
# 表示注册时使用IP而不是hostname
prefer-ip-address: true
# 执行监控检查的频率
health-check-interval: 15s
# 设置健康检查失败多长时间后,取消注册
health-check-critical-timeout: 15s
# 健康检查的路径
health-check-path: /actuator/health
# 服务注册标识,格式为:应用名称+服务器IP+端口
instance-id: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}

Key / Values < config < application 参考配置

server:
port:
logging:
level:
root: INFO
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+
default-property-inclusion: non_null
mybatis-plus:
mapper-locations: classpath*:mapper/*.xml
configuration:
auto-mapping-behavior: partial
auto-mapping-unknown-column-behavior: warning
global-config:
db-config:
logic-delete-value: -1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 1 # 逻辑未删除值(默认为 0)
feign:
client:
config:
default:
connect-timeout: 20000
read-timeout: 20000

【consul】使用学习的更多相关文章

  1. consul初步学习

    简介 consul是一个服务发现框架 类似的还有zookeeper,eureka,etcd等 作用 服务发现(service discovery) 健康检查(health checking) 配置存储 ...

  2. Consul 学习笔记-服务注册

    Consul简介: Consul是一种服务网格解决方案,提供具有服务发现,配置和分段功能的全功能控制平面.这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建完整的服务网格.Consul需要 ...

  3. Ocelot + Consul实践

    关于Consul(https://www.consul.io)是一个分布式,高可用,支持多数据中心的服务发现和配置共享的服务软件,由 HashiCorp 公司用 Go 语言开发, 基于 Mozilla ...

  4. java 与大数据学习较好的网站

    C# C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿!https://www.cnblogs.com/doforfuture/p/6293926.html ...

  5. 分布式架构学习-Consul集群配置

    简介 之前公司用的是Consul进行服务发现以及服务管理,自己一直以来只是用一下,但是没有具体的深入,觉得学习不可以这样,所以稍微研究了一下. 网上有很多关于Consul的介绍和对比,我这里也不献丑了 ...

  6. 微服务学习笔记(2)——使用Consul 实现 MagicOnion(GRpc) 服务注册和发现

    原文:微服务学习笔记(2)--使用Consul 实现 MagicOnion(GRpc) 服务注册和发现 1.下载打开Consul 笔者是windows下面开发的(也可以使用Docker). 官网下载w ...

  7. .net core学习笔记,组件篇:服务的注册与发现(Consul)初篇

    1.什么是服务注册中心? 在学习服务注册与发现时,我们要先搞明白到底什么是服务注册与发现. 在这里我举一个生活中非常普遍的例子——网购来简单说明,网购在我们日常生活中已经是非常普遍了,其实网购中的(商 ...

  8. Consul 学习笔记—服务发现

    前言: 上一篇文章简单实用Consul试下服务注册,本篇继续学习Consul中的另外特性:服务发现.KV操作 :以及对上篇文章中存在的问题进行解决 问题解决 在上一篇文章中,注册服务提示检查失败. 通 ...

  9. 学习搭建 Consul 服务发现与服务网格-有丰富的示例和图片

    目录 第一部分:Consul 基础 1,Consul 介绍 2,安装 Consul Ubuntu/Debian 系统 Centos/RHEL 系统 检查安装 3,运行 Consul Agent 启动 ...

随机推荐

  1. Python input函数使用

    本文链接:https://www.cnblogs.com/zyuanlbj/p/11905475.html 函数定义 def input(*args, **kwargs): # real signat ...

  2. Robot Framework自动化测试环境搭建

    robotFramework是一个通用的自动化测试框架来进行验收测试和验收测试驱动开发模式,它具有易于使用的表格的测试数据和关键字测试驱动方法,其测试功能可通过实现与python或java的测试库进行 ...

  3. JavaWeb02-Servlet

    Servlet概述 生命周期方法: l  void init(ServletConfig):出生之后(1次): l  void service(ServletRequest request, Serv ...

  4. go中的关键字-go(上)

    1. goroutine的使用 在Go语言中,表达式go f(x, y, z)会启动一个新的goroutine运行函数f(x, y, z),创建一个并发任务单元.即go关键字可以用来开启一个gorou ...

  5. 🙀Java 又双叒叕发布新版本,这么多版本如何灵活管理?

    文章来源:http://1t.click/bjAG 前言 不知不觉 JDK13 发布已有两个月,不知道各位有没有下载学习体验一番?每次下载安装之后,需要重新配置一下 Java 环境变量.等到运行平时的 ...

  6. tensorflow:模型的保存和训练过程可视化

    在使用tf来训练模型的时候,难免会出现中断的情况.这时候自然就希望能够将辛辛苦苦得到的中间参数保留下来,不然下次又要重新开始. 保存模型的方法: #之前是各种构建模型graph的操作(矩阵相乘,sig ...

  7. ctf线下赛中检测外来IP的shell脚本

    该脚本可用于ctf线下赛中,用来检测攻击IP的接入,及时做出响应. #!/bin/bash #写自己队的ip ipA="172.22.60.230" ipB="172.2 ...

  8. day20191010ClassNotes

    笔记: 1.DAO模式组成部分: 程序 ----> 数据库 实体类 数据库中的表 工具类:公共的数据库连接.关闭.公共的增删改.查询 接口 : 程序提倡的是面向接口编程,从而降低程序的耦合性 实 ...

  9. 【Android - 组件】之Activity的启动模式

    Activity的启动模式目前有四种:standard.singleTop.singleTask 和 singleInstance. 1.standard standard 是标准模式,也是系统的默认 ...

  10. 【Android - 控件】之V - Toolbar的使用

    Toolbar是Android V7包中的一个控件,用来代替Action Bar作为界面的头部标题栏布局.Toolbar相对于Action Bar的特点是更加灵活,可以显示在任何位置. 首先先来看To ...