前端Springboot学习指南!!!(五)

SpringBoot学习指南,超级全,真的细!挑战全篇无图教会你Java Springboot的使用。

考虑到大家学习的时间都比较碎片前面的9万字的文档确实阅读时间相对比较苛刻,故此将文档做一个分割,如果时间比较充足的小伙伴,建议还是直接阅读长篇这样学习会比较连贯
juejin.cn/post/730931...

5.添加Spring Boot Admin Client组件
1.这里的依赖是需要添加到我们被监控的服务当中的所以就会修改我们之前的项目的POM文件;

注意项目是

com.practice

springboot_practice

别添加错了!!!!

别添加错了!!!

别添加错了!!

重要的事情说三遍!!!!!

添加依赖:

xml 复制代码
        <!-- Spring Boot Admin Client 在POM文件中添加Client的依赖注意要和Server的依赖配套:-->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.7.13</version>
        </dependency>

在POM文件中添加Client的依赖注意要和Server的依赖配套,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.practice</groupId>
    <artifactId>springboot_04mvc</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- 所有的Spring boot项目都要继承这个父工程,父工程对所有的jar包进行管理 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.8</version>
    </parent>
​
    <!-- 依赖 -->
    <dependencies>
        <!-- 单元测试启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
​
        <!-- 通用mapper启动器 -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version> <!-- 使用最新版本 -->
        </dependency>
​
        <!-- JDBC启动器spring-boot-starter-jdbc 是 Spring Boot 中用于简化 JDBC 开发的启动器。它提供了 JDBC 相关的基础配置,让开发者能够更轻松地使用 Spring JDBC 访问数据库。 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
​
        <!-- mysql驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.23</version> <!-- 使用最新版本 -->
        </dependency>
​
        <!-- druid启动器 数据库连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.17</version> <!-- 使用最新版本 -->
        </dependency>
​
        <!-- Web启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
​
        <!-- spring boot Actuator依赖  spring-boot-starter-actuator 是 Spring Boot 提供的一个用于生产环境监控和管理的模块。它为 Spring Boot 应用程序提供了一组内置的、用于监视和管理应用程序的端点(endpoints)。这些端点包括健康检查、信息展示、环境属性、配置信息、日志等,允许你监控应用程序的运行状况、收集信息和进行一些管理操作。-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
​
        <!-- 编码工具包 common3-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version> <!-- 使用最新版本 -->
        </dependency>
​
        <!-- 热部署 spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- Redis 启动器依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--Lombok的主要作用是通过简化常见的Java代码模式,减少样板代码的编写,提高开发效率,
       减少代码错误,增加代码的可读性和可维护性。它已经成为许多Java开发人员的常用工具之一,
       并在许多开源项目和企业应用中广泛使用。-->
​
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version> <!-- 请检查并使用最新的版本 -->
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot Admin Client 在POM文件中添加Client的依赖注意要和Server的依赖配套:-->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.7.13</version>
        </dependency>
​
    </dependencies>
    <!-- 打包成jar   -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
​
2.这里配置是需要添加到我们被监控的服务当中的所以就会修改我们之前的项目的application.yml文件;

注意项目是

com.practice

springboot_practice

别添加错了!!!!

别添加错了!!!

别添加错了!!

重要的事情说三遍!!!!!

添加以下配置:

yaml 复制代码
  # Spring Boot Admin Client
  application:
    name: my-Client #名称随便起的
  boot:
    admin:
      client:
        url: http://localhost:8888 # 指定注册的地址,地址指向 Spring Boot 

修改 src/main/resources/application.yml

yaml 复制代码
# 服务器端口配置
server:
  port: 9090
​
# Spring 数据源配置
spring:
  datasource:
    # 数据库连接 URL
    url: jdbc:mysql://127.0.0.1:3306/springboot
    # MySQL 驱动类
    driver-class-name: com.mysql.cj.jdbc.Driver
    # 数据库用户名
    username: root
    # 数据库密码
    password:
    # 数据源类型(这里使用了 Druid 数据源)
    type: com.alibaba.druid.pool.DruidDataSource
  redis:
    host: 127.0.0.1
    port: 6379
    password:
  # 配置Actuator的安全性
  security:
    user:
      name: admin # 设置Actuator的用户名
      password: password # 设置Actuator的密码
  # Spring Boot Admin Client
  application:
    name: my-Client #名称随便起的
  boot:
    admin:
      client:
        url: http://localhost:8888 # 指定注册的地址,地址指向 Spring Boot Admin Server地址
​
​

至此重新启动springboot_practice项目和`springboot_admin_server项目

通过Chrome访问http://localhost:8888即可访问Springboot Admin Client,你将会看到springboot_practice

指标以及一些运行信息被注册到Springboot Admin Server的信息。

说明:

这里设置了应用程序的名称为 "my-Client"。这个名称是在 Spring Boot Admin 服务器中用于标识该应用程序的唯一标识符。你可以根据自己的需求来设置应用程序的名称。

这里指定了 Spring Boot Admin 服务器的地址。通过将应用程序注册到指定的 URL,应用程序就可以与 Spring Boot Admin 服务器建立连接,并向服务器发送应用程序的信息和指标数据。

在这个示例中,URL被设置为 http://localhost:8888,这意味着 Spring Boot Admin 服务器应该在本地主机上的 8888 端口运行。你需要根据实际情况将该 URL 替换为你的 Spring Boot Admin 服务器的地址。

通过使用这个配置,你的 Spring Boot 应用程序就可以将自身注册到 Spring Boot Admin 服务器,并将其监控和管理功能暴露给 Spring Boot Admin 服务器。这样,你就可以在 Spring Boot Admin 服务器的管理界面上查看和监控你的应用程序的状态、指标数据和日志信息,以便更好地管理和调试应用程序。

请注意,为了使这个配置生效,你需要在你的 Spring Boot 应用程序的类路径中包含适当的依赖项,以支持 Spring Boot Admin 客户端功能。

相关推荐
程序员黑豆1 小时前
鸿蒙应用开发:V1与V2版本数据持久化实战教程
前端·harmonyos
小月土星5 小时前
React Router 前端路由深度解析:从传统后端路由到现代 SPA 的完整演进
前端
程序员黑豆5 小时前
鸿蒙开发 Navigation 路由教程:从入门到实战
前端·harmonyos
weixin_440784116 小时前
【HandlerThread实现原理】
android·java·开发语言
leoZ2316 小时前
本地跑大模型实战(七):llama.cpp 性能调优,让推理更快更省
java·人工智能·spring·生成对抗网络·语言模型·自然语言处理·llama
有梦不弃6 小时前
模块化单体架构设计方案:DDD + 六边形架构落地实践
后端·架构
mubei-1237 小时前
SpringDAO的用法
java·开发语言·数据库
happymagic7 小时前
java spring boot做的jar包程序,如何实现自动运行启动
java·运维·服务器·spring boot·jar
用户938515635078 小时前
从零理解 React Router v6:每一个 API 都是怎么工作的
前端·javascript·全栈
用户0595401744610 小时前
Redis缓存回滚踩坑实录:一个配置失误,让我在凌晨3点丢了3000条数据
前端·css