[16.0.0.3 and later]

配置 Cloudant 驱动程序的高级功能

Liberty 上,可使用 Cloudant Java 客户机的高级功能。

开始之前

请参阅 Cloudant 配置主题在 Liberty 中使用 Cloudant Java 客户机库来配置 CouchDB 连接,并确保配置 server.xml 中的必要资源。

关于此任务

作为 Cloudant Java 客户机的用户,您可能希望在启用 Java 2 安全性的情况下运行服务器,或将认证别名应用于资源引用。另外,可以在没有任何 WebSphere 集成功能的情况下使用 Cloudant Java 客户机。下列步骤可以相互独立地完成。

过程

  1. 如果在启用了 Java 2 安全性的情况下运行服务器,那么必须授予对 cloudant 客户机库的许可权。

    例如,在 server.xml 中,可以指定:

    <javaPermission codebase="${shared.resource.dir}cloudant/cloudant-client-2.2.0.jar" className="java.security.AllPermission"/>
  2. server.xml 文件中创建缺省容器认证别名元素,以提供将用于向数据库进行认证的用户名和密码。
    <cloudant ... containerAuthDataRef="cadr"/>
    <authData id="cadr" user="username" password="password"/>
    提示: 可以使用 cloudant 元素的用户名和密码属性,以代替指定缺省容器认证别名。有关更多信息,请参阅 Liberty:通过密码加密进行保护的限制,以了解如何在配置文件中保护密码。
  3. 应用程序可以对绑定中的每个资源引用指定容器管理的认证别名。指定为认证别名的值必须是服务器配置中 authData 元素的标识。

    例如,在 ibm-web-bnd.xml 中,可以指定:

    <resource-ref name="java:module/env/cloudant/testDBRef" binding-name="cloudant/testDB">
      <authentication-alias name="cadr"/> 
    </resource-ref>
  4. 如果要在应用程序中使用非受管 Cloudant 资源,那么不需要启用 cloudant-1.0 功能部件,也不需要在 server.xml 中添加任何 cloudantcloudantDatabase 元素。

    使用非受管 Cloudant 时,可直接在应用程序中打包 Cloudant 库,也可在 server.xml 中配置库:

    <library name="cloudantLib"> 
      <fileset dir="${shared.resource.dir}cloudant"
               includes="cloudant-client-2.2.0.jar commons-codec-1.6.jar commons-io-2.4.jar gson-2.2.4.jar"/>
    </library>
    <application ...> 
     <classloader commonLibraryRef="cloudantLib"/>
    	</application>
  5. 如果对 cloudant 配置元素指定 JNDI 名称,那么可插入或查找使用 cloudant 配置元素配置的 ClientBuilder
    <cloudant id="builder" jndiName="cloudant/builder" ... />

    使用 cloudant 配置元素配置的 ClientBuilder 的插入或查找示例。

    import com.cloudant.client.api.*; 
    //...
             
    @Resource(lookup="cloudant/builder", name="cloudant/resRef")
    ClientBuilder builder; 
    
    public void useInjectedBuilder() {       
        CloudantClient client = builder.build(); 
        Database db = client.database("somedb", true);
        // use the db ... 
        // Shutdown the connection manager for this client when you are done with it 
        // note that this renders the CloudantClient instance unusable after this point       
        client.shutdown(); 
    } 
    
    public void useBuilderLookup() throws NamingException { 
        ClientBuilder builder = (ClientBuilder) new InitialContext().lookup("java:comp/env/cloudant/resRef");  
        CloudantClient client = builder.build();
        Database db = client.database("somedb",true);
        // use the db ... 
        // Shutdown the connection manager for this client when you are done with it
        // note that this renders the CloudantClient instance unusable after this point       
        client.shutdown(); 
    }

用于指示主题类型的图标 任务主题

文件名:twlp_couchdb_configdriver.html