spring cloud:通过client访问consul集群(spring cloud hoxton sr8 / spring boot 2.3.4)
一,为什么要搭建consul的client?
1,网上的很多资料,访问consul时用的单机模式,这样是不可以直接在生产环境中使用的
还有一些资料,搭建了consul的集群后,直接访问集群中的某一个ip,
这样不能达到高可用的目的,因为如果当前访问的ip宕机,则到整个consul集群的访问会失效.
2,如何访问consul集群?
第一个方法:一个是集成java代码,直接在配置文件中的host写上集群的多个ip,
当访问的地址有异常则访问其他的ip,
大家可以参考这个项目:
https://segmentfault.com/a/1190000020155983
第二个方法:较常用的方法:
在项目所在机器上以client模式安装运行一个consul的agent,
java代码通过client访问consul的server集群
因为服务提供者会多个实例,所以如果某个实例上的consul client发生故障,
只会有一个服务提供者失败,而不会影响到其他的实例继续提供服务
3,安装consul集群:参见:
https://www.cnblogs.com/architectforest/p/13735545.html
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,在linux centos8上搭建consul的client
[root@localhost consul]# mkdir /usr/local/soft/consul
[root@localhost consul]# mv ./consul /usr/local/soft/consul/
[root@localhost consul]# nohup /usr/local/soft/consul/consul agent -data-dir=/data/consul/data -node=client-1 -bind=192.168.1.7 -datacenter=dc1 -ui -client=0.0.0.0 -join=172.17.0.2 > /dev/null 2>&1 &
[root@localhost consul]# /usr/local/soft/consul/consul members
Node Address Status Type Build Protocol DC Segment
server-2 172.17.0.2:8301 alive server 1.8.4 2 dc1 <all>
server-3 172.17.0.3:8301 alive server 1.8.4 2 dc1 <all>
server-4 172.17.0.4:8301 alive server 1.8.4 2 dc1 <all>
client-1 192.168.1.7:8301 alive client 1.8.4 2 dc1 <default>
可以看到client-1是以client类型加入到集群中的
三,演示项目的相关信息
1,项目所在地址
https://github.com/liuhongdi/cloudconsul
2,功能说明:
演示了获取consul集群中的service和实例信息
3,项目结构:如图:

四,配置文件说明
1,pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
2,application.properties
spring.application.name=provider1
server.port=8080
#consul
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
#service name
spring.cloud.consul.discovery.service-name=lhdprovider
provider.name = p1
五, java代码说明
1,DemoApplication.java
@EnableDiscoveryClient
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2,HomeController.java
@RestController
@RequestMapping("/home")
public class HomeController { @Resource
private DiscoveryClient discoveryClient; @Value("${provider.name}")
private String name; @Value("${server.port}")
private String port; // list services
@GetMapping("/serviceslist")
public Object serviceslist() {
return discoveryClient.getServices();
} // list instances in a service id
@GetMapping("/instanceslist")
public Object instanceslist() {
return discoveryClient.getInstances("consul");
} //test api
@GetMapping("/hello")
public String hello() {
//return discoveryClient.getInstances("consul");
String res = "name:"+name+";port:"+port;
return res;
}
}
六,测试效果
1,得到服务列表:访问:
http://127.0.0.1:8080/home/serviceslist
返回

2,得到consul server集群的实例列表
http://127.0.0.1:8080/home/instanceslist
返回:

3,从web ui查看服务列表

查看lhdprovider中的实例:

