SpringCloud之搭建Eureka-Admin

本文详细介绍如何通过Maven依赖和YAML配置搭建Eureka Admin服务,实现对Eureka注册服务的详细信息查看,包括环境详情、内存和线程情况等。通过启用特定注解,服务可自动注册到Eureka,并通过提供的UI界面进行监控和管理。

虽然说在eureka的地址可以看到已经注册到此服务,但有时候需要看到此服务的详情,如:服务所在的环境详情、内存情况、线程情况等,都无法看到,此时需要用到eureka-admin。

1.maven依赖

<?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">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.xichuan.dev</groupId>
	<artifactId>xichuan-eureka-admin</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>xichuan-eureka-admin</name>
	<description>eureka manage</description>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.6.RELEASE</version>
		<relativePath></relativePath>
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-server</artifactId>
			<version>1.5.6</version>
		</dependency>
		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-server-ui</artifactId>
			<version>1.5.6</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
			<version>1.3.2.RELEASE</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

2.application.yml配置

server:
  port: 2002

spring:
  application:
  #注册到eureka的名称
    name: admin-server
  boot:
    admin:
      routes:
        endpoints: env,metrics,trace,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,hystrix.stream
#注册到eureka
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:2001/eureka/

management.security.enabled: false

3.启动类添加注解

import de.codecentric.boot.admin.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableAdminServer       /**eureka-admin服务*/
@EnableDiscoveryClient   /**注册到eureka*/
public class XichuanEurekaAdminApplication {

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

直接运行,后可以看到eureka注册已经注册了eureka-admin

访问http://localhost:2002/,就可以使用eureka-admin了

   github上有更多的笔记:Raray-chuan (兮川) · GitHub

### Spring Cloud 中搭建 Eureka 项目的详细步骤 在微服务架构中,Eureka 是 Spring Cloud 提供的服务注册与发现的核心组件。以下是搭建 Eureka 项目的具体方法。 #### 1. 创建父工程 创建一个 Maven 父工程以统一管理所有子模块的依赖版本和插件配置。父工程的 `pom.xml` 文件示例如下[^1]: ```xml <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"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>springcloud-parent</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <properties> <spring-cloud.version>Hoxton.SR12</spring-cloud.version> </properties> <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> </project> ``` #### 2. 创建 Eureka Server 模块 在父工程下创建一个名为 `eureka-server` 的子模块,并配置其 `pom.xml` 文件。关键依赖如下[^3]: ```xml <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> ``` #### 3. 配置 Eureka Server 在 `eureka-server` 模块的主类上添加 `@EnableEurekaServer` 注解以启用 Eureka Server 功能: ```java package com.example.eurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } } ``` 同时,在 `application.yml` 或 `application.properties` 文件中进行必要的配置: ```yaml server: port: 7001 eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ ``` #### 4. 创建 Eureka Client 模块 创建一个名为 `eureka-client` 的子模块,并在其 `pom.xml` 文件中添加以下依赖[^5]: ```xml <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> ``` 在客户端主类上添加 `@EnableEurekaClient` 注解[^5]: ```java package com.example.eurekaclient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } } ``` 同时,在 `application.yml` 或 `application.properties` 文件中配置客户端连接到 Eureka Server 的地址: ```yaml spring: application: name: eureka-client eureka: client: service-url: defaultZone: http://localhost:7001/eureka/ ``` #### 5. 测试 Eureka Server 和 Client 启动 `eureka-server` 模块后,访问 `http://localhost:7001/` 可以查看 Eureka 控制台。启动 `eureka-client` 模块后,客户端会自动注册到 Eureka Server 上。 #### 6. 可选:集成 Spring Security(如果需要) 如果需要为 Eureka Server 添加安全认证功能,可以在 `eureka-server` 模块中引入 Spring Security 相关依赖[^4]: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 并在配置文件中设置用户名和密码[^4]: ```yaml spring: security: user: name: admin password: 123456 ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值