物联网架构成长之路(30)-Spring Boot Admin微服务WebUI监控
0. 前言
一个完整的微服务解决方案包含了许多微服务,基于我们需要观察各个微服务的运行状态,因此Spring Boot 生态提供了Spring Boot Admin 这个组件来实现微服务管理WEB UI。但是整体的注册中心还是基于Eureka,只是WebUI是用这个Spring Boot Admin 来显示而已。具体的结构如下所示
1. Eureka服务
这个没什么好说的,按照创建一个微服务的流程,通过 Spring Starter 工具,自动生成一个Eureka微服务。主要就是配置Application.java application.yml pom.xml 这三个文件。
pom.xml
<?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.wunaozai.eureka</groupId>
<artifactId>global-service-eureka</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging> <name>global-service-eureka</name>
<description>服务注册中心</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.M2</spring-cloud.version>
</properties> <dependencies>
<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> <repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> </project>
application.yml 这里我配置两份,一份EUREKA1:8761 一份EUREKA:8762
server:
port: 8761 spring:
application:
name: EUREKA服务注册中心 management:
endpoints:
web:
exposure:
include:
- "*"
endpoint:
health:
show-details: ALWAYS eureka:
client:
# register-with-eureka: false
# fetch-registry: false
service-url:
defaultZone: http://EUREKA1:8761/eureka/,http://EUREKA2:8762/eureka/
instance:
hostname: EUREKA1
Application.java
package com.wunaozai.eureka; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer
@SpringBootApplication
public class GlobalServiceEurekaApplication { public static void main(String[] args) {
SpringApplication.run(GlobalServiceEurekaApplication.class, args);
}
}
2. Spring Boot Admin 服务
默认是不需要帐号密码登录的,我这里配置一下spring-boot-starter-security 增加帐号密码登录
pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wunaozai.admin.demo</groupId>
<artifactId>spring-cloud-admin-demo</artifactId>
<version>0.0.1</version>
<name>spring-cloud-admin-demo</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.1.1</spring-boot-admin.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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-security</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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> </project>
applicatoin.yml
server:
port: 8788 spring:
boot:
admin:
client:
url:
- "http://ADMIN:8788" #这里配置Spring Boot Admin 服务地址,配置这里表示把自己注册到Admin上,如果使用Eureka服务发现的,这部分可以省略
username: 'user'
password: 'password'
application:
name: WEB服务监控中心
security:
user:
name: 'user'
password: 'password' management:
endpoints:
web:
exposure:
include:
- "*"
endpoint:
health:
show-details: ALWAYS info:
version: ${spring.application.name}:${server.port} eureka:
instance:
lease-renewal-interval-in-seconds: 10
health-check-url-path: /actuator/health
# 注册给eureka的时候告诉eureka自己的密码
metadata-map:
"user.name": ${spring.security.user.name}
"user.password": ${spring.security.user.password}
prefer-ip-address: true
client:
registry-fetch-interval-seconds: 5
# fetch-registry: false
# register-with-eureka: false
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://EUREKA1:8761}/eureka/,${EUREKA_SERVICE_URL:http://EUREKA2:8762}/eureka/
Application.java
package com.wunaozai.admin.demo; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; import de.codecentric.boot.admin.server.config.AdminServerProperties;
import de.codecentric.boot.admin.server.config.EnableAdminServer; @Configuration
@EnableAdminServer
@EnableEurekaClient
@SpringBootApplication
public class SpringCloudAdminDemoApplication { public static void main(String[] args) {
SpringApplication.run(SpringCloudAdminDemoApplication.class, args);
} @Configuration
public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath; public SecuritySecureConfig(AdminServerProperties prop) {
this.adminContextPath = prop.getContextPath();
} @Override
protected void configure(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler();
handler.setTargetUrlParameter("redirectTo"); http.authorizeRequests()
.antMatchers(adminContextPath + "/assets/**").permitAll()
.antMatchers(adminContextPath + "/login").permitAll()
.antMatchers(adminContextPath + "/actuator/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage(adminContextPath + "/login").successHandler(handler)
.and()
.logout().logoutUrl(adminContextPath + "/logout").and()
.httpBasic().and().csrf().disable();
}
}
}
3. Spring Boot Client 服务(配置到Eureka服务)
这里就配置Eureka Client 即可
pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wunaozai.client.demo</groupId>
<artifactId>spring-cloud-client-demo</artifactId>
<version>0.0.1</version>
<name>spring-cloud-client-demo</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.1.1</spring-boot-admin.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<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-client</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-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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> </project>
applicatoin.yml
server:
port: 18889
spring:
# boot:
# admin:
# client:
# url:
# - "http://127.0.0.1:8788"
# username: 'user'
# password: 'password'
application:
name: 子业务服务器 management:
endpoints:
web:
exposure:
include:
- "*"
endpoint:
health:
show-details: ALWAYS #eureka:
# client:
# fetch-registry: false
# register-with-eureka: false
eureka:
instance:
lease-renewal-interval-in-seconds: 10
health-check-url-path: /actuator/health
prefer-ip-address: true
client:
registry-fetch-interval-seconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://172.16.23.241:8761}/eureka/
Application.java
package com.wunaozai.client.demo; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableEurekaClient
@SpringBootApplication
public class SpringCloudClientDemoApplication { public static void main(String[] args) {
System.out.println("ok");
SpringApplication.run(SpringCloudClientDemoApplication.class, args);
} }
4. Spring Boot Client 服务(直接配置Admin地址)
如果不想通过Eureka,只是用Spring Boot Admin,可以只配置Admin的地址来实现。通过在application.yml 配置
spring:
boot:
admin:
client:
url:
- "http://127.0.0.1:8788"
username: 'user'
password: 'password'
5. 举个例子(基于docker-compose)
使用mvn package 把以上3个微服务打包成Docker镜像
Dockerfile 文件
FROM java:8
VOLUME /tmp ADD spring-cloud-admin-demo-0.0.1.jar app.jar
RUN bash -c 'touch /app.jar' EXPOSE 8788 ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
Build 构建镜像
docker build -t wunaozai/eureka:0.0.1 -f Dockerfile .
docker-compose.yml 文件
version: '3' services:
EUREKA1:
image: eureka:1
ports:
- 8761:8761
EUREKA2:
image: eureka:2
ports:
- 8762:8762
ADMIN:
image: admin:1
ports:
- 8788:8788
client-1:
image: client:1
ports:
- 18881:18888
client-2:
image: client:1
ports:
- 18882:18888
client-3:
image: client:1
ports:
- 18883:18888
client-4:
image: client:1
ports:
- 18884:18888
client-5:
image: client:1
ports:
- 18885:18888
client-6:
image: client:1
ports:
- 18886:18888
client-7:
image: client:1
ports:
- 18887:18888
client-8:
image: client:1
ports:
- 18888:18888
日志界面
WeaveScope 界面
Eureka 界面
Spring Boot Admin 界面
如果出现这个问题,请升级到最新的浏览器(Chrome) 一开始用Chrome 59,不行,升级到Chrome 71 才可以。
参考资料
https://blog.csdn.net/kinginblue/article/details/52132113
https://blog.csdn.net/hubo_88/article/details/80671192
本文地址: https://www.cnblogs.com/wunaozai/p/10313190.html
物联网架构成长之路(30)-Spring Boot Admin微服务WebUI监控的更多相关文章
- 物联网架构成长之路(22)-Docker练习之Etcd服务搭建
0. 前言 时隔多日,前段时间忙完一个可有可无的项目后,又进入摸鱼时间,没有办法,非互联网公司,就是闲得蛋疼.又开始了自学之路.以前入门过Docker,然后又很久没有看了,最近重新看了一下,推荐一下这 ...
- 物联网架构成长之路(23)-Docker练习之Elasticsearch服务搭建
0. 前言 最近基本都是学一些环境配置,和一些中间件的安装与配置.没有实际编写代码.可能看起来有点水,我对自己的学习方式是,先要了解各个中间件的安装配置以及简单使用,理论应用场景,然后我在小项目中,逐 ...
- 物联网架构成长之路(18)-接阿里云OSS服务
1.申请/购买OSS服务 在阿里云上申请/购买OSS服务, 然后在会得AccessKeyID,AccessKeySecret,bucketName 这三个东西 2.增删改查 在pom.xml文件上增加 ...
- 物联网架构成长之路(25)-Docker构建项目用到的镜像1
0. 前言 现在项目处于初级阶段,按照规划,先构建几个以后可能会用到的Image,并上传到阿里云的Docker仓库.以后博客中用到的Image,大部分都会用到这几个基础的Image,构建一个简单的物联 ...
- 物联网架构成长之路(24)-Docker练习之Compose容器编排
0.前言 一开始学的之后,是想一步到位直接上Kubernetes(K8s)的,后面没想到,好像有点复杂,有些概念不是很懂.因此学习东西还是要循序渐进,慢慢来.先了解单机编排技术Docker Compo ...
- 物联网架构成长之路(31)-EMQ基于HTTP权限验证
看过之前的文章就知道,我之前是通过搞插件,或者通过里面的MongoDB来进行EMQ的鉴权登录和权限验证.但是前段时间发现,还是通过HTTP WebHook 方式来调用鉴权接口比较适合实际使用.还是实现 ...
- 一文读懂 Spring Boot、微服务架构和大数据治理三者之间的故事
微服务架构 微服务的诞生并非偶然,它是在互联网高速发展,技术日新月异的变化以及传统架构无法适应快速变化等多重因素的推动下诞生的产物.互联网时代的产品通常有两类特点:需求变化快和用户群体庞大,在这种情况 ...
- Spring Boot、微服务架构和大数据
一文读懂 Spring Boot.微服务架构和大数据治理三者之间的故事 https://www.cnblogs.com/ityouknow/p/9034377.html 微服务架构 微服务的诞生并非偶 ...
- 一文读懂spring boot 和微服务的关系
欢迎访问网易云社区,了解更多网易技术产品运营经验. Spring Boot 和微服务没关系, Java 微服务治理框架普遍用的是 Spring Cloud. Spring Boot 产生的背景,是开发 ...
随机推荐
- Django 学习第七天——Django模型基础第二节
User 是自己创建的模型类,等于数据库中的表 常用的查询方法: all():获取所有数据: User.objects.all() first():获取第一条数据: User.objects.firs ...
- getting data from the keybroad
public static String getString() throws IOException{ InputStreamReader isr = new InoutStreamReader(S ...
- ASP.NET Core的Data Protect(数据保护)的学习和应用
转载请注入出处: https://home.cnblogs.com/u/zhiyong-ITNote/ dotnet core中提供了一个新的身份验证框架Identity,它不同于dot net下的身 ...
- mysql查询根据时间排序
表数据: mysql查询根据时间排序,如果有相同时间则只查询出来一个 所以需要再判断,如果时间相同,则根据id进行降序排序
- Python virtualenvwrapper在Win下的安装和管理
安装: 在Win下安装wrapper的时候,需要安装一个特殊的版本,即virtualenvwrapper-win.如果安装的只是virtualenvwrapper,则会出现mkvirtualenv,l ...
- go channel tips
一.只有一个goroutine时,读写阻塞的chan会出错(“fatal error: all goroutines are asleep - deadlock!”).包括未make的chan(cha ...
- docker自动重启容器
docker run --restart=always -d --name myunbuntu ubuntu /bin/bash -c "l am a docker" //无 ...
- DAG 上的动态规划(训练指南—大白书)
有向无环图(DAG,Directed Acyclic Graph)上的动态规划是学习动态规划的基础.很多问题都可以转化为DAG上的最长路.最短路或路径计数问题. 一.矩形嵌套 题目描述: ...
- 全局解释器锁 GIL
1.什么是GIL? GIL本质上是互斥锁,可以将并发运行变为串行,以此来控制同一时间内共享数据只能被一个任务修改,保证时间安全 2.GIL应用场景 使用原因:Cpython解释器自带垃圾回收机制不是线 ...
- mongodb 索引,全文索引与唯一索引
唯一索引创建: db.createIndex({name: 1}, {unique: true})