Eureka管理界面自定義(Greenwich版)


Eureka管理界面自定義

開發工具:SpringToolSuite 4
Spring-cloud版本:Greenwich.SR2,

近來公司用Eureka來做服務管理,要把服務端的界面增加些自己公司的屬性上去,我簡單的記錄下修改的過程,過程如:

構建Eureka項目

創建Eureka項目

本人用的是SpringToolSuite的工具來構建Eureka項目,個人喜歡,工具可自行選擇,pom.xml配置文件如下:

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
	</parent>
	<properties>
		<java.version>1.8</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<spring-cloud.version>Greenwich.SR6</spring-cloud.version>
	</properties>
	<!-- 引入 eureka-serverr的核心包 -->
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</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>

編寫啟動類

@EnableEurekaServer
@SpringBootApplication
public class ServerEurekaApplication extends SpringBootServletInitializer {

	private static final Logger logger = LoggerFactory.getLogger(ServerEurekaApplication.class);

	public static void main(String[] args) {
		ConfigurableApplicationContext context = SpringApplication.run(ServerEurekaApplication.class, args);
		ConfigurableEnvironment env = context.getEnvironment();
		logger.info(
				"\n----------------------------------------------------------\n\t"
						+ "Application '{}' is running! Access URLs:\n\t" + "Local: \t\thttp://localhost:{}"
						+ "\n----------------------------------------------------------",
				env.getProperty("spring.application.name"), env.getProperty("server.port"));
		logger.info("ServerEurekaApplication 已啟動成功.....");
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(this.getClass());
	}
}
注意:
springboot啟動方式:
	1、可以直接在main方法里用SpringApplication.run(EurekaServerApplication.class, args);運行
	2、使用SpringBootServletInitializer啟動,不過需要實現configure方法,這樣用的第二種
區別暫時就不在此講解了。

配置文件

配置application.properties

# eureka 端口
server.port=5761
spring.application.name=eureka
# eureka 配置
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=http://${eureka.instance.hostname}:${server.port}

eureka.client.fetch-registry=true
eureka.client.register-with-eureka=false
eureka.server.enable-self-preservation=false
eureka.client.registry-fetch-interval-seconds=30
eureka.instance.lease-renewal-interval-in-seconds=15
eureka.instance.lease-expiration-duration-in-seconds=45
# logging文件配置
logging.config=classpath:logback-spring.xml
info.logging.path=/eureka/logs/custom-eureka

啟動

在這里插入圖片描述

自定義Eureka頁面

修改頁面模板

在本地倉庫中找到spring-cloud-netflix-eureka-server.jar文件所在的目錄,版本根據maven引入的版本號
在這里插入圖片描述
打開jar文件
在這里插入圖片描述
eureka.jar目錄下的templates文件夾下存放了Erueka Server管理頁面的模板文件,修改時可以將模板文件復制出來到當前項目的resources/templates/eureka目錄下,然后進行自定義界面內容。

  1. header.ftlh:HOEM頁面導航(導航模板頁面模板)
  2. lastn.ftlh:頁面整合模板,整合了header.ftlh與navbar.ftlh增加服務注冊信息展示
  3. navbar.ftlh:System Status+DS Replicas(服務狀態和集群信息頁面模板)
  4. status.ftlh:Instance Info區域(顯示服務器的基本狀態)

找到status.ftlh文件,該文件是顯示InstanceInfo 后面這一欄,文件中增加版權信息,代碼如下:

  <footer style="position: fixed;background-color: rgba(43, 166, 166, 0.5);color: #FFF;bottom: 0;left: 0;right: 0;text-align: center;height: 20px;
lie-height: 20px;">Copyright (C) Eureka 2019-2060, All Rights Reserved XXXXX</footer>

查看效果

重新運行eureka項目,訪問原先的頁面可以看到已經增加了公司版權一欄。
在這里插入圖片描述

總結

以上是簡單的修改了eureka頁面的展示內容,當然再復雜的信息可以自行發揮,如包括可以修改logo,中文翻譯,修改樣式等。
由於本章設計的代碼比較多,請結合源碼進行學習
源碼參考:https://gitee.com/viphzc/springcloud


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM