之前我使用的是spring-boot-starter-parent版本1.5.7.RELEASE,当我将依赖项升级到2.0.4.RELEASE时,我得到了以下错误:导入
无法解决org.springframework.boot.actuate.metrics.CounterService问题
此外,Spring Data Mongo还提供了以下更新:
类型QueryByExampleExecutor中的方法QueryByExampleExecutor(示例)不适用于参数(字符串) 类型CrudRepository中的方法delete(Person)不适用于参数(字符串)
我非常肯定,在Springv2.0.x.RELEASE中,org.springframework.boot.actuate.metrics.CounterService被废弃了。
任何指针,替换这个类是什么?或者我现在可以调整我的逻辑了吗?
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");
}
}PersonController.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);
}
}application.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发布于 2018-10-19 10:16:41
千分尺现在集成到弹簧引导2.0。在这里阅读更多信息:千分尺-春靴
使用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":[]}https://stackoverflow.com/questions/52875393
复制相似问题