micrometer_具有InlfuxDB的Spring Boot和Micrometer第3部分:Servlet和JDBC

micrometer

在上一个博客中,我们使用由InfluxDB支持的Micrometer设置了React式应用程序。

在本教程中,我们将使用传统的带有JDBC的基于Servlet的阻塞Spring堆栈。 我选择的数据库是postgresql。 我将使用与先前博客文章相同的脚本。

因此,我们将拥有初始化数据库的脚本

 #!/bin/bash  set -e  psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL create schema spring_data_jpa_example; create table spring_data_jpa_example.employee( id SERIAL PRIMARY KEY , firstname  TEXT NOT NULL , lastname   TEXT NOT NULL , email      TEXT not null , age INT NOT NULL , salary real , unique (email) ); insert into spring_data_jpa_example.employee (firstname,lastname,email,age,salary) values ( 'John' , 'Doe 1' , 'john1@doe.com' ,18,1234.23); insert into spring_data_jpa_example.employee (firstname,lastname,email,age,salary) values ( 'John' , 'Doe 2' , 'john2@doe.com' ,19,2234.23); insert into spring_data_jpa_example.employee (firstname,lastname,email,age,salary) values ( 'John' , 'Doe 3' , 'john3@doe.com' ,20,3234.23); insert into spring_data_jpa_example.employee (firstname,lastname,email,age,salary) values ( 'John' , 'Doe 4' , 'john4@doe.com' ,21,4234.23); insert into spring_data_jpa_example.employee (firstname,lastname,email,age,salary) values ( 'John' , 'Doe 5' , 'john5@doe.com' ,22,5234.23);  EOSQL 

我们将拥有一个包含InfluxDB,Postgres和Grafana的docker compose文件。

 version: '3.5'  services: influxdb: image: influxdb restart: always ports: - 8086 : 8086 grafana: image: grafana / grafana restart: always ports: - 3000 : 3000 postgres: image: postgres restart: always environment: POSTGRES_USER: db - user POSTGRES_PASSWORD: your - password POSTGRES_DB: postgres ports: - 5432 : 5432 volumes: - $PWD / init - db - script.sh: / docker - entrypoint - initdb.d / init - db - script.sh 

现在是时候从我们的maven依赖关系开始构建我们的spring应用程序了。

 <? 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.2.4.RELEASE</ version > </ parent > < groupId >com.gkatzioura</ groupId > < artifactId >EmployeeApi</ artifactId > < version >1.0-SNAPSHOT</ version > < build > < defaultGoal >spring-boot:run</ defaultGoal > < plugins > < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-compiler-plugin</ artifactId > < configuration > < source >8</ source > < target >8</ target > </ configuration > </ plugin > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > </ plugin > </ plugins > </ build > < 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 >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-actuator</ artifactId > </ dependency > < dependency > < groupId >org.postgresql</ groupId > < artifactId >postgresql</ artifactId > < version >42.2.8</ version > </ dependency > < dependency > < groupId >io.micrometer</ groupId > < artifactId >micrometer-core</ artifactId > < version >1.3.2</ version > </ dependency > < dependency > < groupId >io.micrometer</ groupId > < artifactId >micrometer-registry-influx</ artifactId > < version >1.3.2</ version > </ dependency > < dependency > < groupId >org.projectlombok</ groupId > < artifactId >lombok</ artifactId > < version >1.18.12</ version > < scope >provided</ scope > </ dependency > </ dependencies >  </ project > 

由于这是JDBC支持的依赖关系,因此我们将创建实体和存储库。

 package com.gkatzioura.employee.model;  import javax.persistence.Column;  import javax.persistence.Entity;  import javax.persistence.GeneratedValue;  import javax.persistence.GenerationType;  import javax.persistence.Id;  import javax.persistence.Table;  import lombok.Data;  @Data  @Entity  @Table (name = "employee" , schema= "spring_data_jpa_example" )  public class Employee { @Id @Column (name = "id" ) @GeneratedValue (strategy = GenerationType.IDENTITY) private Long id; @Column (name = "firstname" ) private String firstName; @Column (name = "lastname" ) private String lastname; @Column (name = "email" ) private String email; @Column (name = "age" ) private Integer age; @Column (name = "salary" ) private Integer salary;  } 

