Cabbage Java self study room covers core knowledge

Spring Cloud Alibaba combat (1) Prepare for Spring Cloud Alibaba combat (2) Nacos Article Spring Cloud Alibaba combat (3) Sentinel article Spring Cloud Alibaba Spring Cloud Alibaba (Zuul) Spring Cloud Alibaba (Zuul Cloud Alibaba Combat (8) SkyWalking

GitHub address: github.com/D2C-Cai/her…

1. SkyWalking profile

Skywalking is by the domestic open source lover Wu Sheng (former OneAPM engineer, currently in Huawei) open source and submitted to Apache incubator products, it also absorbed Zipkin/Pinpoint/CAT design ideas, support non-invasive buried point. Is a distributed tracking based application performance monitoring system. In addition, the community has developed an organization called OpenTracing to promote some of the specifications and standards for call chain monitoring.

  • SkyWalking is an open source monitoring platform for collecting, analyzing, aggregating, and visualizing data from services and cloud native infrastructure.
  • SkyWalking provides an easy way to maintain a clear view of a distributed system, even across the cloud. It is a modern APM designed specifically for cloud-native, container-based distributed systems.
  • SkyWalking monitors applications from three dimensions: Service (service), Service Instance (instance), and endpoint (endpoint). Without mentioning services and instances, an endpoint is a path or URI within a service.
  • SkyWalking allows users to understand the topological relationship between services and endpoints, view metrics for each service/service instance/endpoint, and set alert rules.

The composition of SkyWalking

The main components of SkyWalking are:

  1. Agent is mainly responsible for collecting various indicators and link data from the system and sending them to THE OAP service.
  2. Oap service receives data sent by Agent, stores data, performs analysis, and provides query and alarm functions.
  3. Storage and UI are responsible for storing and viewing data.

2. Use Docker to quickly build SkyWalking 8.0

  1. Select and create a directory on the Linux server.
mkdir skywalking-docker
Copy the code
  1. Go to the Skywalk-docker directory and create a script file named skywalking.yaml with the following contents:
Version: '3' services: elasticsearch7: image: docker. Elastic. Co/elasticsearch/elasticsearch: 7.5.0 container_name: elasticsearch7 restart: always ports: - 9023:9200 environment: - discovery.type=single-node - bootstrap.memory_lock=true - "ES_JAVA_OPTS=-Xms512m -Xmx512m" - TZ=Asia/Shanghai ulimits:  memlock: soft: -1 hard: -1 networks: - skywalking volumes: - elasticsearch7:/usr/share/elasticsearch/data oap: image: Apache/Skywalking - Oap-server: 8.0.1-ES7 Container_name: oAP depends_on: - ElasticSearch7 Links: - elasticsearch7 restart: always ports: - 9022:11800 - 9021:12800 networks: - skywalking volumes: -./ext-config:/skywalking/ext-config UI: image: apache/ Skywalking - UI :8.0.1 Container_name: UI depends_on: -oAP links: - oap restart: always ports: - 9020:8080 environment: SW_OAP_ADDRESS: oap:12800 networks: - skywalking networks: skywalking: driver: bridge volumes: elasticsearch7: driver: localCopy the code

Note: If we want to overwrite the configuration files under/Skywalking /config in the OAP image, we can mount a/Skywalking /ext-config directory in docker and drop the configuration files into this directory.

  1. Execute the Skywalking. yaml script to start the container:
docker-compose -f skywalking.yaml up
Copy the code
  1. Enter the SkyWalking console and find the various dashboards, which are of course empty at first:
http://(Install IP of SkyWalking machine):9020Copy the code

3. Introduce the SkyWalking client in the Spring project

The use of traceId for global log tracing:

  1. Add POM file dependencies:
<dependency> <groupId>org.apache.skywalking</groupId> <artifactId>apm-toolkit-logback-1.x</artifactId> < version > 8.0.1 < / version > < / dependency > < the dependency > < groupId > org. Apache. Skywalking < / groupId > < artifactId > apm toolkit - trace < / artifactId > < version > 8.0.1 < / version > < / dependency >Copy the code
  1. Add the logback-spring. XML file to the resources directory as follows:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="logger.path" value="/mnt/logs"/>

    <!-- 彩色日志 -->
    <!-- 彩色日志依赖的渲染类 -->
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
    <conversionRule conversionWord="wex"
                    converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
    <conversionRule conversionWord="wEx"
                    converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>
    <!-- 彩色日志格式 -->
    <property name="CONSOLE_LOG_PATTERN"
              value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>


    <!-- 输出到控制台 -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>info</level>
        </filter>
        <encoder>
            <Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!-- ConsoleAppender:把日志输出到控制台 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.mdc.TraceIdMDCPatternLogbackLayout">
                <Pattern>
                    <![CDATA[%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} [%X{tid}] %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}]]></Pattern>
            </layout>
        </encoder>
    </appender>

    <!-- 输出到文件 -->
    <!-- 时间滚动输出 level为 DEBUG 日志 -->
    <!-- <appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${logger.path}/log_debug.log</file>
        &lt;!&ndash;日志文件输出格式&ndash;&gt;
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            <charset>UTF-8</charset> &lt;!&ndash; 设置字符集 &ndash;&gt;
        </encoder>
        &lt;!&ndash; 日志记录器的滚动策略,按日期,按大小记录 &ndash;&gt;
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            &lt;!&ndash; 日志归档 &ndash;&gt;
            <fileNamePattern>${logger.path}/debug/log-debug-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            &lt;!&ndash;日志文件保留天数&ndash;&gt;
            <maxHistory>15</maxHistory>
        </rollingPolicy>
        &lt;!&ndash; 此日志文件只记录debug级别的 &ndash;&gt;
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>debug</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender> -->

    <!-- 时间滚动输出 level为 INFO 日志 -->
    <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 正在记录的日志文件的路径及文件名 -->
        <file>${logger.path}/log_info.log</file>
        <!--日志文件输出格式-->
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
        <!-- 日志记录器的滚动策略,按日期,按大小记录 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 每天日志归档路径以及格式 -->
            <fileNamePattern>${logger.path}/info/log-info-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <!--日志文件保留天数-->
            <maxHistory>15</maxHistory>
        </rollingPolicy>
        <!-- 此日志文件只记录info级别的 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>info</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- 时间滚动输出 level为 WARN 日志 -->
    <!-- <appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        &lt;!&ndash; 正在记录的日志文件的路径及文件名 &ndash;&gt;
        <file>${logger.path}/log_warn.log</file>
        &lt;!&ndash;日志文件输出格式&ndash;&gt;
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            <charset>UTF-8</charset> &lt;!&ndash; 此处设置字符集 &ndash;&gt;
        </encoder>
        &lt;!&ndash; 日志记录器的滚动策略,按日期,按大小记录 &ndash;&gt;
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logger.path}/warn/log-warn-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            &lt;!&ndash;日志文件保留天数&ndash;&gt;
            <maxHistory>15</maxHistory>
        </rollingPolicy>
        &lt;!&ndash; 此日志文件只记录warn级别的 &ndash;&gt;
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>warn</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender> -->

    <!-- 时间滚动输出 level为 ERROR 日志 -->
    <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 正在记录的日志文件的路径及文件名 -->
        <file>${logger.path}/log_error.log</file>
        <!--日志文件输出格式-->
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            <charset>UTF-8</charset> <!-- 此处设置字符集 -->
        </encoder>
        <!-- 日志记录器的滚动策略,按日期,按大小记录 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logger.path}/error/log-error-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <!--日志文件保留天数-->
            <maxHistory>15</maxHistory>
        </rollingPolicy>
        <!-- 此日志文件只记录ERROR级别的 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!--
        root节点是必选节点,用来指定最基础的日志输出级别,只有一个level属性
        level:用来设置打印级别,大小写无关:TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF,
        不能设置为INHERITED或者同义词NULL。默认是DEBUG
        可以包含零个或多个元素,标识这个appender将会添加到这个logger。
    -->
    <root level="info">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="STDOUT"/>
        <!--<appender-ref ref="DEBUG_FILE" />-->
        <appender-ref ref="INFO_FILE"/>
        <!--<appender-ref ref="WARN_FILE" />-->
        <appender-ref ref="ERROR_FILE"/>
    </root>

