Apache ServiceComb集成Apache CouchDB开发过程
【摘要】 Apache CouchDB是一个专为现代Web应用设计的开源文档型数据库管理系统。本任务的主要目的是让Service Comb微服架构集成Apache CouchDB,并且基于Apache CouchDB数据库和华为云CCE完成功能验证。
Apache CouchDB概述
Apache CouchDB是一个开源的文档型数据库管理系统,专为现代Web应用设计。CouchDB以JSON文档为单位存储数据,这种数据结构非常适合表示复杂的数据关系。高可用性与可扩展性:即使在容易出现故障的硬件上,CouchDB也能提供高可用性和高可靠性,并且具有高度可伸缩性。
ServiceComb概述
Apache ServiceComb是一个实现Open API规范的RPC框架,提供了配置管理、服务发现、动态路由、可观察性和服务治理功能。
参考文档
- CouchDB官网:https://docs.couchdb.org/
- CouchDB API文档:https://docs.couchdb.org/en/stable/intro/api.html#databases
- ServiceComb官网:https://servicecomb.apache.org/
- DEMO演示地址:https://gitcode.com/weiwenman/opensource-demo-hibernate/overview?ref=couchdb
- 博客地址:https://bbs.huaweicloud.com/blogs/442100
开发过程
第一步:CCE部署Apache CouchDB,部署yaml如下
root@DESKTOP-7490:~/k8s# cat deploy-couchdb.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: couchdb
labels:
app: couchdb-pods
spec:
replicas: 1
selector:
matchLabels:
app: couchdb-pods
template:
metadata:
name: couchdb-pod
labels:
app: couchdb-pods
spec:
containers:
- name: couchdb-container
image: apache/couchdb:latest
imagePullPolicy: IfNotPresent
env:
- name: COUCHDB_USER
value: admin
- name: COUCHDB_PASSWORD
value: admin
ports:
- containerPort: 5984
protocol: TCP
resources:
requests:
memory: "512Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "250m"
restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
name: couchdb
spec:
selector:
app: couchdb-pods
ports:
- protocol: TCP
port: 5984
targetPort: 5984
type: LoadBalancer
启动CouchDB之后可通过访问:http://ip:5984/_utils ,输入账号admin,密码admin访问couchDB控制台
第二步:修改配置文件
- 下载ServiceComb代码,并修改pom.xml,增加org.ektorp依赖
<dependency>
<groupId>org.ektorp</groupId>
<artifactId>org.ektorp</artifactId>
<version>1.5.0</version>
</dependency>
- 修改application.yml,新增CouchDB连接配置
servicecomb:
couchdb:
uri: http://localhost:5984
username: admin
password: admin
- 新增config
@Component
@ConfigurationProperties(prefix = "servicecomb.couchdb")
public class CouchDBConfig {
private String uri;
private String username;
private String password;
第三步:代码开发
- 创建doc和domain
public class ActorDoc {
@JsonProperty("_id")
private String id;
@JsonProperty("_rev")
private String rev;
private Actor actor;
}
public class Actor {
private String id;
private String name;
private String type;
}
- 创建service
@Component
public class CouchDBService {
@Autowired
private CouchDBConfig config;
public CouchDbConnector getConnector(String dbName) throws Exception {
HttpClient client = new StdHttpClient.Builder()
.url(config.getUri()).username(config.getUsername()).password(config.getPassword()).build();
CouchDbInstance dbInstance = new StdCouchDbInstance(client);
return new StdCouchDbConnector(dbName, dbInstance);
}
public CouchDBConfig getConfig() {
return config;
}
public void setConfig(CouchDBConfig config) {
this.config = config;
}
}
- 创建Api
@RestController
@RequestMapping("/actor")
public class CouchDBDemoApi {
@Autowired
private CouchDBService couchDBService;
@GetMapping("/docs/{dbName}")
public List<String> list(@PathVariable String dbName) throws Exception {
return couchDBService.getConnector(dbName).getAllDocIds();
}
@GetMapping("/doc/{dbName}/{docId}")
public ActorDoc listActor(@PathVariable String dbName, @PathVariable String docId) throws Exception {
CouchDbConnector connector = couchDBService.getConnector(dbName);
return connector.get(ActorDoc.class, docId);
}
@PostMapping("/doc/{dbName}")
public ActorDoc addActor(@PathVariable String dbName, @RequestBody Actor actor) throws Exception {
ActorDoc doc = new ActorDoc();
doc.setActor(actor);
CouchDbConnector connector = couchDBService.getConnector(dbName);
connector.create(doc);
return doc;
}
}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)