SpringBoot project integration with Skywalking

1 Case Preparation

We write a SpringBoot integrated MyBatisPlus case to realize the ordering operation, and the services are as follows:

  • Skywalking – Order order service
  • Skywalking -item Goods and Services

The table structure is as follows:

-- ----------------------------
-- Table structure for tb_item
-- ----------------------------
DROP TABLE IF EXISTS `tb_item`;
CREATE TABLE `tb_item` (
  `id` int(11) NOT NULL,
  `title` varchar(200) NOT NULL COMMENT 'Trade Name',
  `price` int(11) NOT NULL,
  `count` int(11) NOT NULL.PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of tb_item
-- ----------------------------
INSERT INTO `tb_item` VALUES ('1'.'huawei P40'.'8000'.'10');
INSERT INTO `tb_item` VALUES ('2'.'30 s glory'.'3500'.'100');


-- ----------------------------
-- Table structure for tb_order
-- ----------------------------
DROP TABLE IF EXISTS `tb_order`;
CREATE TABLE `tb_order` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `item_id` int(11) NOT NULL COMMENT 'commodity ID',
  `count` int(11) NOT NULL COMMENT 'Purchase Quantity'.PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
Copy the code

1.1 Order Engineering

Let’s create a normal SpringBoot integrated MyBatis implementation for the TB_ORDER operation.

1) engineering pom. XML


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

    <groupId>com.itmentu</groupId>
    <artifactId>skywalking-order</artifactId>
    <version>1.0 the SNAPSHOT</version>

    <! -- Spring Boot project -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.8. RELEASE</version>
    </parent>

    <properties>
        <! - the JDK version - >
        <java.version>1.8</java.version>
        <! -- Skip test -->
        <skipTests>true</skipTests>
        <lombok.version>1.18.8</lombok.version>
        <mybatis.plus.version>3.1.1</mybatis.plus.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis.plus.version}</version>
        </dependency>
        <! -- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>
</project>
Copy the code

2) start the class

Create start class com. Itmentu. OrderApplication

@SpringBootApplication
@MapperScan(basePackages = "com.itmentu.mapper")
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class,args);
    }

    /*** * Create */
    @Bean
    public RestTemplate restTemplate(a){
        return newRestTemplate(); }}Copy the code

3) Core configuration files

Create application.yml with the following configuration:

server:
  port: 18081
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/skywalking? serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull
    driverClassName: com.mysql.jdbc.Driver
    username: root
    password: 123456
Copy the code

4)Pojos

Create com.itmentu.pojos.Order as follows:

@Data
@TableName(value = "tb_order")
public class Order {
    @TableId(value = "id",type = IdType.AUTO)
    private Integer id;
    @TableField(value = "item_id")
    private Integer itemId;
    @TableField(value = "count")
    private Integer count;
}
Copy the code

5)Service

Interface: com. Itmentu. Service. The OrderService

public interface OrderService extends IService<Order> {}Copy the code

Interface implementation class: com. Itmentu. Service. Impl. OrderServiceImpl

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper.Order> implements OrderService {}Copy the code

6)Mapper

Create com. Itmentu. Mapper. OrderMapper

public interface OrderMapper extends BaseMapper<Order> {}Copy the code

7)Controller

To create the controller com. Itmentu. Controller OrderController, code is as follows:

@RestController
@RequestMapping(value = "/order")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private OrderService orderService;

    /** ** order */
    @PostMapping(value = "/{id}/{count}")
    public String add(@PathVariable(value = "id")Integer id,
                      @PathVariable(value = "count")Integer count){
        //1) Decrease inventory
        Integer hCount = restTemplate.getForObject("http://localhost:18082/item/1/1", Integer.class);

        / / 2) order
        if(hCount>0){
            Order order = new Order();
            order.setCount(count);
            order.setItemId(id);
            orderService.save(order);
        }
        return "SUCCESS"; }}Copy the code

1.2 Commodity Engineering

Let’s create a normal SpringBoot integrated MyBatis implementation for tb_item operations.

1) engineering pom. XML


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

    <groupId>com.itmentu</groupId>
    <artifactId>skywalking-item</artifactId>
    <version>1.0 the SNAPSHOT</version>

    <! -- Spring Boot project -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.8. RELEASE</version>
    </parent>

    <properties>
        <! - the JDK version - >
        <java.version>1.8</java.version>
        <! -- Skip test -->
        <skipTests>true</skipTests>
        <lombok.version>1.18.8</lombok.version>
        <mybatis.plus.version>3.1.1</mybatis.plus.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis.plus.version}</version>
        </dependency>
        <! -- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>