</configuration>
Copy the code

Note: everything else is a general log configuration, mainly the
configuration.

  1. Enter SkyWalking official website to download SkyWalking APM, mainly to use agent:

Skywalking website to download address: skywalking.apache.org/downloads/

  1. Decompress the downloaded apache-Skywalk-APm-ES7-8.0.1.tar. gz package. The following figure shows the directory structure:

We only need the agent directory, which contains the following items:

Copy the agent directory to a proper directory. Later, you need to configure the JVM startup parameters directory.

  1. Add the following JVM startup parameters to the IDEA startup command:
- javaagent: agent folder directory \ agent \ skywalking - agent. The jar - Dskywalking. Agent. The service_name = (services) - service - Dskywalking. Agent. Instance_name = (services) - the instance - Dskywalking. Collector. Backend_service = (install SkyWalking machine IP) : 9022Copy the code

Because Skywalking is a non-invasive buried point to achieve distributed link tracking and performance monitoring, javaAgent is generally adopted.

What is Javaagent (static Instrument before JVM startup)?

Javaagent is a parameter to a Java command. The javaAgent argument can be used to specify a JAR package with two requirements for the Java package:

  1. The MANIFEst.mf file of this JAR package must specify a premain-class entry.
  2. The Class specified by premain-class must implement the Premain () method.

