irpas技术客

8、SpringCloud第八章,升级篇,服务注册与发现Eureka、Zookeeper和Consule_倚书依杖听河流

大大的周 5419

SpringCloud第七章,升级篇,服务注册与发现Eureka、Zookeeper和Consule 一、基础概念 1、服务治理 传统的远程RPC远程调用框架中,管理每个服务与服务之间的依赖关系比较复杂。所以需要使用服务治理,用于管理服务与服 务之间的依赖关系,可以实现服务调用、负载均衡、容错等。实现服务的注册与发现。 Eureka模块就是用来实现服务治理的 2、服务注册与发现 Eureka采用了CS的设计架构, Eureka Server作为服务注册功能的服务器,他是服务注册中心。而系统中的其他服务,使用Eureka的客户端连接到 Eureka Server,并维持心跳链接。这样系统的维护人员就可以通过Eureka Server来监控系统中各个微服务是否正常 运行。 在服务注册与发现中,有一个注册中心。当服务器启动的时候,会把当前自己服务器的信息比如服务通讯地址以别名的方式 注册到注册中心上。另一方,服务消费者以该别名的方式去注册中心上获取实际的服务通讯地址,然后再实现本地RPC调用。 RPC远程调用框架的设计思想在于:注册中心。因为使用注册中心管理每个服务与服务之间的依赖关系。 在任何RPC远程调用框架中,都会有一个注册中心(存放服务地址相关信息)。

二、Eureka

Eureka包含两个组件 Eureka Server和Eureka Client,Eureka Server 提供服务注册服务。Eureka Client通过注册中心进行访问。

<!--Eureka Server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!--Eureka Client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> Eureka Server 提供服务注册服务。 各微服务节点通过配置启动后,会在EurekaServer中进行注册。这样EurekaServer中的服务注册表中就会存储所有 可用服务节点的信息,各服务节点的信息就可以在界面中直观看到。 Eureka Client通过注册中心进行访问。 是一个java客户端,用于简化EurekaServer的交互,客户端同时也具备一个内置的、使用轮询round-robin负载 算法的负载均衡器。在应用启动后,将会向EurekaServer发送心跳(默认周期为30s)如果EurekaServer在多个心跳周期内没有收到某个节点的心跳,EurekaServer将会从服务注册表中将这个服务节点移除(默认90s)。 1、单机Eureka服务构建 1.1、Eureka构建

new maven module

moduleName cloud-Eureka-server-7001 parentProject cloud_2020 groupId com.lee.springcloud artifactId cloud_2020 packaging jar

POM

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://·/apache/zookeeper/zookeeper-3.4.9/zookeeper-3.4.14.tar.gz ##解压 2>、tar -zxvf zookeeper-3.4.9.tar.gz ##重命名 3>、mv zookeeper-3.4.9 zookeeper ##移动 4>、mv zookeeper /opt/ 5>、修改配置文件 cd /opt/zookeeper/conf cp zoo_sample.cfg zoo.cfg vi zoo.cfg # The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/opt/zookeeper/data/data # the port at which the clients will connect clientPort=2181 dataLogDir=/opt/zookeeper/data/log server.1=localhost:2888:3888 6>、启动 ./zkServer.sh start 7、关闭 ./zkServer.sh stop 8、其他 如放开2181阿里ESC的防火墙配置等不再详说 2、创建cloud-provider-payment-8004

new maven module

moduleName cloud-provider-payment-8004 parentProject cloud_2020 groupId com.lee.springcloud artifactId cloud_2020 packaging jar

创建完成后 父工程POM文件会多了个标签

错误的POM演示

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://`/spring-cloud-consul.html

2、安装并运行consul

由于国内主流都使用springcloud alibaba的Nacos作为注册中心,所以这里就简单的拿windows版做下运用。

下载完成后,只有一个consul.exe 执行consul agent -dev,以开发者模式运行。 访问http://localhost:8500

3、创建cloud-provider-payment-8006

new maven module

moduleName cloud-provider-payment-8006 parentProject cloud_2020 groupId com.lee.springcloud artifactId cloud_2020 packaging jar

