【微服务】初识Spring Cloud、Eureka 注册中心、Eureka集群、Eureka自我保护机制

一、Spring Cloud

1.什么是Spring Cloud?
Spring Cloud官网
Spring Cloud是在Spring Boot基础上构建的,用于简化分布式系统构建的工具集。
Spring Cloud的主要子项目
① Spring Cloud Netflix:集成了各种OOS组件,其中包括Eureka、Ribbon、Hystrix、Zuul、Feign和Archaius等
② Spring Cloud Config:配置管理工具,支持使用Git存储配置内容,可以使用它实现婴童的部署的外部化存储,并支持客户端配置信息刷新、加密和解密等配置内容
③ Spring Cloud Starter:Spring Cloud的基础组件,是基于Spring Boot风格的基础依赖模块
2.Spring Cloud的特点
(1)使用方便
(2)功能齐全
(3)易于扩展和维护
(4)使用于各种环境
3.Spring Boot与Spring Cloud版本的兼容性
在这里插入图片描述
4.Devtools热部署
在这里插入图片描述
(1)模块添加Devtools依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>

(2)父项目添加plugin

<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>

(3)快捷键打开:ctrl+shift+alt+/,选Registry,然后勾选如下两栏。然后关闭重启idea,配置已经生效。
在这里插入图片描述
5.工程重构
(1)新建module,引入pom.xml依赖

<dependencies>
    <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>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>

(2)复制entity(包括全类名),maven命令点击clean,install
(3)删除原本的entity,各自添加如下依赖,重构完成。

<dependency>
	<groupId>org.example</groupId>
	<artifactId>cloud-api-commons</artifactId>
	<version>${project.version}</version>
</dependency>

在这里插入图片描述

二、Eureka注册中心

在这里插入图片描述
Eureka是Netflix开发的一个服务发现框架,本身就是一个基于REST的服务。包含两大组件:
(1)服务端发现组件,也称服务注册中心(Eureka Server):主要提供服务的注册功能
(2)客户端发现组件(Eureka Client):主要用于处理服务的注册与发现
1.如何搭建服务注册中心:
① 服务端工程pom.xml依赖

<!--除了SpringBoot和SpringCloud之外必须的依赖外,还要此依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

② 配置文件application.yml

server:
  port: 7001  #端口号
eureka:
  instance:
    hostname: localhost #实例名
  client:
    register-with-eureka: false #不向注册中心注册自己
    fetch-registry: false #false表示自己就是注册中心,职责就是维护注册实例
    service-url:
      defaultZone: http://${
    
    eureka.instance.hostname}:${
    
    server.port}/eureka/  #注册中心的地址

③ Spring Boot的核心类上的注解:@EnableEurekaServer
2.如何搭建客户端组件
① 服务端工程pom.xml依赖

<!--除了SpringBoot和SpringCloud之外的依赖,还需要引入该依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

② 配置文件application.yml

server:
  port: 8001  #Eureka实例的端口号
spring:
  application:
    name: cloud-payment-service #指定实例的名称  
eureka:
  instance:
    prefer-ip-address: true
  client:
    register-with-eureka: true  #向注册中心注册自己
    fetch-registry: true  #false表示自己就是注册中心,职责就是维护注册实例
    service-url:
      defaultZone: http://localhost:7001/eureka/  #指定eureka的服务端地址

③ Spring Boot的核心类上的注解:@EnableEurekaClient
3.Eureka集群
作用:高可用,互相注册、相互守望
在这里插入图片描述
(1)Eureka集群注册中心配置文件application.yml

server:
  port: 7001  #端口号
eureka:
  instance:
    hostname: eureka7001.com #实例名
  client:
    register-with-eureka: false #不向注册中心注册自己
    fetch-registry: false #false表示自己就是注册中心,职责就是维护注册实例
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/  #端口号相互注册,注册中心的地址

(2)服务提供者集群配置文件application.yml

server:
  port: 8001  #Eureka实例的端口号
spring:
  application:
    name: cloud-payment-service #指定实例的名称
eureka:
  instance:
    prefer-ip-address: true
  client:
    register-with-eureka: true  #向注册中心注册自己
    fetch-registry: true  #false表示自己就是注册中心,职责就是维护注册实例
    service-url:
      #defaultZone: http://localhost:7001/eureka/  #指定eureka的服务端地址
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/  #eureka集群

(3)服务消费消费唯一,配置文件application.yml

