简体   繁体   English

将 Java Google AppEngine 本地标准服务器连接到云数据库 | appengine-api-1.0-sdk-1.9.84.jar | IntelliJ 和云代码

[英]Connect Java Google AppEngine Local Standard Server to Cloud DB | appengine-api-1.0-sdk-1.9.84.jar | IntelliJ & Cloud Code

EDIT2: I have managed to get past the GlobalDatastoreConfig has already been set error. EDIT2:我设法克服了GlobalDatastoreConfig has already been set错误。 I managed to pinpoint all the locations that were getting called before the init function. They were in static space in some weird files.我设法查明了在init function 之前被调用的所有位置。它们在一些奇怪的文件中的 static 空间中。

I have now pointed ALL DatastoreServiceFactory.getDatastoreService() to a new static function I've created in a file called Const.java .我现在已将 ALL DatastoreServiceFactory.getDatastoreService()指向我在名为Const.java

private static boolean hasInit = false;
public static DatastoreService getDatastoreService() {
    if(!hasInit) {
        try {
            CloudDatastoreRemoteServiceConfig config = CloudDatastoreRemoteServiceConfig
                        .builder()
                        .appId(CloudDatastoreRemoteServiceConfig.AppId.create(CloudDatastoreRemoteServiceConfig.AppId.Location.US_CENTRAL, "gcp-project-id"))
                        .build();
            CloudDatastoreRemoteServiceConfig.setConfig(config);
            hasInit = true;
        } catch (Exception ignore) {}
    }
    return DatastoreServiceFactory.getDatastoreService();
}

This returns no errors on the first initialisation.这在第一次初始化时不会返回任何错误。 However, I am getting a new error now!但是,我现在收到一个新错误!

Dec 08, 2022 6:49:56 PM com.google.appengine.api.datastore.dev.LocalDatastoreService init
INFO: Local Datastore initialized: 
    Type: High Replication
    Storage: C:\Users\user\dev\repo\Celbux\core\Funksi179_NSFAS_modules\classes\artifacts\Funksi179_NSFAS_modules_war_exploded\WEB-INF\appengine-generated\local_db.bin
Dec 08, 2022 6:49:56 PM com.google.appengine.api.datastore.dev.LocalDatastoreService load
INFO: Time to load datastore: 20 ms
2022-12-08 18:49:56.757:WARN:oejs.HttpChannel:qtp1681595665-26: handleException / java.io.IOException: com.google.apphosting.api.ApiProxy$CallNotFoundException: Can't make API call urlfetch.Fetch in a thread that is neither the original request thread nor a thread created by ThreadManager
2022-12-08 18:49:56.762:WARN:oejsh.ErrorHandler:qtp1681595665-26: Error page too large: 500 org.apache.jasper.JasperException: com.google.apphosting.api.ApiProxy$RPCFailedException: I/O error

Full stacktrace: https://pastebin.com/YQ2WvqzM完整的堆栈跟踪: https://pastebin.com/YQ2WvqzM

Pretty sure the first of the errors is invoked from this line:很确定第一个错误是从此行调用的:

DatastoreService ds = Const.getDatastoreService();
Key ConstantKey = KeyFactory.createKey("Constants", 1);
Entity Constants1 = ds.get(ConstantKey) // <-- This line.

EDIT1: I am not using Maven. Here are the .jar s I have in WEB-INF/lib EDIT1:我没有使用 Maven。这是我在WEB-INF/lib中的.jar

appengine-api-1.0-sdk-1.9.84.jar
appengine-api-labs.jar
appengine-api-labs-1.9.76.jar
appengine-api-stubs-1.9.76.jar
appengine-gcs-client.jar
appengine-jsr107cache-1.9.76.jar
appengine-mapper.jar
appengine-testing-1.9.76.jar
appengine-tools-sdk-1.9.76.jar
charts4j-1.2.jar
guava-11.0.2.jar
javax.inject-1.jar
json-20190722.jar

Original Question: The company that I'm working at have a legacy GCP codebase written in Java. This codebase uses the appengine-api-1.0-sdk.jar libary.原始问题:我工作的公司有一个用 Java 编写的遗留 GCP 代码库。此代码库使用appengine-api-1.0-sdk.jar库。 Upon running this CloudDatastoreRemoteServiceConfig code in the very first place that our DatastoreService gets initialised, it says that the config has already been set.在我们的 DatastoreService 初始化的第一时间运行此CloudDatastoreRemoteServiceConfig代码时,它表示配置已经设置。

If someone can shed light on how to get this outdated tech connected to the Cloud via localhost , I'll be most grateful!如果有人可以阐明如何通过localhost将这种过时的技术连接到云,我将不胜感激!

web.xml web.xml

    <filter>
        <filter-name>NamespaceFilter</filter-name>
        <filter-class>com.sintellec.funksi.Filterns</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>NamespaceFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Code代码

public class Filterns implements javax.servlet.Filter {

    public void init(FilterConfig filterConfig) {
        try {
            CloudDatastoreRemoteServiceConfig config = CloudDatastoreRemoteServiceConfig
                    .builder()
                    .appId(CloudDatastoreRemoteServiceConfig.AppId.create(CloudDatastoreRemoteServiceConfig.AppId.Location.US_CENTRAL, "gcp-project-id"))
                    .build();
            CloudDatastoreRemoteServiceConfig.setConfig(config);
            DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        } catch (Exception e) {
            System.out.println(e);
            return;
        }

        this.filterConfig = filterConfig;
    }

}

