其他分享
首页 > 其他分享> > |NO.Z.00343|——————————|CloudNative|——|KuberNetes&运维.V60|--------------------------------------------

|NO.Z.00343|——————————|CloudNative|——|KuberNetes&运维.V60|--------------------------------------------

作者:互联网



[CloudNative:KuberNetes&运维.V60]                                                                      [Applications.KuberNetes][|DevOps|k8s|k8s运维|**3节点.V1**|Micrometer|Prometheus监控JVM|服务发现|]








一、使用Micrometer.Prometheus监控JVM项目
### --- 下载Java.Demo
~~~     下载Java.Demo实验环境包

[root@k8s-master01 Jvm.Micrometer]# git clone https://github.com/gongchangwangpi/spring-cloud-demo2.git
Cloning into 'spring-cloud-demo2'...
remote: Enumerating objects: 161, done.
remote: Counting objects: 100% (161/161), done.
remote: Compressing objects: 100% (94/94), done.
remote: Total 161 (delta 33), reused 148 (delta 26), pack-reused 0
Receiving objects: 100% (161/161), 28.65 KiB | 27.00 KiB/s, done.
Resolving deltas: 100% (33/33), done.
### --- 进入安装目录下

[root@k8s-master01 Jvm.Micrometer]# cd spring-cloud-demo2/
[root@k8s-master01 spring-cloud-demo2]# ls
LICENSE  
pom.xml  
README.md  
spring-cloud-commons  
spring-cloud-eureka  
spring-cloud-gateway  
spring-cloud-nacos-discovery  
spring-cloud-user
二、启动一个maven容器
### --- 下载maven:3.5.3版本包:镜像
~~~     maven镜像地址:https://hub.docker.com/_/maven?tab=tags

[root@k8s-master01 ~]# docker pull maven:3.5.3
Unable to find image 'maven:3.5.3' locally
3.5.3: Pulling from library/maven
cc1a78bfd46b: Pull complete 
d2c05365ee2a: Pull complete 
231cb0e216d3: Pull complete 
3d2aa70286b8: Pull complete 
276ddd61a47b: Pull complete 
2c60f2a0b551: Pull complete 
4abbe5585c9b: Pull complete 
aaeb39f0b473: Pull complete 
a1ec96bfaf16: Pull complete 
16d38ec4eb12: Pull complete 
ff6759072b35: Pull complete 
37a68c3b1fc7: Pull complete 
Digest: sha256:84a5c048ffff662571e3f652a8e884a9b8510f05cf4dcce0422534ce50e04cdd
Status: Downloaded newer image for maven:3.5.3
### --- 起一个maven的容器

[root@k8s-master01 spring-cloud-demo2]# docker run -ti --rm -v /opt/m2:/root/.m2 -v `pwd`:/opt/ -p 18761:8761 maven:3.5.3 bash
root@da23a029c0d6:/# 
~~~     # 注:   
--rm -v /opt/m2:/root/.m2   # 把/opt/m2挂载到/root/.m2
-v 'pwd':/opt/              # 把当前目录挂载到它的工作目录下;pwd左右是反撇号
-p 18761:8761               # 映射一个端口,Euraka的默认端口是8760,可以直接把它映射到宿主机上,可以通过宿主机API端口就可以直接取到它的监控数据了
maven:3.5.3 bash            # maven的版本镜像
三、配置Java应用的 pom.xml 下dependencies配置参数
### --- 进入容器内部操作;进入到spring-cloud-eureka

root@da23a029c0d6:/# cd opt/spring-cloud-eureka/
~~~     # 修改配置一:添加JVM监控

root@da23a029c0d6:/opt/spring-cloud-eureka# vi pom.xml 
    <dependencies>                                          # 此行下添加如下内容
        <!-- Micrometer Prometheus registry  -->
        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
          </dependency>
         <dependency>
                <groupId>io.micrometer</groupId>
                 <artifactId>micrometer-core</artifactId>
         </dependency>
         <dependency>
                <groupId>io.micrometer</groupId>
                <artifactId>micrometer-registry-prometheus</artifactId>
         </dependency>
        <!-- finished -->

        <dependency>                                         # 此行上添加如上内容
~~~     # 注释一:这个spring-boot使用的是2.1.9的,需要使用2.0以上的

        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
~~~     # 修改配置二:打开端口

root@25ef8f68dc56:/opt/spring-cloud-eureka# vim src/main/resources/application.yml
spring:
  application:
    name: cloud-eureka                          # 此行下添加如下内容
management:
  endpoints:
    web:
      exposure:                                 # 把端口暴露出来,为了方便,直接使用*代替了
        include: '*'
    shutdown:                                   # shutdown关掉,防止远程把这个镜像关闭掉
      enable: false
  metrics:                                      # metrics添加了一个tags,也就是application名称取到了spring的值//在监控数据中就会加入一个label 
    tags:
      application: "${spring.application.name}" # 在监控数据中就会加入一个label
四、编译Java应用生成Jar包,并启动Jar包
### --- 使用maven编译:编译时间比较长,因为需要下载插件等操作

root@25ef8f68dc56:/opt/spring-cloud-eureka# mvn clean package -DskipTests
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< com.zb:spring-cloud-eureka >---------------------
[INFO] Building spring-cloud-eureka 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ spring-cloud-eureka ---
[INFO] Deleting /opt/spring-cloud-eureka/target
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ spring-cloud-eureka ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ spring-cloud-eureka ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /opt/spring-cloud-eureka/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ spring-cloud-eureka ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /opt/spring-cloud-eureka/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ spring-cloud-eureka ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /opt/spring-cloud-eureka/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ spring-cloud-eureka ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ spring-cloud-eureka ---
[INFO] Building jar: /opt/spring-cloud-eureka/target/spring-cloud-eureka-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.1.9.RELEASE:repackage (repackage) @ spring-cloud-eureka ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.745 s
[INFO] Finished at: 2021-05-29T15:02:37Z
[INFO] ------------------------------------------------------------------------
### --- 查看生成的Jar包:编译完成之后会在target目录下生成一个Jar包

root@25ef8f68dc56:/opt/spring-cloud-eureka# ls target/
spring-cloud-eureka-0.0.1-SNAPSHOT.jar.original  
### --- 启动Jar包

root@25ef8f68dc56:/opt/spring-cloud-eureka# java -jar target/spring-cloud-eureka-0.0.1-SNAPSHOT.jar
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.9.RELEASE)








===============================END===============================


Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart                                                                                                                                                   ——W.S.Landor



来自为知笔记(Wiz)

标签:INFO,maven,KuberNetes,运维,v02,spring,eureka,---,cloud
来源: https://www.cnblogs.com/yanqivip/p/16086435.html