【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. UNIX env查找技巧

    在一些UNIX系统上,也许可以避免硬编码Python解释器的路径,而可以在文件特定的第一行注释中这样写: #!/usr/bin/env python ... script goes here ... ...

  2. LeetCode Bash练习

    195. Tenth Line #!/bin/bash i= cat file.txt | while read line do #echo $line ] then echo $line fi le ...

  3. 程序员用于机器学习数据科学的3个顶级 Python 库

    NumPy NumPy(数值 Python 的简称)是其中一个顶级数据科学库,它拥有许多有用的资源,从而帮助数据科学家把 Python 变成一个强大的科学分析和建模工具.NumPy 是在 BSD 许可 ...

  4. LeetCode 5112. 十六进制魔术数字 Hexspeak

    地址 https://leetcode-cn.com/problems/hexspeak/ 题目描述字母大写的十六进制字符串,然后将所有的数字 0 变成字母 O ,将数字 1  变成字母 I . 如果 ...

  5. 自定制页面跳转时携带原搜索参数的URL

    介绍 django自带反向解析生成URL的功能,目的是避免硬编码,较少代码维护的代价. 前端页面使用模板语法,如:{% url "rbac: request menu_list" ...

  6. jsp实现增加数据功能

    1. 环境的搭建 软件 数据库  sql myeclipse 8.0  tomcat 6.0 2. 安装完 myeclipse 配置下  部署tomcat 6.0 =1=> =2=>  新 ...

  7. 一个null,差点把系统给弄崩溃了

    今天生产上面发现了一个奇异的bug,URL上面会带上一个ID,这个ID是关联别的系统的,类似这种格式 xxx.xxx.xxx.xxx  ,是别的系统自己填写的,我们的URL会带上id=xxx.xxx. ...

  8. ASP.NET Core3.X 终端中间件转换为端点路由运行

    引言 前几天.NET Core3.1发布,于是我把公司一个基础通用系统升级了,同时删除了几个基础模块当然这几个基础模块与.NET Core3.1无关,其中包括了支付模块,升级完后静文(同事)问我你把支 ...

  9. Forrester:华为云容器是容器混合云最佳选择

    近日,国际权威咨询机构Forrester发布<The Forrester New WaveTM: Public Cloud Enterprise Container Platforms, Q3 ...

  10. 华为云垃圾分类大赛,让AI 帮你“见圾行事”

    [摘要] "你是什么垃圾"已经out了,我们来看0看谁是垃圾之王?! 当各位听说深圳实行垃圾分类政策时,是不是虎躯一震,每天焦虑得想搬家? -稳住,别慌! 救兵来啦 华为云人工智能 ...