springboot 2.x--spring cloud Greenwich.SR1 服务注册与发现--eureka搭建以及集群搭建

spring boot 2.x spring cloud Greenwich.SR1 服务注册与发现–eureka搭建以及集群搭建

1、eureka简介

​ Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。

Eureka包含两个组件:Eureka Server和Eureka Client。

Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。

Eureka Client是一个java客户端,用于简化与Eureka Server的交互,客户端同时也就是一个内置的、使用轮询(round-robin)负载算法的负载均衡器。

​ 在应用启动后,将会向Eureka Server发送心跳,默认周期为30秒,如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个服务节点移除(默认90秒)。

​ Eureka Server之间通过复制的方式完成数据的同步,Eureka还提供了客户端缓存机制,即使所有的Eureka Server都挂掉,客户端依然可以利用缓存中的信息消费其他服务的API。综上,Eureka通过心跳检查、客户端缓存等机制,确保了系统的高可用性、灵活性和可伸缩性。

2、项目搭建

springboot版本2.1.3.RELEASE

springcloud版本Greenwich.SR1

1.1、maven的pom文件如下:主要spring-cloud-starter-netflix-eureka-server这个包
<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.badger</groupId>
    <artifactId>badger-spring-cloud-eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>badger-eureka</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</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.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

1.2、springboot主类如下
@SpringBootApplication
@EnableEurekaServer
public class BadgerEurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(BadgerEurekaApplication.class, args);
    }
}

@EnableEurekaServer注解:是开启EurekaServer自动装配的注解。只有写了,才会启动成功

1.3、application.yml配置文件如下
server:
  port: 8761
spring:
  application:
    name: badger-spring-cloud-eureka
eureka:
  server:
    enable-self-preservation: false  #关闭自我保护模式(缺省为打开)生产环境需要打开
    eviction-interval-timer-in-ms: 60000 # 续期时间,即扫描失效服务的间隔时间(缺省为60*1000ms)
  client:
    register-with-eureka: false #设置是否将自己作为客户端注册到注册中心(缺省true,单节点设置成false,集群需要互相同步)
    fetch-registry: false # 设置是否从注册中心获取注册信息(缺省true,单节点设置成false,集群需要互相同步)
    serviceUrl: 
        defaultZone: http://localhost:8761/eureka/  #注册的地址(伙伴机制的方式,实现集群,单节点,指定本机,集群指定伙伴的地址)
1.4启动项目,输入http://localhost:8761/,能够看到如下页面,就算执行成功
1.5、集群配置
server:
  port: 8761
spring:
  application:
    name: badger-spring-cloud-eureka
eureka:
  server:
    enable-self-preservation: false  #关闭自我保护模式(缺省为打开)生产环境需要打开
    eviction-interval-timer-in-ms: 60000 # 续期时间,即扫描失效服务的间隔时间(缺省为60*1000ms)
  client:
    register-with-eureka: true #设置是否将自己作为客户端注册到注册中心
    fetch-registry: true # 设置是否从注册中心获取注册信息
    serviceUrl: 
        defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/  #注册的地址

集群上,只需要配置defaultZone的地址就可以了,还有register-with-eureka和fetch-registry默认为true

具体的配置可以参看org.springframework.cloud.netflix.eureka.EurekaClientConfigBean类,就不在每个解释了

3、自动装配原理

我们先看主类上的@EnableEurekaServer注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EurekaServerMarkerConfiguration.class)
public @interface EnableEurekaServer {

}

@Import(EurekaServerMarkerConfiguration.class) 导入一个实例对象

@Configuration
public class EurekaServerMarkerConfiguration {

	@Bean
	public Marker eurekaServerMarkerBean() {
		return new Marker();
	}
	class Marker {
	}
}

创建了一个Marker的内部类对象。

然后我们再查看spring-cloud-netflix-eureka-server-2.1.1.RELEASE.jar包下的配置,找到org.springframework.cloud.netflix.eureka.server.EurekaServerAutoConfiguration.class

@Configuration
@Import(EurekaServerInitializerConfiguration.class)
@ConditionalOnBean(EurekaServerMarkerConfiguration.Marker.class)
@EnableConfigurationProperties({ EurekaDashboardProperties.class,
		InstanceRegistryProperties.class })
@PropertySource("classpath:/eureka/server.properties")
public class EurekaServerAutoConfiguration extends WebMvcConfigurerAdapter {

@ConditionalOnBean(EurekaServerMarkerConfiguration.Marker.class)

我们主要看这个,必要要有EurekaServerMarkerConfiguration.Marker.class这个类的实例,才会自动注入这个自动装配的类;而这个类,就是上面说到的主类上的@EnableEurekaServer注解,导入的。

关于org.springframework.cloud.netflix.eureka.server.EurekaServerAutoConfiguration.class装配了什么内容,大家有兴趣的,可以自己查看。

4、eureka的自我保护机制

eureka的保护模式,主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护,一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据,也就是不会注销任何微服务。测试环境下,可以通过如下配置,关闭自我保护。

eureka:
  server:
    #测试时关闭自我保护机制,保证不可用服务及时剔除
    enable-self-preservation: false
    #间隔2秒
    eviction-interval-timer-in-ms: 2000

具体代码信息,可以查看《码云》

原创文章 83 获赞 155 访问量 36万+

猜你喜欢

转载自blog.csdn.net/qq_28410283/article/details/100891529