1、spring cloud----注册中心(eureka,zookeeper,consul)服务注册与发现

一、介绍
SpringCloud=分布式微服务架构的一站式解决方案,是多种微服务架构落地技术的集合体,俗称微服务全家桶。

二、搭建springboot工程

  1. 搭建父工程(maven-site简单骨架)

  2. 字符编码
    在这里插入图片描述

  3. 注解生效激活在这里插入图片描述

  4. java编译版本选8
    在这里插入图片描述

  5. File Type过滤不用看的文件
    在这里插入图片描述

  6. 父工程的pom.xml

<packaging>pom</packaging>

在这里插入图片描述

<properties></properties>统一管理jar包版本

在这里插入图片描述

<!--  子模块继承之后。作用:锁定版本+子modlue不用写version-->
<!--  dependencyManagement只有父工程有-->
<dependencyManagement></dependencyManagement>//只是声明依赖,并不实现引入。因此子模块需要声明引入的依赖。

在这里插入图片描述

使用lombok,idea需要安装lombok插件

  1. 创建微服务模块(子模块)
    步骤: 1. 建module。2.改pom,3.写yml,4.启动类。5.业务类

开启热部署:

  • 子工程的pom.xml
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
  • 父工程的pom.xml
<build>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <configuration>
            <fork>true</fork>
            <addResources>true</addResources>
          </configuration>
        </plugin>
      </plugins>
  </build>
  • setting在这里插入图片描述
  • ctrl + alt + shift + / —> Register
    在这里插入图片描述
    在这里插入图片描述
  • 重启idea

开启Run Dashboard

  • 通过修改idea的workspace.xml的方式来快速打开Run Dashboard窗口
    在这里插入图片描述
    在这里插入图片描述

三、1、Eureka注册中心

1.两部分:eureka server和eureka client

- 依赖和配置(单机版):

1.eureka server

- pom.xml
   <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>

- application.yml
	server:
	  port: 7001
	eureka:
	  instance:
	    hostname: localhost #eureka服务器端的实例名称
	  client:
	    register-with-eureka: false #false表示不向注册中心注册自己
	    fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
	    service-url:
	      #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
	      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
- 启动类中添加@EnableEurekaServer

2.eureka client

- pom.xml
	  <dependency>
	       <groupId>org.springframework.cloud</groupId>
	       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
	   </dependency>

- application.yml
	eureka:
	  client:
	    register-with-eureka: true #注册进eureka server中
	    fetch-registry: true #是否从eureka中抓取已有的注册信息。默认为true.单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
	    service-url:
	      defaultZone: http://localhost:7001/eureka
	      
- 启动类中添加@EnableEurekaClient

问:微服务RPC远程服务调用最核心的是什么?
答:高可用
如果只有一个注册中心,出故障的话,整个服务不可用。解决办法:搭建eureka注册中心集群,实现负载均衡+故障容错

- eureka集群版

多个eureka,多个子模块
1.首先在电脑上配置:C:\Windows\System32\drivers\etc\hosts
在这里插入图片描述
2.eureka server

  • eureka7001
- pom.xml不变
- application.yml(7001的defaultZone填写7002的访问地址)
	server:
  	  port: 7001
	eureka:
	  instance:
	    hostname: eureka7001.com #eureka服务器端的实例名称(集群版)
	  client:
	    register-with-eureka: false #false表示不向注册中心注册自己
	    fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
	    service-url:
	      #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
	      defaultZone: http://eureka7002.com:7002/eureka/
- 启动类中添加@EnableEurekaServer
  • eureka7002
- pom.xml不变
- application.yml(7002的defaultZone填写7001的访问地址)
	server:
  	  port: 7002
	eureka:
	  instance:
	    hostname: eureka7002.com
	  client:
	    register-with-eureka: false
	    fetch-registry: false
	    service-url:
	      #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
	      defaultZone: http://eureka7001.com:7001/eureka/
- 启动类中添加@EnableEurekaServer

在这里插入图片描述
在这里插入图片描述

3.eureka客户端提供者:多个服务集群。8081/8082

spring.application.name的名字相同。eureka的配置也相同。server.port不同

- pom.xml
	<dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
   </dependency>
- application.yml
	eureka:
	  client:
	    register-with-eureka: true #注册进eureka server中
	    fetch-registry: true #是否从eureka中抓取已有的注册信息。默认为true.单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
	    service-url:
	      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
- 启动类中添加@EnableEurekaClient
- controller中
	@Value("${server.port}")
    private String serverPort;
    
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Long id) {
        Payment paymenyById = paymentService.getPaymenyById(id);
        System.out.println(paymenyById);
        if (paymenyById != null) {
            return new Result(200,"数据查询成功,serverPort=" + serverPort,paymenyById);
        }else {
            return new Result(444,"数据查询失败" +
                    ",查询ID为" + id,null);
        }
    }

4.eureka客户端消费者配置(子模块)

- pom.xml
	<dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>
- application.yml
  	spring:
	  application:
	    name: cloud-consumer-order
	eureka:
	  client:
	    register-with-eureka: true
	    fetch-registry: true
	    service-url:
	#      defaultZone: http://localhost:7001/eureka单机版
	       defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
- config/ApplicationContextConfig.java
	@Configuration
	public class ApplicationContextConfig {
	    @Bean
	    @LoadBalanced //开启负载均衡(多个服务在eureka server中服务名相同,通过这个注解开启,当客户端访问的时候可随机访问其中一个服务)
	    public RestTemplate restTemplate(){
	        return new RestTemplate();
	    }
	}
 
