1. 程式人生 > 其它 >|NO.Z.00343|——————————|CloudNative|——|KuberNetes&運維.V60|-----------------------------------------------------------|Prometheus.v02|Micrometer.v02|JVM專案|

|NO.Z.00343|——————————|CloudNative|——|KuberNetes&運維.V60|-----------------------------------------------------------|Prometheus.v02|Micrometer.v02|JVM專案|



[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)