【consul】使用学习
【consul】使用学习
转载:https://www.cnblogs.com/yangchongxing/p/10653791.html
1、下载 consul
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】使用学习的更多相关文章
- consul初步学习
简介 consul是一个服务发现框架 类似的还有zookeeper,eureka,etcd等 作用 服务发现(service discovery) 健康检查(health checking) 配置存储 ...
- Consul 学习笔记-服务注册
Consul简介: Consul是一种服务网格解决方案,提供具有服务发现,配置和分段功能的全功能控制平面.这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建完整的服务网格.Consul需要 ...
- Ocelot + Consul实践
关于Consul(https://www.consul.io)是一个分布式,高可用,支持多数据中心的服务发现和配置共享的服务软件,由 HashiCorp 公司用 Go 语言开发, 基于 Mozilla ...
- java 与大数据学习较好的网站
C# C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿!https://www.cnblogs.com/doforfuture/p/6293926.html ...
- 分布式架构学习-Consul集群配置
简介 之前公司用的是Consul进行服务发现以及服务管理,自己一直以来只是用一下,但是没有具体的深入,觉得学习不可以这样,所以稍微研究了一下. 网上有很多关于Consul的介绍和对比,我这里也不献丑了 ...
- 微服务学习笔记(2)——使用Consul 实现 MagicOnion(GRpc) 服务注册和发现
原文:微服务学习笔记(2)--使用Consul 实现 MagicOnion(GRpc) 服务注册和发现 1.下载打开Consul 笔者是windows下面开发的(也可以使用Docker). 官网下载w ...
- .net core学习笔记,组件篇:服务的注册与发现(Consul)初篇
1.什么是服务注册中心? 在学习服务注册与发现时,我们要先搞明白到底什么是服务注册与发现. 在这里我举一个生活中非常普遍的例子——网购来简单说明,网购在我们日常生活中已经是非常普遍了,其实网购中的(商 ...
- Consul 学习笔记—服务发现
前言: 上一篇文章简单实用Consul试下服务注册,本篇继续学习Consul中的另外特性:服务发现.KV操作 :以及对上篇文章中存在的问题进行解决 问题解决 在上一篇文章中,注册服务提示检查失败. 通 ...
- 学习搭建 Consul 服务发现与服务网格-有丰富的示例和图片
目录 第一部分:Consul 基础 1,Consul 介绍 2,安装 Consul Ubuntu/Debian 系统 Centos/RHEL 系统 检查安装 3,运行 Consul Agent 启动 ...
随机推荐
- VS Code 之 Jupyter NoteBook 初试
一.前言 在今年九月的 PyCon China 大会上,官宣了一项 VS Code Python 的全新功能:Visual Studio Code Python 插件将提供 Jupyter Noteb ...
- 使用Executor框架创建线程池
Executor框架 Executor类:在java.util.concurrent类中,是JDK并发包的核心类. ThreadPoolExecutor: 线程池. Excutors: 线程池工厂,通 ...
- python_day05
今日内容 ''' post请求登录github Request URL: https://github.com/session Request Method: POST #Referer表示上一个请求 ...
- 解决 bash cd too many arguments 报错
解决 bash: cd: too many arguments 本来想着用git bash进入文件夹,但是文件夹名称中带有空格,例如:my blog,导致出错. 在查找资料后,找到一种并不可行的方案, ...
- github上传文件让别人下载--xdd
一.可以下载的条件 仓库要为公开(public) 该文件不可预览或者是图片,如.rar .gif .png .doc .pdf 等格式 二.打开文件的预览界面,如下 三.将最上面的地址复制给别人即可 ...
- 解决maven创建过慢问题和快捷键
archetypeCataloginternal idea常用的快捷键 Alt+回车 导入包,自动修正 Ctrl+N 查找类 Ctrl+Shift+N 查找文件 Ctrl+Alt+L 格式化代码 Ct ...
- 这事没完,继续聊spring cloud stream和kafka的这些小事
上一篇文章讲了如何用spring cloud stream集成kafka,并且跑起来一个demo,如果这一次宣传spring cloud stream的文章,其实到这里就可以啦.但实际上,工程永远不是 ...
- Chapter 05—Advanced data management(Part 1)
一. R的数学函数,统计函数及字符处理函数 例01:一道实际应用题 一组学生其数学,科学和英语的成绩如下表: 任务:根据成绩,决定对每个学生的单独指导: 前20%的学生的成绩为A,次之为B,以此类推: ...
- Chapter 04—Basic Data Management
1. 创建新的变量 variable<-expression expression:包含一组大量的操作符和函数.常用的算术操作符如下表: 例1:根据已知变量,创建新变量的三种途径 > my ...
- webpack 4.x 从零开始初始化一个vue项目
创建目录 项目名称: vue-init app css reset.sass js home index.vue router index.js main.js App.vue views index ...