Permalink
Browse files

Commit iniziale

  • Loading branch information...
fpaparoni committed Mar 27, 2017
1 parent a8a5698 commit f9da88125ff7ecfd05e920114924733c8d980740
@@ -1 +1 @@
# SpringBootActuator
Progetto d'esempio dell'articolo "Metriche con Spring Boot Actuator"
68 pom.xml
@@ -0,0 +1,68 @@
<?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">
<modelVersion>4.0.0</modelVersion>

<groupId>com.javastaff</groupId>
<artifactId>spring.boot.actuator</artifactId>
<version>0.0.1</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-hal-browser</artifactId>
<version>2.6.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.1.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>

<properties>
<java.version>1.8</java.version>
</properties>


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
@@ -0,0 +1,12 @@
package com.javastaff.spring.boot.actuator;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ApplicationStarter {

public static void main(String[] args) {
SpringApplication.run(ApplicationStarter.class, args);
}
}
@@ -0,0 +1,31 @@
package com.javastaff.spring.boot.actuator;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.stereotype.Component;

@Component
public class CustomEndpoint implements Endpoint<HashMap<String, List<String>>> {

public String getId() {
return "customEndpoint";
}

public boolean isEnabled() {
return true;
}

public boolean isSensitive() {
return true;
}

public HashMap<String, List<String>> invoke() {
HashMap<String, List<String>> map = new HashMap<>();
map.put("enabled", Arrays.asList("feature1","feature2"));
map.put("notAllowed", Arrays.asList("command1","command2"));
return map;
}
}
@@ -0,0 +1,24 @@
package com.javastaff.spring.boot.actuator;

import org.springframework.boot.actuate.autoconfigure.ExportMetricWriter;
import org.springframework.boot.actuate.metrics.export.MetricExportProperties;
import org.springframework.boot.actuate.metrics.repository.redis.RedisMetricRepository;
import org.springframework.boot.actuate.metrics.writer.MetricWriter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.Protocol;

@Configuration
public class CustomExport {
@Bean
@ExportMetricWriter
MetricWriter metricWriter(MetricExportProperties export) {
JedisShardInfo shardInfo = new JedisShardInfo("localhost", Protocol.DEFAULT_PORT);
JedisConnectionFactory factory=new JedisConnectionFactory(shardInfo);
return new RedisMetricRepository(factory,
export.getRedis().getPrefix(), export.getRedis().getKey());
}
}
@@ -0,0 +1,54 @@
package com.javastaff.spring.boot.actuator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.Protocol;

@Component
public class MyCustomServiceHealthIndicator implements HealthIndicator {

private static final Logger LOG = LoggerFactory.getLogger(MyCustomServiceHealthIndicator.class);

@Override
public Health health() {
int status = redisCheck();
if (status == 0) {
return Health.down().build();
} else {
String lastItem=redisLastItem();
return Health.up().withDetail("lastItem", lastItem).build();
}
}

private String redisLastItem() {
JedisShardInfo shardInfo = new JedisShardInfo("localhost", Protocol.DEFAULT_PORT);
Jedis jedis = new Jedis(shardInfo);
String lastItem=jedis.randomKey();
jedis.close();
return lastItem;
}

public int redisCheck() {
LOG.info("Checking REDIS...");
int status=0;
try {
JedisShardInfo shardInfo = new JedisShardInfo("localhost", Protocol.DEFAULT_PORT);
Jedis jedis = new Jedis(shardInfo);
String pong=jedis.ping();
if (pong.equals("PONG"))
status=1;
jedis.close();
}
catch (Exception ex) {
LOG.error(ex.getMessage());
}

return status;
}
}
@@ -0,0 +1,23 @@
package com.javastaff.spring.boot.actuator;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.boot.actuate.metrics.GaugeService;
import org.springframework.stereotype.Service;

@Service
public class ServiceWithMetrics {
@Autowired
private CounterService counterService;

@Autowired
private GaugeService gaugeService;

@PostConstruct
public void faccioCose() {
counterService.increment("faccio.cose");
gaugeService.submit("faccio.grandi.cose", 123);
}
}
@@ -0,0 +1,14 @@
management:
security:
enabled: false

endpoints:
shutdown:
enabled: true

spring:
metrics:
export:
redis:
prefix: javastaff.actuator.test.${random.value:0000}
key: keys.javastaff.actuator

0 comments on commit f9da881

Please sign in to comment.