Search code examples
javafirebaserestgoogle-cloud-firestoreglassfish

No route to host: firestore.googleapis.com


I have a Jakarta JAX-RS REST API. From one of its endpoint method I need to retrieve some data from Firebase Firestore. This is the endpoint code.

@GET
@Path("/alive")
@Produces("text/plain")
public String alive() throws IOException, ExecutionException, InterruptedException {

    FileInputStream serviceAccount = new FileInputStream("serviceAccountKey.json");
    FirebaseOptions options = new FirebaseOptions.Builder()
            .setCredentials(GoogleCredentials.fromStream(serviceAccount))
            .build();
    FirebaseApp.initializeApp(options);

    Firestore dbFirestore = FirestoreClient.getFirestore();

    DocumentReference docRef = dbFirestore.collection("drivers").document("8QM7cdz8zOoafQSIHFsd");

    // asynchronously retrieve the document
    ApiFuture<DocumentSnapshot> future = docRef.get();
    // block on response
    DocumentSnapshot document = future.get();
    if (document.exists()) {
        return document.getData().toString();
    } else {
        return "No such document!";
    }

}

When I run the configuration (on a Glassfish server) and I call the api I got the error in the image. What can I do?

enter image description here

It seems that it doesn't work only on the server. I tried with a simple application and it worked.


Solution

  • You need to troubleshoot it better :)

    Connect to the server using ssh and check connectivity to the firestore.googleapis.com

    Try things like:

    $ ping firestore.googleapis.com
    PING firestore.googleapis.com (142.251.128.138) 56(84) bytes of data.
    64 bytes from xxx.xxx.net (yyy.yyy.yyy.yyy): icmp_seq=1 ttl=114 time=23.6 ms
    64 bytes from xxx.xxx.net (yyy.yyy.yyy.yyy): icmp_seq=2 ttl=114 time=22.7 ms
    

    Then check port (in case of your pings get blocked):

    $ apt install -y ncat # or nmap sometimes
    $ ncat -zv firestore.googleapis.com 443
    Ncat: Version 7.80 ( https://nmap.org/ncat )
    Ncat: Connected to 142.251.128.138:443.
    Ncat: 0 bytes sent, 0 bytes received in 0.04 seconds.
    

    And finally:

    You are trying to connect using IPv6. Evidence from your error screenshot: evidence from your error screenshot

    Try to ping/ncat with IPv4 142.251.128.138. If it works, the problem seems to be at IPv6 stack.

    In this case, you may disable IPv6 in JVM: -Djava.net.preferIPv4Stack=true

    Or you can disable it at all on the server:

    $ sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
    $ sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1