SpringCloud Eureka初体验

本文详细介绍如何使用Spring Cloud Netflix Eureka搭建服务发现组件。包括创建Eureka Server和Client的步骤,配置application.properties和bootstrap.properties文件,以及如何通过浏览器访问和查看服务信息。

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

Eureka是SpringCloud Netflix提供的服务发现组件,本文介绍Eureka Server及Client的搭建步骤。

Eureka Server

创建Eureka Server项目

为了方便起见,我们从Spring Initializr创建一个Eureka Server :
在这里插入图片描述
将得到的压缩包解压后导入到idea中。

配置application.properties

在application.properties中加入以下配置:

server.port=8761

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF

增加@EnableEurekaServer注解

给程序入口类中加上@EnableEurekaServer,标记这是一个Euraka Server应用,如下:

 package com.example.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaserverApplication {
	public static void main(String[] args) {
		SpringApplication.run(EurekaserverApplication.class, args);
	}
}

完成以上步骤,不出意外的化就可以把程序跑起来了

访问Eureka Server

打开浏览器,输入地址http://localhost:8761,出现以下界面则表示Eureka Server创建成功:
在这里插入图片描述

Eureka Client

创建Eureka Client项目

同样,我们从Spring Initializr创建一个Eureka Client :
在这里插入图片描述

配置bootstrap.properties

创建bootstrap.properties配置文件,在文件中加入:

spring.application.name=a-bootiful-client

编写EurekaclientApplication.java

将Spring initializr默认生成的EurekaclientApplication.java替换成以下内容:

package com.example.eurekaclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaclientApplication {

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

@RestController
class ServiceInstanceRestController {

	@Autowired
	private DiscoveryClient discoveryClient;

	@RequestMapping("/service-instances/{applicationName}")
	public List<ServiceInstance> serviceInstancesByApplicationName(
			@PathVariable String applicationName) {
		return this.discoveryClient.getInstances(applicationName);
	}
}

完成上述步骤便可以运行该应用

访问Eureka Client

打开浏览器,输入地址http://localhost:8080/service-instances/a-bootiful-client,可以看到一下界面:
在这里插入图片描述

在Eureka Server上查看Client的信息

在这里插入图片描述

参考资料

https://spring.io/guides/gs/service-registration-and-discovery/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值