spring cloud 服务发现之Eureka Client(一)—客户端配置DEMO

这篇文章主要通过DEMO的方式启动一个微服务,并将服务注册到Eureka Server. Eureka Server为本地启动的服务信息,可以通过之前的文章查看启动过程,以及启动原理。

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">
    <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-client</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.12.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

<!--        <dependency>-->
<!--            <groupId>org.springframework.cloud</groupId>-->
<!--            <artifactId>spring-cloud-netflix-ribbon</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>

        <dependency>
            <groupId>com.netflix.eureka</groupId>
            <artifactId>eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>com.netflix.eureka</groupId>
            <artifactId>eureka-core</artifactId>
        </dependency>

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

</project>

服务配置

spring:
  cloud:
    discovery:
      enabled: true
  application:
    name: spring-eureka-client
  main:
    allow-bean-definition-overriding: true
server:
  port: 8080
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

启动服务

package org.spring.learn.eureka.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class SpringEurekaClientDemoApplication {

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

在源码中,有两种方式启动eureka, 可以通过:

  • @EnableEurekaClient注解的方式启动,这种方式主要是针对Eureka提供的注解方式
  • @EanbleDiscoveryClient注解方式启动, 是作为一个通用的方式启动的, 可以针对其他的注册发现组件启动
  • 其实不加注解也是可以启用的,因为EurekaClientAutoConfiguration类默认是根据properties配置启动,后续章节将会介绍

查看日志

2021-09-04 16:31:25.775  INFO 13272 --- [           main] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-1
2021-09-04 16:31:26.801  INFO 13272 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON encoding codec LegacyJacksonJson
2021-09-04 16:31:26.801  INFO 13272 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON decoding codec LegacyJacksonJson
2021-09-04 16:31:26.895  INFO 13272 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using XML encoding codec XStreamXml
2021-09-04 16:31:26.895  INFO 13272 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using XML decoding codec XStreamXml
2021-09-04 16:31:27.084  INFO 13272 --- [           main] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
2021-09-04 16:31:27.892  INFO 13272 --- [           main] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
2021-09-04 16:31:27.892  INFO 13272 --- [           main] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
2021-09-04 16:31:27.892  INFO 13272 --- [           main] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
2021-09-04 16:31:27.892  INFO 13272 --- [           main] com.netflix.discovery.DiscoveryClient    : Application is null : false
2021-09-04 16:31:27.892  INFO 13272 --- [           main] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2021-09-04 16:31:27.892  INFO 13272 --- [           main] com.netflix.discovery.DiscoveryClient    : Application version is -1: true
2021-09-04 16:31:27.892  INFO 13272 --- [           main] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2021-09-04 16:31:28.258  INFO 13272 --- [           main] com.netflix.discovery.DiscoveryClient    : The response status is 200
2021-09-04 16:31:28.258  INFO 13272 --- [           main] com.netflix.discovery.DiscoveryClient    : Starting heartbeat executor: renew interval is: 30
2021-09-04 16:31:28.258  INFO 13272 --- [           main] c.n.discovery.InstanceInfoReplicator     : InstanceInfoReplicator onDemand update allowed rate per min is 4
2021-09-04 16:31:28.258  INFO 13272 --- [           main] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1630744288258 with initial instances count: 0
2021-09-04 16:31:28.258  INFO 13272 --- [           main] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application SPRING-EUREKA-CLIENT with eureka with status UP
2021-09-04 16:31:28.258  INFO 13272 --- [           main] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1630744288258, current=UP, previous=STARTING]
2021-09-04 16:31:28.258  INFO 13272 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SPRING-EUREKA-CLIENT/windows10.microdone.cn:spring-eureka-client:8080: registering service...
2021-09-04 16:31:28.305  INFO 13272 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-09-04 16:31:28.321  INFO 13272 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8080
2021-09-04 16:31:28.399  INFO 13272 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SPRING-EUREKA-CLIENT/windows10.microdone.cn:spring-eureka-client:8080 - registration status: 204
2021-09-04 16:31:29.868  INFO 13272 --- [           main] .l.e.c.SpringEurekaClientDemoApplication : Started SpringEurekaClientDemoApplication in 12.467 seconds (JVM running for 14.206)

 

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注