• 欢迎来到Compiler网站,如果对网站内容感兴趣或者帮助到你,请为文章点赞,谢谢!

spring cloud服务注册之Eureka Server – DEMO

Spring Cloud 奔跑的蜗牛 1周前 (09-15) 13次浏览 已收录 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访问控制台界面,表示服务正常执行。


Compiler编程笔记 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:spring cloud服务注册之Eureka Server – DEMO
喜欢 (0)
[阳光路上]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址