这个依赖不存在 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-etcd</artifactId> <version>3.0.1</version></dependency>
时间: 2023-07-09 21:45:27 浏览: 88
spring5 和 依赖包 spring-framework-5.0.7 spring-framework-3.0.2.dependencies
非常抱歉,我犯了一个错误。目前Spring Cloud Etcd Starter的最新版本是2.2.4.RELEASE。以下是正确的依赖:
```xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-etcd</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
```
同时,我也注意到在上一个回答中缺少一些必要的配置。下面是完整的示例代码:
在pom.xml中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-etcd</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
```
在application.yml中添加ETCD的配置:
```yaml
spring:
cloud:
etcd:
endpoints: http://localhost:2379
```
在Service中注入EtcdKeyValueRepository并进行操作:
```java
import org.springframework.cloud.etcd.config.EtcdKeyValueRepository;
import org.springframework.stereotype.Service;
@Service
public class EtcdService {
private final EtcdKeyValueRepository etcdKeyValueRepository;
public EtcdService(EtcdKeyValueRepository etcdKeyValueRepository) {
this.etcdKeyValueRepository = etcdKeyValueRepository;
}
public void save(String key, String value) {
etcdKeyValueRepository.put(key, value);
}
public String get(String key) {
return etcdKeyValueRepository.get(key);
}
public void delete(String key) {
etcdKeyValueRepository.delete(key);
}
}
```
在Controller中调用EtcdService来操作ETCD:
```java
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/etcd")
public class EtcdController {
private final EtcdService etcdService;
public EtcdController(EtcdService etcdService) {
this.etcdService = etcdService;
}
@PostMapping("/{key}/{value}")
public void save(@PathVariable String key, @PathVariable String value) {
etcdService.save(key, value);
}
@GetMapping("/{key}")
public String get(@PathVariable String key) {
return etcdService.get(key);
}
@DeleteMapping("/{key}")
public void delete(@PathVariable String key) {
etcdService.delete(key);
}
}
```
希望这次回答能够帮到你,如有其他问题,请随时提出。
阅读全文