【Spring Cloud实战】Consul服务注册与发现
创始人
2024-01-21 15:10:27
0

个人博客上有很多干货,欢迎访问:https://javaxiaobear.gitee.io/

1、简介

https://www.consul.io/docs/intro

Consul is a service mesh solution providing a full featured control plane with service discovery, configuration, and segmentation functionality. Each of these features can be used individually as needed, or they can be used together to build a full service mesh. Consul requires a data plane and supports both a proxy and native integration model. Consul ships with a simple built-in proxy so that everything works out of the box, but also supports 3rd party proxy integrations such as Envoy.

Consul 是一套开源的分布式服务发现和配置管理系统,由 HashiCorp 公司用 Go 语言开发。

提供了微服务系统中的服务治理、配置中心、控制总线等功能。这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网格,总之Consul提供了一种完整的服务网格解决方案。它具有很多优点。包括: 基于 raft 协议,比较简洁; 支持健康检查, 同时支持 HTTP 和 DNS 协议 支持跨数据中心的 WAN 集群 提供图形界面 跨平台,支持 Linux、Mac、Windows

特性

下载

https://www.consul.io/downloads

2、安装并运行

https://learn.hashicorp.com/tutorials/consul/get-started-install?in=consul/getting-started

在这里插入图片描述

解压之后就是一个exe文件,双击运行即可

查看版本信息

在这里插入图片描述

consul --version

在这里插入图片描述

使用开发者模式启动

consul agent -dev

在这里插入图片描述

通过地址即可访问到consul的首页:http://localhost:8500/

在这里插入图片描述

3、服务提供者

1、新建module

xiaobear-provider-consul-payment8006-7

2、改pom

org.springframework.cloudspring-cloud-starter-consul-discoveryorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-actuatororg.springframework.bootspring-boot-devtoolsruntimetrueorg.projectlomboklomboktrueorg.springframework.bootspring-boot-starter-testtest

3、写yml

server:port: 8006spring:application:name: consul-provider-payment#consul服务注册地址cloud:consul:host: localhostport: 8500discovery:service-name: ${spring.application.name}

4、主启动类

@SpringBootApplication
public class ConsulProviderPayment {public static void main(String[] args) {SpringApplication.run(ConsulProviderPayment.class,args);}
}

5、业务类

controller

@RestController
public class PaymentController {@Value("${server.port}")private String serverPort;@GetMapping("/consul/payment")public String paymentInfo(){return "hello consul,"+serverPort;}
}

6、测试

http://localhost:8006/consul/payment
在这里插入图片描述

4、服务消费者

1、新建module

xiaobear-consumer-consul-order82

2、改pom

    org.springframework.cloudspring-cloud-starter-consul-discoveryorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-actuatororg.springframework.bootspring-boot-devtoolsruntimetrueorg.projectlomboklomboktrueorg.springframework.bootspring-boot-starter-testtest

3、写yml

###consul服务端口号
server:port: 80spring:application:name: cloud-consumer-order####consul注册中心地址cloud:consul:host: localhostport: 8500discovery:#hostname: 127.0.0.1service-name: ${spring.application.name}

4、主启动类

@SpringBootApplication
public class ConsulOrderMain {public static void main(String[] args) {SpringApplication.run(ConsulOrderMain.class,args);}
}

5、业务类

1、配置bean

@Configuration
public class ApplicationContextBean {@Bean@LoadBalancedpublic RestTemplate getRestTemplate(){return new RestTemplate();}
}

2、controller

@RestController
public class OrderConsulController {public static final String INVOKE_URL = "http://consul-provider-payment"; //consul-provider-payment@Resourceprivate RestTemplate restTemplate;@GetMapping(value = "/consumer/payment/consul")public String paymentInfo(){String result = restTemplate.getForObject(INVOKE_URL+"/consul/payment", String.class);System.out.println("消费者调用支付服务(consule)--->result:" + result);return result;}
}

6、测试

在这里插入图片描述
http://localhost/consumer/payment/consul

在这里插入图片描述

5、三个注册中心的异同点

在这里插入图片描述

CAP

  • C:consistency(强一致性)
  • A:Availability(可用性)
  • P:Partition tolerance(分区容错性)

CAP理论关注粒度是数据,而不是整体设计的策略

最多只能同时较好的满足两个。

CAP理论的核心是:一个分布式系统不可能同时很好的满足一致性,可用性和分区容错性这三个需求,

因此,根据 CAP 原理将 NoSQL 数据库分成了满足 CA 原则、满足 CP 原则和满足 AP 原则三 大类:

  • CA - 单点集群,满足一致性,可用性的系统,通常在可扩展性上不太强大。

  • CP - 满足一致性,分区容忍必的系统,通常性能不是特别高。

  • AP - 满足可用性,分区容忍性的系统,通常可能对一致性要求低一些。

在这里插入图片描述

Eureka(AP)

AP架构

当网络分区出现后,为了保证可用性,系统B可以返回旧值,保证系统的可用性。

结论:违背了一致性C的要求,只满足可用性和分区容错,即AP

在这里插入图片描述

Zookeeper/Consul(CP)

CP架构

当网络分区出现后,为了保证一致性,就必须拒接请求,否则无法保证一致性

结论:违背了可用性A的要求,只满足一致性和分区容错,即CP

在这里插入图片描述
Spring Cloud实战合集:

  • 一篇彻底带你理解微服务
  • Spring Cloud入门以及版本管理
  • 【Spring Cloud实战】消费者直接调用提供者(案例)
  • 【Spring Cloud实战】Eurake服务注册与发现

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...