感谢博主Redick01,记录一下,防止想要的时候搜不到
平台:Windows 11

1. 部署ES+Kibana

ES用于存储SkyWalknig的日志数据,不想用也可以换成MySQL
(1)创建容器共享网络docker network create elk-net
(2)启动Docker,拉取并运行ES+Kibana

docker pull elasticsearch:7.6.2
docker run -d --name elasticsearch --network elk-net -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
docker pull kibana:7.6.2
docker run -d --name kibana --network elk-net -p 5601:5601 -e "ELASTICSEARCH_HOSTS=http://elasticsearch:9200" kibana:7.6.2

(3)浏览器访问http://localhost:9200/和http://localhost:5601/,各自出现正常内容,说明启动成功

{
  "name" : "552890a11ae3",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "cCc8iMV2T9-Tnnqu2lyeZg",
  "version" : {
    "number" : "7.6.2",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
    "build_date" : "2020-03-26T06:34:37.794943Z",
    "build_snapshot" : false,
    "lucene_version" : "8.4.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

2. 部署SkyWalking

(1)OAP (Observability Analysis Platform):SkyWalking的Server端
作用:接收来自所有 Agent 上报的遥测数据 -> 在内存中实时聚合、分析这些数据 -> 持久化到数据库中

注意run时的启动参数,–link后面的第一个参数和elasticsearch容器名一致

docker pull apache/skywalking-oap-server:8.3.0-es7
docker run --name oap --restart always -d  --network elk-net -e TZ=Asia/Shanghai -p 12800:12800 -p 11800:11800 -e SW_STORAGE=elasticsearch7 -e SW_STORAGE_ES_CLUSTER_NODES=elasticsearch:9200 apache/skywalking-oap-server:8.3.0-es7

(2)UI:可视化控制面板,就是个SkyWalking的Kibana
作用:UI 向 OAP 请求数据 -> OAP 从数据库查询后返回给 UI 进行展示

–link后面的第一个参数和OAP容器名一致

docker pull apache/skywalking-ui:8.3.0
docker run -d --name skywalking-ui --restart=always --network elk-net -e TZ=Asia/Shanghai -p 8088:8080 -e SW_OAP_ADDRESS=oap:12800 apache/skywalking-ui:8.3.0

(3)agent:SkyWalking的Client端
作用:在运行web服务时指定为一个参数(不用修改代码,所以叫无侵入收集) -> 将收集到的数据通过 HTTP 或 gRPC 协议上报给 OAP Server

skywalking官网下载,注意版本要符合上面的OAP和UI的版本(8.3.0),因为开始安装的ES是7.6.2,所以这里也下载apache-skywalking-apm-es7-8.3.0.tar.gz

3. 具体使用

(1)下载作者Redick01提供的示例项目my-transaction,进入其中的spring-transaction文件夹
(2)如果用的不是java 8,修改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">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.5.5</version>
        <relativePath/>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-transaction</artifactId>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- Spring相关 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 数据库 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.27</version>
        </dependency>


        <!-- 其他 -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>5.19.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.40</version>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <targetPath>${project.build.directory}/classes</targetPath>
                <excludes>
                    <exclude>config-sample/*</exclude>
                    <exclude>**/*.java</exclude>
                    <exclude>generatorConfig.xml</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <targetPath>${project.build.directory}/</targetPath>
                <excludes>
                    <exclude>config-sample/*</exclude>
                    <exclude>**/*.java</exclude>
                    <exclude>**/*.zip</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <!-- 是否替换资源中的属性-->
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.14.0</version>
                <configuration>
                    <source>21</source>
                    <target>21</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>config</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                    <excludes>
                                        <exclude>**/config-sample/*.sample</exclude>
                                    </excludes>
                                </resource>
                                <resource>
                                    <directory>src/main/resources/config-sample</directory>
                                </resource>
                            </resources>
                            <outputDirectory>${project.build.directory}/release</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib</classpathPrefix>
                            <mainClass>org.transaction.springtx.Application</mainClass>
                            <useUniqueVersions>false</useUniqueVersions>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>./</Class-Path>
                        </manifestEntries>
                    </archive>
                    <excludes>
                        <exclude>**/*.zip</exclude>
                        <exclude>**/*.properties.sample</exclude>
                        <exclude>generatorConfig.xml</exclude>
                        <exclude>**/*.sh</exclude>
                        <exclude>**/*.md</exclude>
                    </excludes>
                    <outputDirectory>${project.build.directory}/release</outputDirectory>
                </configuration>
            </plugin>
            <!--拷贝依赖包到指定位置-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}/release/lib
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

(3)执行下面的SQL语句

这里插入一下修改MySQL密码的语句,有时候原密码忘了,但是DBeaver之类的客户端还能进,就可以在里面改密码,执行下列指令后,就可以用mysql -u root -p,接着输入新密码即可登录

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
FLUSH PRIVILEGES;
CREATE DATABASE tx;
USE tx;

-- 订单表
CREATE TABLE `order` (
	id BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
	order_no VARCHAR(64) COMMENT '订单号',
	product_id BIGINT(20) COMMENT '商品id',
	pay_count int(11) COMMENT '购买数量',
	create_time datetime  COMMENT '创建时间',
	PRIMARY KEY (`id`),
	UNIQUE KEY `order_no_index` (`order_no`) USING BTREE
);

-- 库存表
CREATE TABLE `stock` (
	id BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
	product_id BIGINT(20) COMMENT '商品id',
	total_count int(11) COMMENT '总数',
	create_time datetime  COMMENT '创建时间',
	PRIMARY KEY (`id`),
	UNIQUE KEY `product_id_idx` (`product_id`) USING BTREE
);

-- 事务日志
CREATE TABLE `tx_log` (
	id BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
	tx_no VARCHAR(64) COMMENT '事务号',
	create_time datetime  COMMENT '创建时间',
	PRIMARY KEY (`id`)
);

然后修改OrderDataSourceConfiguration和StockDataSourceConfiguration中的setUrl、setUsername、setPassword

(4)运行Application后发现没问题就可以开始执行
①解压SkyWalking文件夹到某个目录
②得到jar,mvn install
③运行jar,指定agent地址、jar地址。下面的11800指的是OAP的端口

mvn install
java -jar -javaagent:D:/software/apache-skywalking-apm-bin-es7/agent/skywalking-agent.jar=agent.service_name=fw-gateway,collector.backend_service=127.0.0.1:11800 -jar C:/Users/29525/Desktop/my-transaction-master/spring-transaction/target/release/spring-transaction-3.5.5.jar

④进入localhost:8088即可看到(目前还没调好,进去之后没数据)
⑤如果要调试服务,就在stock表里插个数据然后用postman啥的请求

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