server:
  port: 80
spring:
  application:
    name: cloud-order-service
eureka:
  instance:
    prefer-ip-address: true
  client:
    register-with-eureka: true  #向注册中心注册自己
    fetch-registry: true  #false表示自己就是注册中心,职责就是维护注册实例
    service-url:
      #指定eureka的服务端地址
      #defaultZone: http://localhost:7001/eureka/
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/  #eureka集群

(4)在服务消费者的配置类中添加负载均衡注解@LoadBalanced

@Configuration
public class ApplicationContextConfig {
    
    
    @Bean
    @LoadBalanced   //实现调用订单服务时服务的负载均衡
    public RestTemplate getRestTemplate(){
    
    
    //RestTemplate是Spring提供的用于访问Rest服务的客户端实例,它提供了多种便捷访问远程Http服务的方法,只需要传入url及返回值类型即可。
        return new RestTemplate();
    }
}

(5)客户端组件服务名称的规范,在对应微服务的配置文件application.yml中添加配置:

server:
  port: 8001  #Eureka实例的端口号
spring:
  application:
    name: cloud-payment-service #指定实例的名称
eureka:
  instance:
    prefer-ip-address: true	#选中时是否显示主机的Ip
    instance-id: payment8001  #指定id
  client:
    register-with-eureka: true  #向注册中心注册自己
    fetch-registry: true  #false表示自己就是注册中心,职责就是维护注册实例
    service-url:
      #defaultZone: http://localhost:7001/eureka/  #指定eureka的服务端地址
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/  #eureka集群

4.服务发现Discovery
功能:对于注册进Eureka里面的微服务,可以通过服务发现来获得该服务的信息
步骤:
① 在服务提供者对应的controller中,添加如下内容

@RestController
@Slf4j	//日志记录
public class PaymentController {
    
    
	@Resource
    private DiscoveryClient discoveryClient;
    @GetMapping("/payment/discovery")
    public Object discovery() {
    
    
        //获取服务
        List<String> services = discoveryClient.getServices();
        for(String element: services) {
    
    
            log.info("*******element**********"+element);
        }
        //获取服务的实例
        List<ServiceInstance> instances = discoveryClient.getInstances("cloud-payment-service");
        for (ServiceInstance instance: instances) {
    
    
            log.info(instance.getInstanceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
        }
        return this.discoveryClient;	//返回当前的实例
    }
}

② 在服务提供者的主启动类上添加注解:@EnableDiscoveryClient

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient  //开启服务发现
public class PaymentMain8001 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(PaymentMain8001.class,args);
    }
}

5.Eureka保护机制
某一时刻一个服务不可用了,Eureka不会立即清理,依旧会对当前服务的信息保留。属于CAP里面的AP分支。
在自我保护模式(默认开启)中,Eureka Server会保护服务注册表中的信息,不再注销任何服务实例。
如何禁止自我保护?
此处以单机版的注册中心为例
① 注册中心application.yml配置

server:
  port: 7001  #端口号
eureka:
  instance:
    hostname: eureka7001.com #实例名
  client:
    register-with-eureka: false #不向注册中心注册自己
    fetch-registry: false #false表示自己就是注册中心,职责就是维护注册实例
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/  #注册中心的地址
  server:
    enable-self-preservation: false #false关闭自我保护机制,保证不可用的服务被及时移除
    eviction-interval-timer-in-ms: 2000 #单位:毫秒

② 服务提供者application.yml配置

server:
  port: 8001  #Eureka实例的端口
spring:
  application:
    name: cloud-payment-service #指定实例的名称
eureka:
  client:
    register-with-eureka: true  #向注册中心注册自己
    fetch-registry: true  #false表示自己就是注册中心,职责就是维护注册实例
    service-url:
      defaultZone: http://localhost:7001/eureka/  #指定eureka的服务端地址
      #defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/  #eureka集群
  instance:
    instance-id: payment8001  #指定id
    prefer-ip-address: true #是否选中的时候显示主机的ip
    lease-expiration-duration-in-seconds: 2 #Eureka服务端在接收到客户端发送的最后一次心跳后的等待时间,超时将清除服务(默认:90秒
    lease-renewal-interval-in-seconds: 1  #Eureka客户端向服务端发送心跳的时间间隔(默认:30秒)

猜你喜欢

转载自blog.csdn.net/weixin_46081857/article/details/123477202