spring - 导入 org.springframework.boot.actuate.metrics.CounterService 无法解析
问题描述
早些时候我使用spring-boot-starter-parent版本1.5.7.RELEASE
,当我将依赖项升级到2.0.4.RELEASE
时,出现以下错误:导入
org.springframework.boot.actuate.metrics.CounterService 无法解决
此外,Spring Data Mongo 也有更新:
QueryByExampleExecutor 类型中的方法 findOne(Example) 不适用于参数 (String)
CrudRepository 类型中的方法 delete(Person) 不适用于参数 (String)
我很确定,org.springframework.boot.actuate.metrics.CounterService
在 Spring v2.0.x.RELEASE 中已弃用。
任何指针什么是这个类的替换?或者我现在可以调整我的逻辑吗?
PersonCounterService.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.stereotype.Service;
@Service
public class PersonCounterService {
private final CounterService counterService;
@Autowired
public PersonCounterService(CounterService counterService) {
this.counterService = counterService;
}
public void countNewPersons() {
this.counterService.increment("services.person.add");
}
public void countDeletedPersons() {
this.counterService.increment("services.person.deleted");
}
}
人控制器.java
@RestController
@RequestMapping("/person")
public class PersonController {
@Autowired
private PersonRepository repository;
@Autowired
private PersonCounterService counterService;
@GetMapping
public List<Person> findAll() {
return repository.findAll();
}
@GetMapping("/{id}")
public Person findById(@RequestParam("id") String id) {
return repository.findOne(id);
}
@PostMapping
public Person add(@RequestBody Person p) {
p = repository.save(p);
counterService.countNewPersons();
return p;
}
@DeleteMapping("/{id}")
public void delete(@RequestParam("id") String id) {
repository.delete(id);
counterService.countDeletedPersons();
}
@PutMapping
public void update(@RequestBody Person p) {
repository.save(p);
}
@GetMapping("/lastname/{lastName}")
public List<Person> findByLastName(@RequestParam("lastName") String lastName) {
return repository.findByLastName(lastName);
}
@GetMapping("/age/{age}")
public List<Person> findByAgeGreaterThan(@RequestParam("age") int age) {
return repository.findByAgeGreaterThan(age);
}
}
应用程序.yml
server:
port: ${port:2222}
spring:
application:
name: first-service
logging:
pattern:
console: "%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n"
file: "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"
# level:
# org.springframework.web: DEBUG
file: app.log
management:
security:
enabled: false
---
spring:
profiles: production
application:
name: first-service
data:
mongodb:
host: 192.168.99.100
port: 27017
database: microservices
username: micro
password: micro
autoconfigure:
exclude: org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration
解决方案
Micrometer 现在已集成到 spring boot 2.0 中。在这里阅读更多:千分尺-Springboot
使用 MetricRegistry 调整逻辑
@Bean
MeterRegistryCustomizer<MeterRegistry> addPersonRegistry() {
return registry -> registry.config().namingConvention().name("services.person.add", Type.COUNTER);
}
@Bean
MeterRegistryCustomizer<MeterRegistry> deletePersonRegistry() {
return registry -> registry.config().namingConvention().name("services.person.deleted", Type.COUNTER);
}
@Service
public class PersonCounterService {
private final Counter personAddCounter;
private final Counter personDeleteCounter;
public PersonCounterService(MeterRegistry registry) {
this.personAddCounter = registry.counter("services.person.add");
this.personDeleteCounter = registry.counter("services.person.deleted");
}
public void countNewPersons() {
this.personAddCounter.increment();
}
public void countDeletedPersons() {
this.personDeleteCounter.increment();
}
}
最后,点击执行器端点以查看指标:
/actuator/metrics/services.person.add
{"name":"services.person.add","description":null,"baseUnit":null,"measurements":[{"statistic":"COUNT","value":1.0}],"availableTags":[]}