</project>
Copy the code

2) start the class

Create start class com. Itmentu. ItemApplication

@SpringBootApplication
@MapperScan(basePackages = "com.itmentu.mapper")
public class ItemApplication {

    public static void main(String[] args) { SpringApplication.run(ItemApplication.class,args); }}Copy the code

3) Core configuration files

Create application.yml with the following configuration:

server:
  port: 18082
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/skywalking? serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull
    driverClassName: com.mysql.jdbc.Driver
    username: root
    password: 123456
Copy the code

4)Pojos

Create com.itmentu.pojos.Item as follows:

@Data
@TableName(value = "tb_item")
public class Item {
    @TableId(value = "id",type = IdType.AUTO)
    private Integer id;
    @TableField(value = "title")
    private String title;
    @TableField(value = "price")
    private Integer price;
    @TableField(value = "count")
    private Integer count;
}
Copy the code

5)Service

Interface: com. Itmentu. Service. ItemService

public interface ItemService extends IService<Item> {
    Integer incr(Integer id, Integer count);
}
Copy the code

Interface implementation class: com. Itmentu. Service. Impl. ItemServiceImpl

@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper.Item> implements ItemService {

    @Autowired
    private ItemMapper itemMapper;

    /** * inventory decrease */
    @Override
    public Integer incr(Integer id, Integer count) {
        returnitemMapper.updateItem(id,count); }}Copy the code

6)Mapper

Create com. Itmentu. Mapper. ItemMapper

public interface ItemMapper extends BaseMapper<Item> {
    @Update("update tb_item set count=count-#{count} where id=#{id} and count>=#{count}")
    Integer updateItem(@Param("id") Integer id,@Param("count") Integer count);
}
Copy the code

7)Controller

To create the controller com. Itmentu. Controller ItemController, code is as follows:

@RestController
@RequestMapping(value = "/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    /** * inventory decrease */
    @GetMapping(value = "/{id}/{count}")
    public Integer incrCount(@PathVariable(value = "id")Integer id,
                             @PathVariable(value = "count")Integer count){
        returnitemService.incr(id,count); }}Copy the code

2 integrated Skywalking

Using IDEA integration and uploading application data to Skywalking is really just a matter of configuring startup parameters, but for JAR based integration, command integration is required.

2.1 Probe Download

To achieve data collection, the first step is to download the probe, download the address; skywalking.apache.org/downloads/

After downloading, we unzip the file into D:\dev\install\itmentu\ as shown below:

Jar and config/agent.config files are probe files and core configuration files respectively, as shown below:

2.2 IDEA Agent probe is used

IDEA integrated probe, you only need to set the following parameters:

-javaagent:skywalking-agent.jar
-Dskywalking_config=agent.config
-Dskywalking.collector.backend_service=Skywalking OAP service address
-Dskywalking.agent.service_name=The name of the service registered in Skywalking for the current service
Copy the code

When we run skywalk-item and skywalk-order, we can add the above parameters as follows:

The configuration is as follows:

-javaagent:D:/dev/install/itmentu/apache-skywalking-apm-bin-es7/agent/skywalking-agent.jar 
-Dskywalking_config=D:/dev/install/itmentu/apache-skywalking-apm-bin-es7/agent/config/agent.config 
-Dskywalking.collector.backend_service=192.168.211.130:11800 
-Dskywalking.agent.service_name=item
Copy the code

Let’s configure skywalk-order as follows:

-javaagent:D:/dev/install/itmentu/apache-skywalking-apm-bin-es7/agent/skywalking-agent.jar 
-Dskywalking_config=D:/dev/install/itmentu/apache-skywalking-apm-bin-es7/agent/config/agent.config 
-Dskywalking.collector.backend_service=192.168.211.130:11800 
-Dskywalking.agent.service_name=order
Copy the code

When the project is running, we look at the topology of each major service, and the results are as follows:

2.3 Production Environment Integration

Production use, so we need to agent and jar package uploaded to the server for each project, upload the apache – skywalking – apm – bin to/usr/local/server/skywalking, again will project in the package, and uploaded to the server, Execute the startup program plus command:

java -javaagent:/usr/local/server/skywalking/apache-skywalking-apm-bin/agent/skywalking-agent.jar - Dskywalking. Agent. The service_name = item - jar skywalking - item - 1.0 - the SNAPSHOT. The jar
Copy the code