七,查看spring boot的版本
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.4.RELEASE)
spring cloud:通过client访问consul集群(spring cloud hoxton sr8 / spring boot 2.3.4)的更多相关文章
- kubernetes实战之部署一个接近生产环境的consul集群
系列目录 前面我们介绍了如何在windows单机以及如何基于docker部署consul集群,看起来也不是很复杂,然而如果想要把consul部署到kubernetes集群中并充分利用kubernete ...
- 实战中的asp.net core结合Consul集群&Docker实现服务治理
0.目录 整体架构目录:ASP.NET Core分布式项目实战-目录 一.前言 在写这篇文章之前,我看了很多关于consul的服务治理,但发现基本上都是直接在powershell或者以命令工具的方式在 ...
- .net core结合Consul集群&Docker实现服务治理
实战中的asp.net core结合Consul集群&Docker实现服务治理 https://www.cnblogs.com/guolianyu/p/9614050.html 0.目录 整体 ...
- spring cloud:搭建基于consul的服务提供者集群(spring cloud hoxton sr8 / spring boot 2.3.4)
一,搭建基于consul的服务提供者集群 1,consul集群,共3个实例: 2, 服务提供者集群:共2个实例: 3,服务消费者:一个实例即可 4,consul集群的搭建,请参考: https://w ...
- Consul集群Server+Client模式
Consul集群Server+Client模式 架构示意图 只使用Consul的Server模式有以下2个问题: 因为Consul Server数量受到控制所以压力承载(扩展性)是个问题. Serve ...
- spring Cloud服务注册中心Eureka集群
spring Cloud服务注册中心Eureka集群配置: 在application.yml文件加以下配置: server: port: 8761 tomcat: uri-encoding: UTF- ...
- Consul集群Server模式
Consul集群Server模式 架构示意图 Consul在生产环境下运行模式分为两种:Server模式和Client模式(dev模式属于开发模式不在这里讨论),我们先用Server模式搭建一个Con ...
- 分布式架构学习-Consul集群配置
简介 之前公司用的是Consul进行服务发现以及服务管理,自己一直以来只是用一下,但是没有具体的深入,觉得学习不可以这样,所以稍微研究了一下. 网上有很多关于Consul的介绍和对比,我这里也不献丑了 ...
- Consul集群版容器化部署与应用集成
背景 由于公司目前的主要产品使用的注册中心是consul,consul需要用集群来保证高可用,传统的方式(Nginx/HAProxy)会有单点故障问题,为了解决该问题,我开始研究如何只依赖consul ...
随机推荐
- JAVA JDK 环境变量配置 入门详解 - 精简归纳
JAVA JDK 环境变量配置 入门详解 - 精简归纳 JERRY_Z. ~ 2020 / 9 / 13 转载请注明出处!️ 目录 JAVA JDK 环境变量配置 入门详解 - 精简归纳 一.为什么j ...
- synchronized底层是怎么实现的?
前言 面试的时候有被问到,synchronized底层是怎么实现的,回答的比较浅,面试官也不是太满意,所以觉得要好好总结一下,啃啃这个硬骨头. synchronized使用场景 我们在使用synchr ...
- oracle之SQL的数据类型
SQL的数据类型 3.1 四种基本的常用数据类型(表的字段类型) 1.字符型, 2.数值型,3.日期型,4.大对象型 3.1.1 字符型: char 固定字符,最长2000个 ...
- Win10安装Tensorflow-gpu遇到Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问
最近因为上课需要安装Anaconda和Tensorflow,安装Anaconda后再使用 Tensorflow官网提供的pip安装Tensorflow-GPU方法会出现如下错误: 解决方法:在安装命令 ...
- hystrix讲解:熔断降级隔离以及合并请求
对springcloud只是学习了基本的框架搭建,基本上看到的例子都是只使用了fallback 但是hystrix还有线程隔离和请求合并的能力 顺便吐槽 大部分人的博客例子估计都是听课的 应用 ...
- 工具请求接口参数为string类型的JSON字符串时需要加转义字符模拟测试
例如postMan传String类型的json字符串请后台接口时,需要\转义
- 在移动硬盘上安装Linux Mint19记录
前要: 有一12年买的手提电脑,打算在其上直接装linux部署分布式爬虫顺便学linux 唔,开机吧--然开机动画没有,只有间断有序的悲鸣,一查,主板逝世 卖给收买旧电脑估计不到20-不能忍,想了想不 ...
- IntelliJ IDEA 2020.2 x64 最新破解教程有效期到2089年 完全免费分享
作者:极客小俊 一个专注于web技术的80后 我不用拼过聪明人,我只需要拼过那些懒人 我就一定会超越大部分人! CSDN@极客小俊,原创文章, B站技术分享 B站视频 : Bilibili.com 个 ...
- spring:spring再总结(ioc、aop、DI等)
IOC(Inversion of Control),即"控制反转",不是一种技术而是一种思想 1.IOC的理解 Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部 ...
- Harbor介绍与企业级私有Docker镜像仓库搭建
Harbor介绍与安装部署,并实现通过http和https协议[自签发SSL证书]访问,客户端如何通过Harbor镜像仓库实现镜像的上传[推送]与下载[拉取]. Harbor介绍 Harbor,是一个 ...