SpringCloud 教程 | 第十四篇: 服务注册(consul)
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者、原文超链接 ,博主地址:http://blog.csdn.net/forezp。 http://blog.csdn.net/forezp/article/details/70245644
转载请标明出处:
http://blog.csdn.net/forezp/article/details/70245644
本文出自方志朋的博客
这篇文章主要介绍 spring cloud consul 组件,它是一个提供服务发现和配置的工具。consul具有分布式、高可用、高扩展性。
一、consul 简介
consul 具有以下性质:
- 服务发现:consul通过http 方式注册服务,并且服务与服务之间相互感应。
- 服务健康监测
- key/value 存储
- 多数据中心
consul可运行在mac windows linux 等机器上。
二、consul安装
linux
$ mkdir -p $GOPATH/src/github.com/hashicorp && cd $!
$ git clone https://github.com/hashicorp/consul.git
$ cd consul
$ make bootstrap
$ make bootstrap
- 1
- 2
- 3
- 4
- 5
- 6
windows下安装:
见consul怎么在windows下安装
三、构建工程
构建一个consul-miya的springboot工程,导入依赖pring-cloud-starter-consul-discovery,其依赖文件:
<?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>
<groupId>com.forezp</groupId>
<artifactId>consul-miya</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>consul-miya</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>Dalston.RELEASE</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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
在其入口文件ConsulMiyaApplication加入注解@EnableDiscoveryClient,开启服务发现:
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConsulMiyaApplication {
@RequestMapping("/hi")
public String home() {
return "hi ,i'm miya";
}
public static void main(String[] args) {
new SpringApplicationBuilder(ConsulMiyaApplication.class).web(true).run(args);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
在其配置文件application.yml指定consul服务的端口为8500:
spring:
cloud:
consul:
host: localhost
port: 8500
discovery:
healthCheckPath: ${management.contextPath}/health
healthCheckInterval: 15s
instance-id: consul-miya
application:
name: consul-miya
server:
port: 8502
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
启动工程,访问localhost:8500,可以发现consul-miya被注册了。
SpringCloud 教程 | 第十四篇: 服务注册(consul)的更多相关文章
- 史上最简单的 SpringCloud 教程 | 第十四篇: 服务注册(consul)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2017/07/12/sc14-consul/ 本文出自方志朋的博客 这篇文章主要介绍 s ...
- 史上最简单的SpringCloud教程 | 第十二篇: 断路器监控(Hystrix Dashboard)(Finchley版本)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f12-dash/ 本文出自方志朋的博客 在我的第四篇文章断路 ...
- 史上最简单的SpringCloud教程 | 第十二篇: 断路器监控(Hystrix Dashboard)
转载请标明出处: 首发于:https://www.fangzhipeng.com/springcloud/2017/07/12/sc12-hystix-dashbd/ 本文出自方志朋的博客 最新Fin ...
- (转) SpringBoot非官方教程 | 第二十四篇: springboot整合docker
这篇文篇介绍,怎么为 springboot程序构建一个Docker镜像.docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源.Docker 可以让开发者打包他们的 ...
- SpringCloud教程 | 第十二篇: 断路器监控(Hystrix Dashboard)
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/art ...
- SpringBoot非官方教程 | 第二十四篇: springboot整合docker
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot24-docker/ 本文出自方志朋的博客 这篇文 ...
- SpringBoot非官方教程 | 第十四篇:在springboot中用redis实现消息队列
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot14-redis-mq/ 本文出自方志朋的博客 这 ...
- 跟我学SpringCloud | 第十四篇:Spring Cloud Gateway高级应用
SpringCloud系列教程 | 第十四篇:Spring Cloud Gateway高级应用 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 ...
- Spring Cloud第十四篇 | Api网关Zuul
本文是Spring Cloud专栏的第十四篇文章,了解前十三篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring C ...
随机推荐
- Angular4中常用管道(转载)
Angular4中常用管道 通常我们需要使用管道实现对数据的格式化,Angular4中的管道和之前有了一些变化,下面说一些常用的管道. 一.大小写转换管道 uppercase将字符串转换为大写 low ...
- CAS单点登录实践(spring cas client配置)
前言: 最近的项目需要将多个站点统一登录,查阅了资料Jasig cas(Central Authentication Service)(官方站点:http://www.jasig.org/cas)使用 ...
- 吴超老师课程--HBASE的集群安装
1.hbase的机群搭建过程(在原来的hadoop上的hbase伪分布基础上进行搭建)1.1 集群结构,主节点(hmaster)是hadoop,从节点(region server)是hadoop1和h ...
- nginx缓存原理
一.HTTP字段理解 1.Expires: 该字段的http1.0时的规范,值为一个绝对时间的GMT格式的时间字符串,代表缓存资源的过期时间,在这个时点之前即命中缓存. 缺点:服务器返回的时间,可能与 ...
- 解释一下python中的//,%和**运算符
//运算符执行地板除法(向下取整除),它会返回整除结果的整数部分 print(7//2) 这里整除后会返回3.5 同样的,执行取幂运算,ab会返回a的b次方 print(2**10) 最后,%执行取模 ...
- 11 Spring框架 SpringDAO的JdbcTemplate
上几个章节我们探讨了Spring的IoC和AOP,这是Spring的重点,但是Spring对jdbc的支持同样我们也不能忘记,毕竟我们还要通过Spring来管理DAO框架(例如Hibernate或者M ...
- Spring 之定义切面尝试(基于注解)
[Spring 之定义切面尝试] 1.标记为深红色的依赖包是必须的 <dependency> <groupId>org.springframework</groupId& ...
- ping主机脚本
#!/bin/bash #ping net='172.16.1' uphosts=0 downhosts=0 for i in {1..254};do ping -c 1 -w 1 ${net}.${ ...
- Wyx20162314 2016-2017-2 《程序设计与数据结构》课程总结
20162314 2016-2017-2 <程序设计与数据结构>课程总结 一.每周作业.结对编程博客的链接汇总 预备作业一01 20162314:专业的期许.浅谈师生关系.对未来学习任务的 ...
- mysql官网下载链接——绿色版&安装版
windows64位5.5.60安装版 https://downloads.mysql.com/archives/get/file/mysql-5.5.60-winx64.msi windows64位 ...