Spring Cloud 服务注册中心Eureka

Eureka介绍

在Spring Cloud Netflix 整合技术栈中,Eureka既可以作为服务注册中心也可以用于服务发现对整个微服务架构起着最核心的整合作用。
Eureka是基于REST(Representational State Transfer)服务,主要以AWS云服务为支撑,提供服务发现并实现负载均衡和故障转移。在Netflix,为Eureka提供更为复杂的负载均衡方案进行封装,以实现高可用,它包括基于流量、资源利用率以及请求返回状态的加权负载均衡。
现有的Eureka 2.0开源工作已经停止。作为2.x分支上现有工作存储库的一部分发布的代码库和工件被认为是自担风险使用的。
虽然Eureka开源工作已经停止,但是它以前提供的版本的功能足以对需要项目的业务场景适用。
除了Eureka,还可以使用 Feature,Consul,zookeeper等来作为服务注册中心。

父工程创建

现在创建一个cloud-spring 的pom工程作为本次Spring Cloud所有章篇的一个父工程。
父工程pom.xml配置
这次我们使用Spring Cloud Finchley.RELEASE版,这个版本相对是比较稳定的。

    <!-- 定义依赖版本进行依赖管理 -->
    <properties>
        <spring-cloud-version>Finchley.RELEASE</spring-cloud-version>
        <spring-boot-version>2.0.4.RELEASE</spring-boot-version>
    </properties>
    
    <dependencyManagement>
        <dependencies>
            <!-- SpringCloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- SpringBoot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <!-- 插件 -->
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>build-info</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <target>${java.version}</target>
                        <source>${java.version}</source>
                        <encoding>${project.reporting.outputEncoding}</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

使用Spring Cloud Finchley.RELEASE版注意事项:Spring Boot 版本请勿低于2.0.3否则导致很多依赖启动报错

搭建Eureka服务注册中心

在父工程下创建子模块,工程名:cloud-netfix-eureka-server
在pom.xml中添加依赖:

 <dependencies>
        <!-- Eureka服务端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
 </dependencies>

创建启动类 CommenEurekaServerApplication.java

/**
 * Eureka注册中心服务
 * @author SimpleWu
 */
@SpringBootApplication
@EnableEurekaServer
public class CommenEurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(CommenEurekaServerApplication.class,args);
    }
}

通过@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话
然后在稍微写下配置即可搭建好一个服务注册中心,下面编写application.properties(yml):

server.port=10001
spring.application.name=cloud-netflix-eureka-server

eureka.instance.hostname=localhost
# 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己
eureka.client.register-with-eureka=false
# 由于注册中心的职责就是维护服务实例,他并不需要去检索服务,所以也设置为false
eureka.client.fetch-registry=false
# 关闭自我保护
eureka.server.enableSelfPreservation=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
#清理无效节点的频率 30秒
eureka.server.eviction-interval-timer-in-ms=30000
#表示 Eureka Server 开启自我保护的系数,默认:0.85。
eureka.server.renewal-percent-threshold=0.85

在默认配置下Eureka服务端会主动注册自己,在这里我们只作为服务注册中心所以不需要注册到服务中心,在以后的集群模式需要将Eureka服务高可用集群时需要注册到其他的注册中心。
并且Eureka提供服务事件监听,如下:

/**
 * Eureka事件监听
 * @author SimpleWu
 */
@Component
public class EurekaListener {

    private final  Logger log = LoggerFactory.getLogger(EurekaListener.class);

    @EventListener
    public void listen(EurekaInstanceCanceledEvent event) {
        log.debug(event.getServerId() + "\t" + event.getAppName() + " 服务下线");
    }
    @EventListener
    public void listen(EurekaInstanceRegisteredEvent event) {
        InstanceInfo instanceInfo = event.getInstanceInfo();
        log.debug(instanceInfo.getAppName() + "进行注册");
    }
    @EventListener
    public void listen(EurekaInstanceRenewedEvent event) {
        log.debug(event.getServerId() + "\t" + event.getAppName() + " 服务进行续约");
    }
    @EventListener
    public void listen(EurekaRegistryAvailableEvent event) {
        log.debug("注册中心 启动");
    }
    @EventListener
    public void listen(EurekaServerStartedEvent event) {
        log.debug("Eureka Server 启动");
    }
}

然后我们通过启动类CommenEurekaServerApplication.java 允许main函数进行启动。
访问: http://localhost:10001/ 即可看到Eureka服务治理中心,其中Instances currently registered with Eureka 还没有注册任何实例。
该案例代码可参考:https://github.com/450255266/open-doubi/tree/master/SpringCloud

点赞