I got this code snippet from here .我从这里得到了这个代码片段。

Was thinking a few ideas:在想一些想法:

  1. Perhaps there's GCP code that's called before our Java code which initialises the Local DB也许在我们初始化本地数据库的 Java 代码之前调用了 GCP 代码
  2. Perhaps I need to set a global environment variable to point this old emulator to a Cloud Configuration instead也许我需要设置一个全局环境变量来将这个旧模拟器指向云配置

Only problem is I have no idea what to do from here, hoping someone has experience on the legacy Java library here.唯一的问题是我不知道从这里做什么,希望有人对这里的旧 Java 库有经验。

To clarify;澄清; I am trying to get this outdated GCP Java codebase ( appengine-api-1.0-sdk.jar ) to connect to Cloud Datastore , NOT use the Local Datastore Emulator.我试图让这个过时的 GCP Java 代码库 ( appengine-api-1.0-sdk.jar ) 连接到云数据存储,而不是使用本地数据存储模拟器。 This is so I can debug multiple applications that all access the same Cloud DB这样我就可以调试所有访问同一个云数据库的多个应用程序

It is very difficult to say especially with that amount of code and we can only guess but, as you indicated, probably some code is initializing your DataStore configuration, probably the SDK itself.很难说,特别是对于那么多的代码,我们只能猜测,但正如您所指出的,可能某些代码正在初始化您的 DataStore 配置,可能是 SDK 本身。 You could try setting a breakpoint in the setConfig method of CloudDatastoreRemoteServiceConfig and analyze the call stack.您可以尝试在CloudDatastoreRemoteServiceConfigsetConfig方法中设置断点并分析调用堆栈。

In any way, one think you could also try is not performing such as initialization in your code, delegating to Application Default Credentials the authentication of your client libraries .无论如何,人们认为您也可以尝试不执行代码中的初始化等操作 ,将客户端库的身份验证委托给应用程序默认凭据。

For local development you have two options to configure such as Application Default Credentials.对于本地开发,您有两个选项可以配置,例如应用程序默认凭据。

On one hand, you can use user credentials , ie, you can use the gcloud CLI to authenticate against GCP with an user with the required permissions to interact with the service, issuing the following command:一方面,您可以使用user credentials ,即,您可以使用gcloud CLI 向具有与服务交互所需权限的用户进行 GCP 身份验证,发出以下命令:

gcloud auth application-default login

Please, don't forget to revoke those credentials when you no longer need them:请不要忘记在您不再需要这些凭据时撤销它们:

gcloud auth application-default revoke

On the other, you can create a service account with the necessary permissions and a corresponding service account key , and download that key, a JSON file, to your local filesystem.另一方面,您可以创建一个具有必要权限和相应服务帐户密钥的服务帐户,然后将该密钥(一个 881569990078588 文件)下载到您的本地文件系统。 See this for instructions specific to DataStore.有关特定于 DataStore 的说明,请参阅内容。 Then, set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the downloaded file with your service account key.然后,使用您的服务帐户密钥将环境变量GOOGLE_APPLICATION_CREDENTIALS设置为下载文件的路径。

Again, a word of caution: take care of the downloaded service account key file and never put it under version control because anyone with that file could assume the permissions granted to the service account.再次提醒一句:保管好下载的服务帐户密钥文件,切勿将其置于版本控制之下,因为拥有该文件的任何人都可以承担授予服务帐户的权限。

You code should work without further problem when running in GCP because probably you will be using a service that supports attaching a service account which means that Application Default Credentials are provided by the GCP services per se .在 GCP 中运行时,您的代码应该可以正常工作,因为您可能会使用支持附加服务帐户的服务,这意味着应用程序默认凭据由 GCP 服务本身提供。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 EAR 应用程序从 AppEngine 迁移到 Google Cloud SDK - Migrating an EAR application from AppEngine to Google Cloud SDK Google AppEngine 部署错误(云错误报告) - Google AppEngine Deploy Error (cloud-error-reporting) 使用 dev_appserver.py 和 google.golang.org/appengine@v1.6.6 连接到谷歌云数据存储 - Connect to Google Cloud Datastore using dev_appserver.py & google.golang.org/appengine@v1.6.6 Google Cloud AppEngine 和 Django:我应该如何设置域名才能正常工作? - Google Cloud AppEngine and Django: how should I set the domain name for have it work? 尝试在 google cloud flex appengine 中安装 wordpress; 在“gcloud app deploy”中出错 - Trying to install wordpress in google cloud flex appengine; getting error in "gcloud app deploy" 无法从 App Engine 标准环境连接到 Google 云 SQL - Cannot connect to Google cloud SQL from App Engine Standard Environment 使用 appengine “django.core.exceptions.ImproperlyConfigured:设置 SECRET_KEY 环境变量”在 Google 云上部署 Django 应用程序时出错 - Error in deploying Django app on Google cloud using appengine "django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable" 为什么我在尝试从本地谷歌云 function 连接到谷歌云 MySQL db 时收到 sqlalchemy.exc.OperationalError? - Why am I getting sqlalchemy.exc.OperationalError when trying to connect to Google Cloud MySQL db from a local google cloud function? Google Cloud SDK 创建 Cloud Run Job - Google Cloud SDK to create a Cloud Run Job Google App Engine:java.lang.ClassCastException:com.google.appengine.api.datastore.Key 无法转换为 java.lang.Integer - Google App Engine: java.lang.ClassCastException: com.google.appengine.api.datastore.Key cannot be cast to java.lang.Integer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM