spring cloud服务注册之Eureka Server - DEMO

15 9 月, 2021 145点热度 0人点赞 0条评论

在之前章节中介绍了Eureka Server的启动流程,  该篇文章主要实现一个eureka server的简单demo, 便于在本地做调试。

创建项目

在本地中,创建一个maven项目, 具体的pom.xml配置如下:

<?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">
    <parent>
        <artifactId>spring-cloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-cloud-eureka-server</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-context</artifactId>
        </dependency>
    </dependencies>

</project>

创建启动类

创建EurekaServer启动类型, 取名为SpringEurekaServerApplication, 具体代码如下:

package org.spring.learn;

import com.netflix.appinfo.ApplicationInfoManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class SpringEurekaServerApplication {

    @Autowired
    private ApplicationInfoManager applicationInfoManager;

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

application.yaml配置

application.yaml作为spring启动时的重要文件,里面配置了eureka server启动时的参数信息, 具体配置如下:

spring:
  main:
    allow-bean-definition-overriding: true
server:
  port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8761/eureka

当我们执行启动类型时,可以看到如下日志:

2021-09-15 18:10:22.861  INFO 123840 --- [      Thread-11] o.s.c.n.e.server.EurekaServerBootstrap   : Eureka data center value eureka.datacenter is not set, defaulting to default
2021-09-15 18:10:22.862  INFO 123840 --- [      Thread-11] o.s.c.n.e.server.EurekaServerBootstrap   : Eureka environment value eureka.environment is not set, defaulting to test
2021-09-15 18:10:22.880  INFO 123840 --- [      Thread-11] o.s.c.n.e.server.EurekaServerBootstrap   : isAws returned false
2021-09-15 18:10:22.880  INFO 123840 --- [      Thread-11] o.s.c.n.e.server.EurekaServerBootstrap   : Initialized server context
2021-09-15 18:10:22.880  INFO 123840 --- [      Thread-11] c.n.e.r.PeerAwareInstanceRegistryImpl    : Got 1 instances from neighboring DS node
2021-09-15 18:10:22.880  INFO 123840 --- [      Thread-11] c.n.e.r.PeerAwareInstanceRegistryImpl    : Renew threshold is: 1
2021-09-15 18:10:22.880  INFO 123840 --- [      Thread-11] c.n.e.r.PeerAwareInstanceRegistryImpl    : Changing status to UP
2021-09-15 18:10:22.882  INFO 123840 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8761 (http) with context path ''
2021-09-15 18:10:22.882  INFO 123840 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8761
2021-09-15 18:10:22.886  INFO 123840 --- [      Thread-11] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2021-09-15 18:10:23.296  INFO 123840 --- [           main] o.s.l.s.SpringEurekaServerApplication    : Started SpringEurekaServerApplication in 7.681 seconds (JVM running for 8.488)

则表示启动成功服务成功,我们可以通过访问http://localhost:8761访问控制台界面,表示服务正常执行。

专注着

一个奋斗在编程路上的小伙

文章评论