创建完成后 父工程POM文件会多了个标签

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"> <parent> <artifactId>cloud_2020</artifactId> <groupId>com.lee.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-provider-payment-8006</artifactId> <dependencies> <!--SpringCloud consul-server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>com.lee.springcloud</groupId> <artifactId>cloud-api-common</artifactId> <version>${project.version}</version> </dependency> <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.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>

application.yml

server: port: 8006 spring: application: name: cloud-provider-payment cloud: consul: host: 127.0.0.1 # consul注册中心地址 port: 8500 discovery: hostname: 127.0.0.1 service-name: ${spring.application.name}

主启动类

package com.lee.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /** * 该注解用于向使用consul或者zookeeper作为注册中心时注册服务 * 同时也可以用于对外服务暴露-服务发现 */ @EnableDiscoveryClient @SpringBootApplication public class PaymentConsulMain8006 { public static void main(String[] args) { SpringApplication.run(PaymentConsulMain8006.class,args); } }

Controller

package com.lee.springcloud.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.UUID; /** * 这里不再像cloud-provider-payment-8001和8002一样写service等方法了 * 直接写一个controller方法 */ @RestController public class PaymentController { @Value("${server.port}") private String serverPort; @RequestMapping(value = "payment/consul") public String paymentZk() { return "SpringCloud with consul:" + serverPort + "\t" + UUID.randomUUID().toString(); } }

测试:

启动consul: 访问:http://localhost:8500/ 查看节点情况 4、创建cloud-consumer-order-82

new maven module

moduleName cloud-consumer-order-82 parentProject cloud_2020 groupId com.lee.springcloud artifactId cloud_2020 packaging jar

创建完成后 父工程POM文件会多了个标签

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"> <parent> <artifactId>cloud_2020</artifactId> <groupId>com.lee.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-consumer-order-82</artifactId> <dependencies> <!--SpringCloud consul-server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>com.lee.springcloud</groupId> <artifactId>cloud-api-common</artifactId> <version>${project.version}</version> </dependency> <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.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>

Application.yml

server: port: 82 spring: application: name: cloud-consumer-order cloud: consul: host: 127.0.0.1 # consul注册中心地址 port: 8500 discovery: hostname: 127.0.0.1 service-name: ${spring.application.name}

主启动类

package com.lee.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /** * 该注解用于向使用consul或者zookeeper作为注册中心时注册服务 * 同时也可以用于对外服务暴露-服务发现 */ @EnableDiscoveryClient @SpringBootApplication public class ConsumerConsulMain82 { public static void main(String[] args) { SpringApplication.run(ConsumerConsulMain82.class,args); } }

config

package com.lee.springcloud.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } }

Controller

package com.lee.springcloud.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @Slf4j @RestController public class OrderController { public static final String INVOKE_URL = "http://cloud-provider-payment"; @Resource private RestTemplate restTemplate; @GetMapping("/consumer/payment/consul") public String paymentInfo() { return restTemplate.getForObject(INVOKE_URL + "/payment/consul", String.class); } }

测试:

启动consul、cloud-provider-payment-8006、cloud-consumer-order-82 访问:http://localhost:82/consumer/payment/consul 五、三个注册中心对比 1、先复习下CAP原则

CAP原则又称CAP定理,指的是在一个分布式系统中中, Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性),三者不可得兼。 CAP原则的精髓就是要么AP,要么CP,要么AC,但是不存在CAP。

C Consistency 一致性 A Availability 可用性 P Partition tolerance 分区容错性
2、三者区别 组件名 语言 CAP SpringCloud集成 Eureka Java AP 已集成 Zookeeper Java CP 已集成 Consul Go CP 已集成

AP即是:如果两个服务器没有完成数据同步,仍然能够对外提供服务。

CP是:如果两个服务器没有完成数据同步,则不再进行对外提供服务,知道数据同步完成。

===============================================================

SpringCloud Alibaba? Nacos作为服务注册中心的部分,将在后面的文章中写


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #8SpringCloud第八章 #升级篇