springcloud+eureka再相识

本文介绍了如何在Spring Cloud环境中配置并使用Eureka作为服务注册与发现组件。通过添加相关依赖,配置application.properties,以及创建监听器来监控服务的注册、取消和心跳事件。启动Eureka服务器后,可以观察到服务的注册情况,并在测试工程中验证其功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在微服务架构中,经常会遇到服务注册与发现的组件,下面重新认识一下springcloud中的eureka组件。
开门见山,上实例,具体原理参考官网。
1依赖

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.4</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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>

2application.properties


spring.application.name=eureka
server.port=9001

eureka.instance.hostname=localhost
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false

eureka.client.fetch-registry=false为不去获取服务列表
eureka.client.register-with-eureka=false为本身不去注册
3代码

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

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

}

4监听

@Configuration
public class InstanceCancelListener implements ApplicationListener<EurekaInstanceCanceledEvent> {

    @Override
    public void onApplicationEvent(EurekaInstanceCanceledEvent event) {
        System.out.println(String.format("服务:%s挂了",event.getAppName()));
    }
}

@Configuration
public class InstanceRegisterListener implements ApplicationListener<EurekaInstanceRegisteredEvent> {

    @Override
    public void onApplicationEvent(EurekaInstanceRegisteredEvent event) {
        System.out.println(String.format("服务:%s,注册成功了",event.getInstanceInfo().getAppName()));
    }
}

@Configuration
public class InstanceRenewListener implements ApplicationListener<EurekaInstanceRenewedEvent> {

    @Override
    public void onApplicationEvent(EurekaInstanceRenewedEvent event) {
        System.out.println(String.format("心跳检测服务:%s",event.getInstanceInfo().getAppName()));
    }
}

5启动
启动后可访问,如
在这里插入图片描述
6测试
新建一个测试工程,在启动类中添加@EnableDiscoveryClient
在配置文件中添加

#使用IP注册
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
eureka.client.service-url.defaultZone=http://localhost:9001/eureka/

启动该测试工程,查看eureka网页,如
在这里插入图片描述
出现如上即可,eureka服务配置完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值