Liberty + Spring Data 中的容器管理的 MongoDB 连接 [英] Container managed MongoDB Connection in Liberty + Spring Data

查看:34
本文介绍了Liberty + Spring Data 中的容器管理的 MongoDB 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在 Spring Boot + spring data(后端)+ MongoDB 中开发了一个应用程序,并使用了 IBM Websphere Liberty代码> 作为应用程序服务器.我们在 yml 文件中使用了应用程序管理的数据库连接",并享受了 Spring Boot 自动配置 的好处.

由于政策变化,我们需要在 Server.xml 中管理 Liberty Server 中的数据库连接(使用 mongo 功能).我花了一整天的时间寻找一个很好的例子来做到这一点,但在 Spring 中没有找到任何例子,在 IBM Websphere Liberty Server 中使用 Container Managed MongoDB Connection".

有人可以在这里支持吗?

解决方案

过去 Liberty 为 server.xml 提供了一个专用的 mongodb-2.0 特性,但是这个特性提供的好处非常小,因为您仍然需要带上自己的 MongoDB 库.此外,随着时间的推移,MongoDB 对其 API 进行了重大的突破性更改,包括 MongoDB 的配置方式.

由于 MongoDB API 在不同版本之间变化如此之大,我们发现最好在 Liberty 中提供任何新的 MongoDB 功能,而是建议用户简单地使用这样的 CDI 生成器:

CDI 生产者(也持有任何配置):

@ApplicationScoped公共类 MongoProducer {@Produces公共 MongoClient createMongo() {return new MongoClient(new ServerAddress(), new MongoClientOptions.Builder().build());}@Produces公共 MongoDatabase createDB(MongoClient 客户端){返回 client.getDatabase("testdb");}public void close(@Disposes MongoClient toClose) {toClose.close();}}

示例用法:

@InjectMongoDatabase db;@POST@Path("/添加")@Consumes(MediaType.APPLICATION_JSON)公共无效添加(船员成员船员成员){MongoCollection<文档>船员 = db.getCollection("船员");文档 newCrewMember = new Document();newCrewMember.put("Name",crewMember.getName());newCrewMember.put("Rank",crewMember.getRank());newCrewMember.put("CrewID",crewMember.getCrewID());船员.insertOne(newCrewMember);}

这只是基础知识,但以下博客文章更详细地介绍了代码示例:https://openliberty.io/blog/2019/02/19/mongodb-with-open-liberty.html

We have developed an application in Spring Boot + spring data (backend) + MongoDB and used IBM Websphere Liberty as application Server. We were used "Application Managed DB Connection" in an yml file and enjoyed the benefit of Spring Boot autoconfiguration.

Due to policy changes, we would need to manage our DB Connection in Liberty Server(using mongo feature), in Server.xml. I spent whole day in finding out an good example to do this, but dont find any example in Spring with "Container Managed MongoDB Connection" in IBM Websphere Liberty Server.

Can someone please support here?

解决方案

In the past Liberty had a dedicated mongodb-2.0 feature for the server.xml, however this feature provided pretty minimal benefit, since you still needed to bring your own MongoDB libraries. Also, over time MongoDB made significant breaking changes to their API, including how MongoDB gets configured.

Since the MongoDB API is changing so drastically between releases, we found it better to not provide any new MongoDB features in Liberty and instead suggest that users simply use a CDI producer like this:

CDI producer (holds any configuration too):

@ApplicationScoped
public class MongoProducer {

    @Produces
    public MongoClient createMongo() {
        return new MongoClient(new ServerAddress(), new MongoClientOptions.Builder().build());
    }

    @Produces
    public MongoDatabase createDB(MongoClient client) {
        return client.getDatabase("testdb");
    }

    public void close(@Disposes MongoClient toClose) {
        toClose.close();
    }
}

Example usage:

@Inject
MongoDatabase db;

@POST
@Path("/add")
@Consumes(MediaType.APPLICATION_JSON)
public void add(CrewMember crewMember) {
    MongoCollection<Document> crew = db.getCollection("Crew");
    Document newCrewMember = new Document();
    newCrewMember.put("Name",crewMember.getName());
    newCrewMember.put("Rank",crewMember.getRank());
    newCrewMember.put("CrewID",crewMember.getCrewID());
    crew.insertOne(newCrewMember);
}

This is just the basics, but the following blog post goes into much greater detail along with code examples: https://openliberty.io/blog/2019/02/19/mongodb-with-open-liberty.html

这篇关于Liberty + Spring Data 中的容器管理的 MongoDB 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