SpringCloud学习笔记-Eureka服务治理

Eureka服务治理

Spring Cloud

目前在大型的招聘,或者是Java界,对SpringCloud的要求也是越来越多,有的公司不仅仅要求了解基本的配置信息,以及代码书写能力,而且深入源码,了解底层。

本人不才,也不知道底层是怎么实现的,就基于Eureka的服务治理做一下笔记整理。

不足之处,还望各位大神不吝赐教,再次感激涕零。

基于Eureka实现的服务治理,在理论上和zookeeper对服务治理差不多

关系调用:

服务生产者启动时,向服务注册中心注册自己提供的服务。

服务消费者启动时,向服务注册中心订阅自己需要的服务。

注册中心会返回服务提供者的地址信息给消费者。

消费者则从服务提供者调用服务。

使用Eureka进行服务治理:

首先搭建注册中心,为了以后项目能在此基础上继续开发,所以选择了基于maven建立模块化。

建立父项目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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zhaixingzu</groupId>
    <artifactId>yyc</artifactId>
    <version>1.1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>yyc</name>
    <description>yyc-pom</description>

    <modules>
        <module>yyc-registry</module>
    </modules>

    <!-- 集中定义版本号 -->
    <properties>
        <spring-boot.version>2.1.3.RELEASE</spring-boot.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <!--eureka 客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--测试依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

下来建立yyc-registry 

在yyc-registry中添加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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.zhaixingzu</groupId>
        <artifactId>yyc</artifactId>
        <version>1.1.0-SNAPSHOT</version>
    </parent>

    <artifactId>yyc-register</artifactId>
    <packaging>jar</packaging>

    <name>yyc-register</name>
    <description>yyc 注册中心</description>

    <dependencies>
        <!--服务中心-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--security-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-security</artifactId>
        </dependency>
        <!--web 模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!--排除tomcat依赖-->
                <exclusion>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--undertow容器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

在RegistryApplication中我们添加注解

package com.zhaixingzu.yyc.registry;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * 服务注册中心
 * @author Herbert
 * @date 2019年06月19日
 */
@EnableEurekaServer
@SpringBootApplication
public class RegistryApplication {

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

}

在application.properties中添加配置信息:

server.port=9900
spring.application.name=yyc-registry
spring.security.user.name=admin
spring.security.user.password=admin
spring.application.admin.enabled=false

eureka.instance.hostname=127.0.0.1
eureka.instance.prefer-ip-address=true

#是否开启自我保护(运行期间spring会统计信条失败的比例在15分钟之内是否低于85%,如果不低于85%,Eureka会将实例注册信息保护起来,让这些实例不会过期)
eureka.server.enable-self-preservation= false
#3秒钟自动剔除失效的节点,清理无效的节点
eureka.server.eviction-interval-timer-in-ms= 3000
#eureka server刷新readCacheMap的时间,注意,client读取的是readCacheMap,这个时间决定了多久会把readWriteCacheMap的缓存更新到readCacheMap上
eureka.server.response-cache-update-interval-ms= 3000
#eureka server缓存readWriteCacheMap失效时间,这个只有在这个时间过去后缓存才会失效,失效前不会更新,过期后从registry重新读取注册服务信息,registry是一个ConcurrentHashMap。
#由于启用了evict其实就用不太上改这个配置了
#默认180s
eureka.server.response-cache-auto-expiration-in-seconds=180

#不要向注册中心注册自己
eureka.client.register-with-eureka=false
#禁止检索服务 设置为true可以从其他eureka节点获取注册信息
eureka.client.fetch-registry=false
#设置与eureka交互的地址
eureka.client.service-url.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/

到此所有的配置已经完成,直接启动启动类:

访问页面:

输入账号,密码:

注:从上图看到,在"Instances currently registered with Eureka"信息中,没有一个实例,说明目前还没有服务注册。

注册中心启动完成

下来记录其中重点笔记:

1:在父级项目yyc中定义的maven包为pom,引用了一些SpringCloud项目常用的一些依赖包,这样在每一个子项目中就不会再去依赖

2:子项目yyc-registry中排除了springboot用tomcat作为内嵌容器,引进了undertow容器,tomcat和undertow的负载能力基本差不多。引进的原因是因为undertow为轻量级容器,比Jeety性能稍优

3:@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan

4:application.properties 和 application.yml 中对配置没有任何区别,yml的配置文件可增强可读性,在书写方面也比较方便

5:如需要开启密码认证,需要进行安全配置:

引人Jar包

并且在application中进行配置用户名与密码:

设置与eureka的交互地址:

eureka.client.service-url.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/

6:配置信息详解:

server:
  port: 9900

spring:
  application:
    name: yyc-registry
  security:
    user:
      name: admin
      password: admin
  cloud:
    config:
      enabled: false


eureka:
  instance:
    hostname: 127.0.0.1
    prefer-ip-address: true
  server:
    #是否开启自我保护(运行期间spring会统计信条失败的比例在15分钟之内是否低于85%,如果不低于85%,Eureka会将实例注册信息保护起来,让这些实例不会过期)
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 3000      #3秒钟自动剔除失效的节点,清理无效的节点
    response-cache-update-interval-ms: 3000  #eureka server刷新readCacheMap的时间,注意,client读取的是readCacheMap,这个时间决定了多久会把readWriteCacheMap的缓存更新到readCacheMap上
    response-cache-auto-expiration-in-seconds: 180
    #eureka server缓存readWriteCacheMap失效时间,这个只有在这个时间过去后缓存才会失效,失效前不会更新,过期后从registry重新读取注册服务信息,registry是一个ConcurrentHashMap。
    #由于启用了evict其实就用不太上改这个配置了
    #默认180s
  client:
    register-with-eureka: false #不要向注册中心注册自己
    fetch-registry: false #禁止检索服务 设置为true可以从其他eureka节点获取注册信息
    service-url:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/  #设置与eureka交互的地址

7: 对于配置application.yml 和 bootstrap.yml 的区别:

  • Spring Cloud 构建于 Spring Boot 之上,在 Spring Boot 中有两种上下文,一种是 bootstrap,另外一种是 application,

  • application 配置文件这个容易理解,主要用于 Spring Boot 项目的自动化配置。

  • bootstrap 是应用程序的父上下文,也就是说 bootstrap 加载优先于 applicaton。

  • bootstrap 主要用于从额外的资源来加载配置信息,还可以在本地外部配置文件中解密属性。

  • 这两个上下文共用一个环境,它是任何Spring应用程序的外部属性的来源。

  • bootstrap 里面的属性会优先加载,它们默认也不能被本地相同配置覆盖。

  • boostrap 由父 ApplicationContext 加载,比 applicaton 优先加载

  • boostrap 里面的属性不能被覆盖

8:Eureka中启动成功后显示红色标记(如下图)

系统在三种情况下会出现红色加粗的字体提示:

a.在配置上,自我保护机制关闭
RENEWALS ARE LESSER THAN THE THRESHOLD. THE SELF PRESERVATION MODE IS TURNED OFF.THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.

b.自我保护机制开启了
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE
NOT BEING EXPIRED JUST TO BE SAFE.

c.在配置上,自我保护机制关闭了,但是一分钟内的续约数没有达到85% , 可能发生了网络分区,会有如下提示
THE SELF PRESERVATION MODE IS TURNED OFF.THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.

9:在启动加入banner.txt可以出现banner自定义样式

欢迎关注公众号:

发布了56 篇原创文章 · 获赞 22 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/weixin_41986096/article/details/92842618