手摸手入门Springboot+Grafana10.2接收JSON
        【摘要】 手摸手入门Springboot+Grafana10.2接收JSON
    
    
    
    环境介绍
| 技术栈 | springboot+mybatis-plus+mysql+oracle+Prometheus+Grafana | 
| 软件 | 版本 | 
| 8 | |
| IDEA | IntelliJ IDEA 2022.2.1 | 
| JDK | 1.8 | 
| Spring Boot | 2.7.13 | 
| mybatis-plus | 3.5.3.2 | 
本地主机应用 192.168.1.8:8007
grafana应用 http://192.168.68.132:3000
Grafana安装
#安装go语言环境
yum -y install go
#下载grafana-7.2.0-1.x86_64.rpm
wget https://dl.grafana.com/oss/release/grafana-10.2.0-1.x86_64.rpm
#安装
yum -y install grafana-10.2.0-1.x86_64.rpm
#开机自启grafana-server
systemctl enable grafana-server
#开启grafana-server
systemctl start grafana-server
#浏览器输入IP:3000
账号密码默认admin/admin
设置新密码
离线安装json插件
#安装go语言环境
mv marcusolsson-json-datasource /var/lib/grafana/plugins/
systemctl restart grafana-server
springboot应用搭建

引入依赖:将springboot暴露的数据转为普罗米修斯的格式
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
    <scope>runtime</scope>
</dependency>pom.xml
<dependencies>
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.4</version>
    </dependency>
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.oracle.database.jdbc</groupId>
        <artifactId>ojdbc8</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.14</version>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
        <version>3.5.0</version>
    </dependency>
    <dependency>
        <groupId>p6spy</groupId>
        <artifactId>p6spy</artifactId>
        <version>3.9.1</version>
    </dependency>
</dependencies>application.yml
management:
  endpoints:
    web:
      exposure:
        include: "*"   
  endpoint:
    prometheus:
      enabled: true #激活prometheus
    health:
      show-details: always
  metrics:
    export:
      prometheus:
        enabled: true
server:
  port: 8007
spring:
  application:
    name: ProvideAPIServices
  datasource:
    dynamic:
      primary: sys2 #设置默认的数据源或者数据源组,默认值即为master
      strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
      datasource:
        oracle:
          username: system
          password: pwd
          url: jdbc:oracle:thin:@ip:1521:orcl
          driver-class-name: oracle.jdbc.driver.OracleDriver
#          driver-class-name: com.mysql.jdbc.Driver
        wms:
          url: jdbc:p6spy:mysql://ip:3306/Wms?useUnicode=true&characterEncoding=UTF-8
          username: root
          password: 1pwd
          driver-class-name: com.p6spy.engine.spy.P6SpyDriver
#          driver-class-name: com.mysql.jdbc.Driver
        sys2:
          username: root
          password: pwd
          url: jdbc:p6spy:mysql://127.0.0.1:3306/sys?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
          driver-class-name: com.p6spy.engine.spy.P6SpyDriver
mybatis-plus:
  configuration:
    #输出日志
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    #配置映射规则
    map-underscore-to-camel-case: true #表示支持下划线到驼蜂的映射
    #隐藏mybatis图标
  global-config:
    banner: false
    db-config:
      logic-delete-field: status
      logic-not-delete-value: 1
      logic-delete-value: 0Application启动类需添加Bean

@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(@Value("spring.application.name") String applicationName) {
return (registry) ->  registry.config().commonTags("application", applicationName);
}http://192.168.1.8:8007/actuator/prometheus

prometheus添加主机
vim /usr/local/prometheus/prometheus.yml
添加配置
#配置ProvideApiServicesApplication
- job_name: "ProvideAPIServices"
scrape_interval: 5s
metrics_path: "/actuator/prometheus"
static_configs:
- targets: ["IP:8007"]

domain
@TableName(value ="t_address")
@Data
public class TAddress implements Serializable {
    /**
     * 
     */
    @TableId(type = IdType.AUTO)
    private Integer id;
    /**
     * 
     */
    private String address;
    @TableField(exist = false)
    private static final long serialVersionUID = 1L;
}mapper
@Mapper
public interface TAddressMapper extends BaseMapper<TAddress> {
}service
public interface TAddressService extends IService<TAddress> {
}serviceimpl
@Service
@DS("sys2")
public class TAddressServiceImpl extends ServiceImpl<TAddressMapper, TAddress>
    implements TAddressService{
}controller
@Controller
public class demoController {
    @Autowired
    private TAddressServiceImpl tAddressService;
    @RequestMapping("/test")
    @ResponseBody
    public List<TAddress> setTAddressService() {
        return tAddressService.list();
    }
}

 
 


  
            【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
                cloudbbs@huaweicloud.com
                
            
        
        
        
        
        
        
        - 点赞
- 收藏
- 关注作者
 
             
           
评论(0)