然后让我们添加存储库

 package com.gkatzioura.employee.repository;  import com.gkatzioura.employee.model.Employee;  import org.springframework.data.jpa.repository.JpaRepository;  public interface EmployeeRepository extends JpaRepository<Employee,Long> {  } 

和控制器

 package com.gkatzioura.employee.controller;  import java.util.List;  import com.gkatzioura.employee.model.Employee;  import com.gkatzioura.employee.repository.EmployeeRepository;  import org.springframework.web.bind.annotation.RequestMapping;  import org.springframework.web.bind.annotation.RestController;  @RestController  public class EmployeeController { private final EmployeeRepository employeeRepository; public EmployeeController(EmployeeRepository employeeRepository) { this .employeeRepository = employeeRepository; } @RequestMapping ( "/employee" ) public List<Employee> getEmployees() { return employeeRepository.findAll(); }  } 

最后但并非最不重要的Application类

 package com.gkatzioura.employee;  import org.springframework.boot.SpringApplication;  import org.springframework.boot.autoconfigure.SpringBootApplication;  @SpringBootApplication  public class Application { public static void main(String[] args) { SpringApplication.run(Application. class , args); }  } 

以及配置

 spring: datasource: platform: postgres driverClassName: org.postgresql.Driver username: db - user password: your - password url: jdbc:postgresql: / / 127.0 . 0.1 : 5432 / postgres  management: metrics: export: influx: enabled: true db: employeeapi uri: http: / / 127.0 . 0.1 : 8086 endpoints: web: expose: "*" 

试试看

 curl http: //localhost :8080 /employee 

经过一些请求后,我们可以找到保留的条目。

 docker exec -it influxdb- local influx  > SHOW DATABASES;  name: databases  name  ----  _internal  employeeapi  > use employeeapi  Using database employeeapi  > SHOW MEASUREMENTS  name: measurements  name  ----  hikaricp_connections  hikaricp_connections_acquire  hikaricp_connections_active  hikaricp_connections_creation  hikaricp_connections_idle  hikaricp_connections_max  hikaricp_connections_min  hikaricp_connections_pending  hikaricp_connections_timeout  hikaricp_connections_usage  http_server_requests  jdbc_connections_active  jdbc_connections_idle  jdbc_connections_max  jdbc_connections_min  jvm_buffer_count  jvm_buffer_memory_used  jvm_buffer_total_capacity  jvm_classes_loaded  jvm_classes_unloaded  jvm_gc_live_data_size  jvm_gc_max_data_size  jvm_gc_memory_allocated  jvm_gc_memory_promoted  jvm_gc_pause  jvm_memory_committed  jvm_memory_max  jvm_memory_used  jvm_threads_daemon  jvm_threads_live  jvm_threads_peak  jvm_threads_states  logback_events  process_cpu_usage  process_files_max  process_files_open  process_start_time  process_uptime  system_cpu_count  system_cpu_usage  system_load_average_1m  tomcat_sessions_active_current  tomcat_sessions_active_max  tomcat_sessions_alive_max  tomcat_sessions_created  tomcat_sessions_expired  tomcat_sessions_rejected 

如您所见,指标与上一个示例有些不同。 我们有jdbc连接度量标准tomcat度量标准和与我们的应用程序相关的所有度量标准。 您可以在github上找到源代码。

翻译自: https://www.javacodegeeks.com/2020/03/spring-boot-and-micrometer-with-inlfuxdb-part-3-servlets-and-jdbc.html

micrometer

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/331220.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

漫画算法:辗转相除法是什么鬼