- controller中(调用服务端)
	@RestController
	public class OrderController {
	//    public static final String PAYMENT_URL = "http://localhost:8081";//单机版
	    public static final String PAYMENT_URL = "http://CLOUD-PROVIDER-PAYMENT";//集群版。需要开启负载均衡,不然不知道对应哪个服务.CLOUD-PROVIDER-PAYMENT注册在eureka中的名字
	    @Resource //@Resource是java提供的。@Autowired是spring提供的
	    private RestTemplate restTemplate;
	    
	    @GetMapping("/consumer/payment/getById/{id}")
	    public Result<Payment> getById(@PathVariable("id")Long id) {
	        return restTemplate.getForObject(PAYMENT_URL + "/payment/getById/" + id, Result.class);
	    }
	}
- 启动类中添加@EnableEurekaClient

运行结果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.修改微服务的主机名称和显示ip地址

提供者微服务模块

- 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>
- application.yml
eureka:
  instance:
    instance-id: payment8081 #服务名称修改
    prefer-ip-address: true #显示服务的ip地址

在这里插入图片描述
在这里插入图片描述

3. 服务发现Discovery

可以获取注册到eureka里面的微服务的信息

提供者微服务中

- controller
public class PaymentController {
    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("/discovery")
    public Object getInstance() {
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            System.out.println("service:" + service);
        }

        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PROVIDER-PAYMENT");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getInstanceId() + "\t" +instance.getHost()+ "\t" +instance.getPort() + "\t" +instance.getUri());
        }
        return this.discoveryClient;
    }
}
- 启动类中添加@EnableDiscoveryClient

运行结果
在这里插入图片描述
在这里插入图片描述

4.自我保护机制

某时刻某一个微服务不可用了,Eureka不会立刻清理,依旧会对该服务的信息进行存储。属于CAP里面的AP分支

默认是开启自我保护机制
在这里插入图片描述
1.在eureka server中关闭自我保护机制

eureka:
  server:
    enable-self-preservation: false #关闭自我保护机制,保证不可用服务被及时清除

在这里插入图片描述
eureka client中可以添加的配置

eureka:
  instance:
	lease-renewal-interval-in-seconds: 30 #eureka客户端向服务器端发送心跳的时间间隔,单位是秒(默认是30s)
    lease-expiration-duration-in-seconds: 90 #eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90s),超时将剔除服务。	

2、consul

1、安装并运行consul

  • 官网下载,解压后双击运行即可

https://www.consul.io/downloads

  • 查看版本号:consul -version在这里插入图片描述
  • 开发模式启动:consul agent -dev
    在这里插入图片描述
  • 访问:http://localhost:8500
    在这里插入图片描述
    2、运用到服务当中
  • 服务提供者:
- pom.xml
 	<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>
- application.yml
	spring:
	  application:
	    name: cloud-provider-payment
	  #consul注册中心地址
	  cloud:
	    consul:
	      host: localhost
	      port: 8500
	      discovery:
	        service-name: ${spring.application.name}
- 启动类加上@EnableDiscoveryClient
- controller
	@RestController
	public class PaymentController {

	    @Value("${server.port}")
	    private String serverPort;
	
	    @GetMapping("/payment/consul")
	    public String paymentConsul() {
	        return "spring-cloud-consul:" + serverPort + "\t" + UUID.randomUUID().toString();
    }
}

在这里插入图片描述

cloud-provider-payment注册进入consul
在这里插入图片描述

  • 服务消费者
- pom.xml
 	<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>
- application.yml
	spring:
	  application:
	    name: cloud-consumer-order
	  #consul注册中心地址
	  cloud:
	    consul:
	      host: localhost
	      port: 8500
	      discovery:
	        service-name: ${spring.application.name}
- 启动类加上@EnableDiscoveryClient
- config/ApplicationContextConfig
	@Configuration
	public class ApplicationContextConfig {
	    @Bean
	    @LoadBalanced
	    public RestTemplate restTemplate() {
	        return new RestTemplate();
	    }
	}
- controller
	@RestController
	public class ConsumerController {
	
	    private static final String SERVICE_URL = "http://cloud-provider-payment";
	
	    @Resource
	    private RestTemplate restTemplate;
	
	    @GetMapping("/consumer/payment/consul")
	    public String consumerConsul() {
	        return restTemplate.getForObject(SERVICE_URL + "/payment/consul",String.class);
	    }
	}

在这里插入图片描述
在这里插入图片描述

3、zookeeper

总结:三种注册中心的异同点

  • CAP

C:Consistency(强一致性)
A:Availability(可用性)
P:Partition tolerance(分区容错性)
CAP理论关注粒度是数据,而不是整体系统设计的策略

  • CA:单点集群,满足一致性,可用性系统,通常在可可扩展性上不太强大。
  • CP:满足一致性,分区容忍性的系统,通常性能不是特别高。(当数据不一致时,会返回error)
  • AP:满足可用性,分区容忍性的系统。通常可能对一致性要求低一些。(双十一,双十二会采用AP,错失一点类似点赞功能的数据一致性,但是整体还是可用的)
  • 三种注册中心
  • Eureka:AP,java语言编写,通过HTTP即可访问
  • Consul:CP,Go语言编写,通过HTTP/DNS即可访问
  • Zookeeper:CP,java语言编写,通过客户端查看注册进来的服务信息

个人记忆总结

个人总结:注册中心属于服务方(service)相当于物业公司,各种微服务业务模块属于客户方(client)其中(有提供者(属服务端,不被用户直接使用)(公司)和消费者(属客户端,用户直接使用方)(员工))。物业公司管理着各种公司。需要将各种客户方注册到注册中心(服务方)当中。步骤都是pom.xml–>application.yml—>启动类---->controller使用。

  • eureka需要建立一个模块,当作服务方。用该模块的访问链接即可查看注册进来的服务信息。
  • consul只需从官网下载,开发者模式启动即可。访问localhost:8500即可查看有哪些微服务服务注册进来
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值