The premain() method is, literally, the class that runs before main(). When the Java virtual machine starts, the JVM runs the Premain () method of the premain-class Class in the jar specified by -JavaAgent before executing the main() function.

  1. We have built a complete project in the previous several articles. In these projects, we will configure all of them according to the above configuration and start to see the effect:
  • Gateway services: Herring -gateway, zuul unified gateway micro service.
  • Authentication services: Herring oauth2, OAuth2 authentication center microservices.
  • Member service: herring-member-service, one of the micro services, will receive the request to the certification center verification.
  • Order service: herring orders-service, the second micro service, receives the request and goes to the certification center for verification.
  • Goods and services: herring product-service, the third micro service, will be verified by the certification center after receiving the request.

  1. Request token/API /member/update:
#### POST http://localhost:8080/oauth2-service/oauth/token? grant_type=password&username=admin&password=123456&client_id=app-client&client_secret=client-secret-8888&scope=all Accept: */* Cache-Control: no-cacheCopy the code

Return token:

{ "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiZ2F0ZXdheS1zZXJ2aWNlIl0sInVzZXJfbmFtZSI6ImFkbWluIiwiand0LWV4dCI6IkpXVC DmianlsZXkv6Hmga8iLCJzY29wZSI6WyJhbGwiXSwiZXhwIjoxNjEzOTcwMDk2LCJhdXRob3JpdGllcyI6WyJST0xFX0FETUlOIl0sImp0aSI6IjU4MDY5OD lhLWUyNDQtNGQyMy04YTU5LTBjODRiYzE0Yjk5OSIsImNsaWVudF9pZCI6ImFwcC1jbGllbnQifQ.EP4acam0tkJQ9kSGRGk_mQsfi1y4M_hhiBL0H931v60 ", "token_type": "bearer", "refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiZ2F0ZXdheS1zZXJ2aWNlIl0sInVzZXJfbmFtZSI6ImFkbWluIiwiand0LWV4dCI6IkpXVC DmianlsZXkv6Hmga8iLCJzY29wZSI6WyJhbGwiXSwiYXRpIjoiNTgwNjk4OWEtZTI0NC00ZDIzLThhNTktMGM4NGJjMTRiOTk5IiwiZXhwIjoxNjE0MDM0OD k2LCJhdXRob3JpdGllcyI6WyJST0xFX0FETUlOIl0sImp0aSI6IjQxZGM1ZDc1LTZmZDgtNDU3My04YmRjLWI4ZTMwNWEzMThmMyIsImNsaWVudF9pZCI6Im FwcC1jbGllbnQifQ.CGmGx_msqJBHxa95bBROY2SAO14RyeRklVPYrRxZ7pQ", "expires_in": 7199, "scope": "all", "jwt-ext": "JWT extension info ", "JTI ":" 5806989A-e244-4D23-8a59-0c84bc14b999 "}Copy the code

Request execution/API /member/update

#### GET http://localhost:8080/member-service/api/member/update Accept: */* Cache-Control: no-cache Authorization: bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiZ2F0ZXdheS1zZXJ2aWNlIl0sInVzZXJfbmFtZSI6ImFkbWluIiwiand0LWV4dCI6IkpXVCD mianlsZXkv6Hmga8iLCJzY29wZSI6WyJhbGwiXSwiZXhwIjoxNjEzOTcwMDk2LCJhdXRob3JpdGllcyI6WyJST0xFX0FETUlOIl0sImp0aSI6IjU4MDY5ODl hLWUyNDQtNGQyMy04YTU5LTBjODRiYzE0Yjk5OSIsImNsaWVudF9pZCI6ImFwcC1jbGllbnQifQ.EP4acam0tkJQ9kSGRGk_mQsfi1y4M_hhiBL0H931v60Copy the code

Dashboard results display:

Topology result display:

Display of link tracking results:

Spring Cloud Alibaba combat (1) Prepare for Spring Cloud Alibaba combat (2) Nacos Article Spring Cloud Alibaba combat (3) Sentinel article Spring Cloud Alibaba Spring Cloud Alibaba (Zuul) Spring Cloud Alibaba (Zuul Cloud Alibaba Combat (8) SkyWalking

GitHub address: github.com/D2C-Cai/her…