转载自 玻璃猫 程序员小灰 大四毕业前夕&#xff0c;计算机学院的小灰又一次顶着炎炎烈日&#xff0c; 去某IT公司面试研发工程师岗位…… 半小时后&#xff0c;公司会议室&#xff0c;面试开始…… 小灰奋笔疾书&#xff0c;五分钟后…… 小灰的思路十分简单。他使用暴力…

tomcat(9)Session管理

【0】README0.0&#xff09;本文部分描述转自“深入剖析tomcat”&#xff0c;旨在学习“tomcat-Session管理” 的基础知识&#xff1b;0.1&#xff09;Catalina通过一个称为Session 管理器的组件来管理建立的Session对象&#xff0c;该组件由org.apache.catalina.Manager接口来…

micrometer_具有InlfuxDB的Spring Boot和Micrometer第2部分:添加InfluxDB

micrometer自从我们添加了基本应用程序以来&#xff0c;是时候启动InfluxDB实例了。 我们将按照之前的教程进行操作&#xff0c;并添加一个docker实例。 docker run –rm -p 8086&#xff1a;8086 –name influxdb-本地influxdb 是时候在我们的pom上添加微米InfluxDB依赖项了…

漫画:什么是volatile关键字?(整合版)

转载自 永远爱大家的 程序员小灰 ————— 第二天 ————— ———————————— Java内存模型简称JMM&#xff08;Java Memory Model&#xff09;&#xff0c;是Java虚拟机所定义的一种抽象规范&#xff0c;用来屏蔽不同硬件和操作系统的内存访问差异&#xff0c;让j…

tomcat(supplement)HttpConnector.initialize() 和 start() 方法 以及 StandardContext.start()方法的分析

【0】README 0.0&#xff09;本文中源代码的背景&#xff0c;参见 tomcat(9)session管理 0.1&#xff09;本文主要以图片的形式分析他们大致的调用过程&#xff1b; 0.2&#xff09;HttpConnector org.apache.catalina.connector.http.HttpConnector; 而StandardContext o…

restful rest_HATEOAS的RESTful服务。 超媒体:REST的秘密要素

restful rest在这篇文章中&#xff0c;我们将介绍有关HATEOAS的RESTful服务的综合文章。 超媒体是REST的秘密成分。 1.简介 在本教程的前一部分中&#xff0c;我们花了一些时间来刷新有关REST体系结构样式的基本原理的知识。 业界对REST状态的批判性眼光揭示了一个令人失望的…

漫画:什么是单例设计模式

转载自 永远爱大家的 程序员小灰 ————— 第二天 ————— 单例模式第一版&#xff1a; 1234567891011public class Singleton {private Singleton() {} //私有构造函数private static Singleton instance null; //单例对象//静态工厂方法public static Singleton ge…

如何在工作繁重、睡眠较少的情况下保持旺盛精力?

作者&#xff1a;陈炬 链接&#xff1a;https://www.zhihu.com/question/23177623/answer/47785761 来源&#xff1a;知乎 著作权归作者所有。商业转载请联系作者获得授权&#xff0c;非商业转载请注明出处。 本人也在创业&#xff0c;结合《精力管理》一书&#xff0c;说说我…

mockito接口没法赋值_Mockito:无法实例化@InjectMocks字段:类型是接口

mockito接口没法赋值使用Mockito进行Java类的模拟和存根的任何人&#xff0c;可能都熟悉InjectMocks -annotation。 在要测试的类上使用此批注&#xff0c;Mockito将尝试通过构造函数注入&#xff0c;setter注入或属性注入来注入模拟。 魔术成功了&#xff0c;它无声地失败了&a…

tomcat(10)安全性

【0】README0.0&#xff09;本文部分描述转自“深入剖析tomcat”&#xff0c;旨在学习 tomcat(10)安全性 的基本知识&#xff1b;0.1&#xff09;servlet技术支持通过配置部署描述器&#xff08;web.xml&#xff09;文件来对这些内容进行访问控制&#xff1b;&#xff08;干货—…

SonarQube 8.3.x中的Maven项目的测试覆盖率报告

