环境的搭建

首先搭建eruka的注册中心

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.</modelVersion>
<groupId>com.tuling.cloud</groupId>
<artifactId>microservice-eureka-server</artifactId>
<version>0.0.-SNAPSHOT</version>
<packaging>jar</packaging> <!-- 引入spring boot的依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1..RELEASE</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- spring boot admin监控客户端依赖 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.1.</version>
</dependency>
</dependencies> <!-- 引入spring cloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <!-- 添加spring-boot的maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

application.yml

server:
port: # 指定该Eureka实例的端口
eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/ # 参考文档:http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html#_standalone_mode
# 参考文档:http://my.oschina.net/buwei/blog/618756

EurekaApplication启动类

package com.tuling.cloud.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /**
* 使用Eureka做服务发现.
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}

接下来我们来看admin sererhttps://blog.csdn.net/a1036645146/article/details/97249153

参考博客:https://blog.csdn.net/a1036645146/article/details/97249153

Spring Boot Admin是一个社区项目,用于管理和监视基于SpringBoot的应用程序。客户端应用可以通过Spring Boot Admin Client或者注册中心就可以注册到Spring Boot Admin服务端进行监控。Spring Boot Admin 是在 Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI。2.X版本使用Vue.js重写了UI界面,简洁。

2.3 修改application.yml配置文件,端口可以自己修改: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.tuling.cloud</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.4</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <!-- eureka注册中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

2.2 启动类添加相关注解,@EnableAdminServer:

package com.tuling.cloud.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration; import de.codecentric.boot.admin.server.config.EnableAdminServer; @Configuration
@EnableAutoConfiguration
@EnableAdminServer
@EnableDiscoveryClient
public class SpringBootAdminApplication { public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}

2.3 修改application.yml配置文件,端口可以自己修改:

server:
port: 6666
spring:
application:
name: SpringBootAdmin
boot:
admin:
ui:
title: SpringBootAdmin-Server # eureka注册中心
#eureka:
# client:
# serviceUrl:
# defaultZone: http://localhost:1001/eureka/ #Actuator配置:暴露敏感路径,默认情况下,敏感路径并不暴露
management:
endpoints:
web:
exposure:
# 暴露xxx端点,如需暴露多个,用,分隔;如需暴露所有端点,用'*'
include: "*"
endpoint:
health:
# 是否展示健康检查详情
show-details: ALWAYS
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true

2.4 启动服务端应用:启动先启动注册中心,然后在启动admin server

admin Server应用已经把自己注册上到了注册中心,同时也监控了自己

接下来,我们要让admin server监控其他客户端应用, Spring Boot Admin Client 客户端搭建 同样创建一个Springboot 应用,基于2.1.4版本,pom依赖如下:

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.tuling.cloud</groupId>
<artifactId>microservice-provider-user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <!-- 引入spring boot的依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> <dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> <!-- spring boot admin监控客户端依赖 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.1.4</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-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency> </dependencies> <!-- 引入spring cloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <!-- 添加spring-boot的maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

application.yml如下

server:
port: 8888 spring:
boot:
admin:
client:
# server地址
url: http://localhost:6666
instance:
#client地址
service-base-url: http://localhost:${server.port}
jpa:
generate-ddl: false
show-sql: true
hibernate:
ddl-auto: none
datasource: # 指定数据源
platform: h2 # 指定数据源类型
schema: classpath:schema.sql # 指定h2数据库的建表脚本
data: classpath:data.sql # 指定h2数据库的数据脚本
application:
name: SpringBootAdmin-Client
main:
allow-bean-definition-overriding: true
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true #Actuator配置:暴露敏感路径,默认情况下,敏感路径并不暴露
management:
endpoints:
web:
exposure:
# 暴露xxx端点,如需暴露多个,用,分隔;如需暴露所有端点,用'*'
include: "*"
endpoint:
health:
# 是否展示健康检查详情
show-details: ALWAYS # info信息会显示到SpringBootAdmin的server端,这里取的是pom文件中的数据
info:
version: @project.version@
groupId: @project.groupId@
artifactId: @project.artifactId@

这里需要特别需要特别强调的是,因为admin server是注册在了注册中心上面,admin server 的客户端如果也注册到了注册中心上面, -admin-starter-client会从注册中心获得admin Server的地址,所以上面的

不要在手动指定admin server的地址,这里写成下面的形式

server:
port: 4444 spring:
jpa:
generate-ddl: false
show-sql: true
hibernate:
ddl-auto: none
datasource: # 指定数据源
platform: h2 # 指定数据源类型
schema: classpath:schema.sql # 指定h2数据库的建表脚本
data: classpath:data.sql # 指定h2数据库的数据脚本
application:
name: user
main:
allow-bean-definition-overriding: true
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true #Actuator配置:暴露敏感路径,默认情况下,敏感路径并不暴露
management:
endpoints:
web:
exposure:
# 暴露xxx端点,如需暴露多个,用,分隔;如需暴露所有端点,用'*'
include: "*"
endpoint:
health:
# 是否展示健康检查详情
show-details: ALWAYS # info信息会显示到SpringBootAdmin的server端,这里取的是pom文件中的数据
info:
version: @project.version@
groupId: @project.groupId@
artifactId: @project.artifactId@

data.sql

insert into user (id, username, name, age, balance) values (1, 'account1', '张三', 20, 100.00);
insert into user (id, username, name, age, balance) values (2, 'account2', '李四', 28, 180.00);
insert into user (id, username, name, age, balance) values (3, 'account3', '王五', 32, 280.00);

schema.sql

drop table user if exists;
create table user (id bigint generated by default as identity, username varchar(40), name varchar(20), age int(3), balance decimal(10,2), primary key (id));

jpa访问h2数据库

package com.tuling.cloud.study.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import com.tuling.cloud.study.entity.User; @Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
package com.tuling.cloud.study.entity;

import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; @Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String username;
@Column
private String name;
@Column
private Integer age;
@Column
private BigDecimal balance; public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} public String getUsername() {
return this.username;
} public void setUsername(String username) {
this.username = username;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return this.age;
} public void setAge(Integer age) {
this.age = age;
} public BigDecimal getBalance() {
return this.balance;
} public void setBalance(BigDecimal balance) {
this.balance = balance;
} }
package com.tuling.cloud.study.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import com.tuling.cloud.study.entity.User;
import com.tuling.cloud.study.repository.UserRepository; @RestController
public class UserController { @Autowired
private UserRepository userRepository; @Autowired
private Registration registration; @GetMapping("/{id}")
public User findById(@PathVariable Long id) throws Exception {
Thread.sleep(3000);
User findOne = userRepository.findById(id).get();
return findOne;
} @GetMapping("/getIpAndPort")
public String findById() {
return registration.getHost() + ":" + registration.getPort();
} @GetMapping("/testException")
public User testException() throws Exception {
Thread.sleep(2000);
throw new RuntimeException("userService throw new RuntimeException ");
} @RequestMapping(value = "/aa", method = RequestMethod.GET)
public String aa(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "aa is sb";
}
}

启动类的代码

package com.tuling.cloud.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient
@SpringBootApplication
public class ProviderUserApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderUserApplication.class, args);
}
}

3.3 启动客户端应用,显示已注册上,选项内容挺丰富的,具体自己运行去看下:

Spring Boot Admin 2.1.4最新实战教程的更多相关文章

  1. 使用 Spring Boot Admin 监控应用状态

    程序员优雅哥 SpringBoot 2.7 实战基础 - 11 - 使用 Spring Boot Admin 监控应用状态 1 Spring Boot Actuator Spring Boot Act ...

  2. spring boot admin + spring boot actuator + erueka 微服务监控

    关于spring boot actuator简单使用,请看 简单的spring boot actuator 使用,点击这里 spring boot admin 最新的正式版本是1.5.3 与 spri ...

  3. Spring boot admin 节点状态一直为DOWN的排查

    项目中需要监控各个微服务节点的健康状态,找到了spring boot admin这个全家桶监控工具,它其实是Vue.js美化过的Spring Boot Actuator,官方的解释是: codecen ...

  4. 物联网架构成长之路(30)-Spring Boot Admin微服务WebUI监控

    0. 前言 一个完整的微服务解决方案包含了许多微服务,基于我们需要观察各个微服务的运行状态,因此Spring Boot 生态提供了Spring Boot Admin 这个组件来实现微服务管理WEB U ...

  5. Springboot监控之二:Spring Boot Admin对Springboot服务进行监控

    概述 Spring Boot 监控核心是 spring-boot-starter-actuator 依赖,增加依赖后, Spring Boot 会默认配置一些通用的监控,比如 jvm 监控.类加载.健 ...

  6. spring boot 2.0.3+spring cloud (Finchley)8、微服务监控Spring Boot Admin

    参考:Spring Boot Admin 2.0 上手 Spring Boot Admin 用于管理和监控一个或多个Spring Boot程序,在 Spring Boot Actuator 的基础上提 ...

  7. Spring Boot (十): Spring Boot Admin 监控 Spring Boot 应用

    Spring Boot (十): Spring Boot Admin 监控 Spring Boot 应用 1. 引言 在上一篇文章<Spring Boot (九): 微服务应用监控 Spring ...

  8. Spring Boot 2.X(十七):应用监控之 Spring Boot Admin 使用及配置

    Admin 简介 Spring Boot Admin 是 Spring Boot 应用程序运行状态监控和管理的后台界面.最新UI使用vue.js重写里. Spring Boot Admin 为已注册的 ...

  9. Spring Boot Admin 详解(Spring Boot 2.0,基于 Eureka 的实现)

    原文:https://blog.csdn.net/hubo_88/article/details/80671192 Spring Boot Admin 用于监控基于 Spring Boot 的应用,它 ...

随机推荐

  1. [PHP学习教程 - 网络]002.获取网页内容(URL Content)

    引言:获取网页内容是我们实现网页操作的基本之基本,今天这一讲,我们和大家讲一下基本请求网页内容的几种方法. 我们似乎每天都要做这样一件事情,打开一个浏览器,输入网址,回车,一个空白的页面顿时有了东西, ...

  2. 使用 fileupload 组件完成文件的上传应用

    1. 使用 fileupload 组件完成文件的上传应用 commons-dbutils-1.3.jarcommons-fileupload-1.2.1.jar 1). 需求: > 在 uplo ...

  3. Druid数据库连接池的使用

    Druid  阿里提供的数据库连接池,集以上连接池优点于一身,开发使用此连接池 使用配置文件方式获取Druid数据库连接池 TestDruid package com.aff.connection; ...

  4. ES6-json与字符串的转换

    1.ES5下的json 1.1 基本概念 是对象 简写形式,名字跟值(key和value)一样,留一个就行 方法 :function一块删 即show:function(){...}等价于show() ...

  5. 【Hadoop】mapreduce环形缓冲区

    mapreduce过程解析 数据在map中怎么写入磁盘? 数据:经过map逻辑处理过后的数据(key,value)- 磁盘:本地磁盘 环形缓冲区 1.为什么要环形缓冲区? 答:使用环形缓冲区,便于写入 ...

  6. Linux (八)服务

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 1.服务的概念 操作系统中在后台持续运行的程序,本身并没有操作界面,需要通过端口号访问和操作.CentO ...

  7. Java实现 LeetCode 724 寻找数组的中心索引(暴力)

    724. 寻找数组的中心索引 给定一个整数类型的数组 nums,请编写一个能够返回数组"中心索引"的方法. 我们是这样定义数组中心索引的:数组中心索引的左侧所有元素相加的和等于右侧 ...

  8. Java实现 蓝桥杯 算法提高 7-1用宏求球的体积

    算法提高 7-1用宏求球的体积 时间限制:1.0s 内存限制:256.0MB 问题描述 使用宏实现计算球体体积的功能.用户输入半径,系统输出体积.不能使用函数,pi=3.1415926,结果精确到小数 ...

  9. Java实现 计蒜客 1251 仙岛求药

    仙岛求药 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难来到岛的中心,发现仙药摆在了迷阵的深处.迷阵由 M \times NM× ...

  10. Java实现DFS深度优先查找

    1 问题描述 深度优先查找(depth-first search,DFS)可以从任意顶点开始访问图的顶点,然后把该顶点标记为已访问.在每次迭代的时候,该算法紧接着处理与当前顶点邻接的未访问顶点.这个过 ...