几年前&#xff0c;我写了一篇博客文章&#xff0c;介绍如何在SonarQube中生成测试报告&#xff0c;该报告独立于单元测试和集成测试的测试报告中。 从SonarQube 6.2开始&#xff0c;测试报告不再在这些类别中分开&#xff08;请参阅SonarQube的博客文章 &#xff09;。 SonarQ…

单例模式懒汉、饿汉和登记

转载自 JAVA设计模式之单例模式本文继续介绍23种设计模式系列之单例模式。 概念&#xff1a;  java中单例模式是一种常见的设计模式&#xff0c;单例模式的写法有好几种&#xff0c;这里主要介绍三种&#xff1a;懒汉式单例、饿汉式单例、登记式单例。  单例模式有以下特点…

量角器中Selenium定位器的完整指南(示例)

在测试网站的功能时&#xff0c;特别是Web元素&#xff08;例如单选按钮&#xff0c;文本框&#xff0c;下拉列表等&#xff09;&#xff0c;您需要确保能够访问这些元素。 Selenium定位器正是出于这个目的&#xff0c;通过使用此命令&#xff0c;我们可以识别这些Web元素DOM&a…

MySQL的自然联结+外部联结(左外连接,右外连接)+内部联结

【0】README0.1&#xff09;本文旨在review MySQL的自然联结外部联结&#xff08;左外连接&#xff0c;右外连接&#xff09;内部联结 的相关知识&#xff1b;【1】自然联结1&#xff09;自然联结定义&#xff1a;无论何时对表进行联结&#xff0c;应该至少有一个列出现不止一个…

MySQL 添加列+修改列+删除列

【0】REAMDE 0.1&#xff09;本文部分文字描述转自 http://blog.163.com/zhangjie_0303/blog/static/99082706201191911653778/ 0.2&#xff09;本文旨在review mysql 对列的相关操作&#xff1a;如添加&#xff0c;修改&#xff0c;删除以及重命名表名等操作&#xff1b; 【1】…

compose应用_带有PostgreSQLDocker Compose for Spring Boot应用程序

compose应用在此博客文章中&#xff0c;您将学习如何使用PostgreSQL配置Spring Boot应用程序以与Docker Compose一起运行。 这篇博客文章涵盖&#xff1a; Spring Boot应用程序Dockerfile配置&#xff0c;在依赖项和资源之间进行了清晰的分离 用于通过PostgreSQL运行应用程序…

单例模式面试题

转载自 单例模式面试题&#xff08;特点、理解&#xff09; (1)单例模式特点&#xff08;什么是单例模式&#xff09;&#xff1f;  a.单例类只能有一个实例。  b.单例类必须自己创建自己的唯一实例。  c.单例类必须给所有其他对象提供这一实例。 (2)单例模式的作用&#x…

MySQL的source命令不加分号和delimiter的使用

【0】README 0.1&#xff09;本文旨在 review source 命令&#xff0c; 这一直是我的痛&#xff0c;为什么一直导入 sql 文件不成功&#xff0c;一直没有写 blog 吧他 记录下来&#xff08;事实上&#xff0c;也间接证明我就是个小白&#xff09;&#xff1b; 0.2&#xff09…

selenium自动化测试_维持Selenium测试自动化的完美方法

selenium自动化测试毫无疑问&#xff0c; 自动浏览器测试已改变了软件开发的工作方式。 如果不是Selenium&#xff0c;我们将无法像我们一样使用各种各样的无错误的Web应用程序。 但是有时&#xff0c;甚至IT部门也误解了自动化一词。 大多数人认为计算机将为他们完成所有测试…

单例模式的优与劣

转载自 大话设计模式(四)单例模式的优与劣前言首先来明确一个问题&#xff0c;那就是在某些情况下&#xff0c;有些对象&#xff0c;我们只需要一个就可以了&#xff0c;比如&#xff0c;一台计算机上可以连好几个打印机&#xff0c;但是这个计算机上的打印程序只能有一个&…