Netty gRPC Connection Refused in Docker - java

I'm running a Docker container (GPC Spanner) and attempting to create an instance using org.testcontainers.containers (from a Junit BeforeClass). The connection is refused.
io.grpc.netty.shaded.io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/0:0:0:0:0:0:0:1:9010
Cause: java.net.ConnectException: Connection refused
Code:
private static final Integer[] SPANNER_EMULATOR_PORTS = {9010, 9020};
private static final WaitStrategy WAIT_FOR_START_LOG_MESSAGE =
Wait.forLogMessage(".*gRPC server listening.*", 1).withStartupTimeout(Duration.ofSeconds(30));
...
final GenericContainer<?> container =
new GenericContainer<>(DockerImageName.parse(emulatorDockerImage))
.withExposedPorts(SPANNER_EMULATOR_PORTS)
.waitingFor(
new WaitStrategy() {
#Override
public void waitUntilReady(WaitStrategyTarget waitStrategyTarget) {
// do not wait on #start call, so that we can hook up logger and print output
// for errors which we can do only after #start is invoked
}
#Override
public WaitStrategy withStartupTimeout(Duration startupTimeout) {
return this;
}
});
final StringBuilder containerOutput = new StringBuilder();
final long startTime = System.currentTimeMillis();
try {
System.out.println("Running Spanner Emulator Container");
container.start();
// TODO: See if there's a way to print the output as it's happening and not on timeout
recordOutput(containerOutput, startTime, "Container initialized\n");
} catch (Throwable ex) {
throw new RuntimeException(
"Failed to start up Spanner Emulator",
ex);
}
container.followOutput(
outputFrame -> recordOutput(containerOutput, startTime, outputFrame.getUtf8String()));
try {
WAIT_FOR_START_LOG_MESSAGE.waitUntilReady(container);
// we print only errors in the tests
System.err.println(
"It took "
+ (System.currentTimeMillis() - startTime) / 1000
+ " seconds to init Spanner");
} catch (Exception e) {
recordOutput(containerOutput, startTime, "Explicit timeout");
throw new RuntimeException("Spanner initialization timeout\n" + containerOutput, e);
}
SpannerEmulatorContainer spannerEmulatorContainer = new SpannerEmulatorContainer(container);
spannerEmulatorContainer.createSpannerClient(projectId);
return spannerEmulatorContainer;
...
InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
// Create a instance
InstanceInfo instanceInfo =
InstanceInfo.newBuilder(
InstanceId.of(TestConstants.testProjectId, TestConstants.testInstanceId))
.setInstanceConfigId(
InstanceConfigId.of(TestConstants.testProjectId, TestConstants.testRegion))
.setNodeCount(1)
.setDisplayName(TestConstants.testInstanceId)
.build();
// ***** Errors out here ******
OperationFuture<Instance, CreateInstanceMetadata> instanceOperation =
instanceAdminClient.createInstance(instanceInfo);
Std Out:
2022-10-09T01:35:40.244-0700 [DEBUG] [TestEventLogger] It took 3 seconds to init ...
io.grpc.StatusRuntimeException: UNAVAILABLE: io exception
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a0d09b8a15f4 gcr.io/cloud-spanner-emulator/emulator:1.4.6 "./gateway_main --ho…" 9 minutes ago Up 9 minutes 0.0.0.0:55082->9010/tcp, 0.0.0.0:55081->9020/tcp sleepy_poitras
b1315b3c091e testcontainers/ryuk:0.3.0 "/app" 9 minutes ago Up 9 minutes 0.0.0.0:55080->8080/tcp testcontainers-ryuk-c0389f48-9da7-4731-9dc9-f5f03fc050e0
docker logs a0d09b8a15f4
WARNING: proto: file "google/rpc/status.proto" is already registered
previously from: "google.golang.org/genproto/googleapis/rpc/status"
currently from: "unknown"
See https://developers.google.com/protocol-buffers/docs/reference/go/faq#namespace-conflict
WARNING: proto: file "google/rpc/status.proto" has a name conflict over google.rpc.Status
previously from: "google.golang.org/genproto/googleapis/rpc/status"
currently from: "unknown"
See https://developers.google.com/protocol-buffers/docs/reference/go/faq#namespace-conflict
WARNING: proto: message google.rpc.Status is already registered
previously from: "google.golang.org/genproto/googleapis/rpc/status"
currently from: "unknown"
See https://developers.google.com/protocol-buffers/docs/reference/go/faq#namespace-conflict
2022/10/09 08:37:50 gateway.go:140: Cloud Spanner emulator running.
2022/10/09 08:37:50 gateway.go:141: REST server listening at 0.0.0.0:9020
2022/10/09 08:37:50 gateway.go:142: gRPC server listening at 0.0.0.0:9010
docker logs b1315b3c091e
2022/10/09 08:37:49 Pinging Docker...
2022/10/09 08:37:49 Docker daemon is available!
2022/10/09 08:37:49 Starting on port 8080...
2022/10/09 08:37:49 Started!
2022/10/09 08:37:49 Connected
2022/10/09 08:37:49 Adding {"label":{"org.testcontainers.sessionId=c0389f48-9da7-4731-9dc9-f5f03fc050e0":true,"org.testcontainers=true":true}}

The problem was that I wasn't using the randomized port when making the connection from the test. The actual exposed port is different from the one it's listening to in the container.
String address = container.getHost();
Integer port = container.getFirstMappedPort();

Related

Connection to localhost failed - ActiveMQ in MacOs

I started a project in java with ActiveMQ 5.17.0.
Therefore I have downloaded ActiveMQ and opened my terminal from the folder bin and execute ./activemq start.
I received:
INFO: Loading '/Users/NAME/Downloads/apache-activemq-5.17.0//bin/env'
INFO: Using java '/usr/bin/java'
INFO: Starting - inspect logfiles specified in logging.properties and log4j.properties to get details
./activemq: line 343: [: : integer expression expected
INFO: pidfile created : '/Users/NAME/Downloads/apache-activemq-5.17.0//data/activemq.pid' (pid '3084')
As I have seen in other posts, this is the expected result.
A part of my Java Code is:
public static void main(String[] args) {
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:8161"); // ActiveMQ-specific
Connection con = null;
try {
con = factory.createConnection();
Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE); // non-transacted session
Queue queue = session.createQueue("test.queue"); // only specifies queue name
MessageProducer producer = session.createProducer(queue);
Message msg = session.createTextMessage("hello queue"); // text message
producer.send(msg);
} catch (JMSException e) {
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close(); // free all resources
} catch (JMSException e) { /* Ignore */ }
}
}
}
As soon as I try to open the website https://localhost:8161/admin/admin I receive the response that Safari, Chrome, and Firefox do not get connection to localhost.
Anybody any suggestions what to do?
I have already tried to download again, different browsers, start and stop the server several times.
The URL for the admin UI should be http: http://localhost:8161/admin or http://127.0.0.1:8161/admin/
The script activemq fails parsing the output of java -version. Maybe set the line VERSION= to your java version (e.g. VERSION=17 for java 17) to check if that is the issue.
Your code sample shows that you are attempting to send messages to the Web UI URL vs the JMS protocol port.
Try changing your code to use port '61616':
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616); // ActiveMQ-specific
For the web browser access, try starting with the non-SSL url: http://localhost:8161/admin/admin instead of 'https'.

Cannot access port of Jetty Server

I have the problem that I get a 504 if I try to access the Jetty Server on my private server. The exact message is "ReadResponse() failed: The server did not return a complete response for this request. Server returned 0 bytes.".
I create the server with
ContextHandler context = new ContextHandler("/");
SessionHandler sessions = new SessionHandler(new HashSessionManager());
sessions.setHandler(new WebHandler(this));
context.setHandler(sessions);
// Setup the server
final Server server = new Server(getConfig().getInt("server.port"));
server.setSessionIdManager(new HashSessionIdManager());
server.setHandler(sessions);
server.setStopAtShutdown(true);
// Start listening
getProxy().getScheduler().runAsync(this, new Runnable() {
#Override
public void run() {
try {
server.start();
getLogger().warning("Webserver started on URI: " + server.getURI() + ", state: " + server.getState());
} catch (Exception e) {
getLogger().warning("Unable to bind web server to port.");
e.printStackTrace();
}
}
});
If I start the server on localhost all things work well, but if I put it on my private Hetzner server (Debian 10) I get the error I've written above. The firewall is set to accept all ports. If I create a tunnel to my server via ssh the site loads infinitely long without showing a result.
I hope you can help me :)
Edit: I saw that he tried to connect via IPv6. I forced it to use IPv4, but same result. Here's the netstat output:
tcp6 0 0 :::8092 :::* LISTEN 4581/java

Connection refused: localhost/0:0:0:0:0:0:0:1:9000 with gRPC

I creating a gRPC server but everything seems to run okay but the server never starts up on the specifies port and application is throwing no errors. But when I test with telnet on that specific port, I get this from terminal
isaack$ telnet localhost 9000
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host
Below is my code to create the server (NB: All the services are generated okay with proto and the generated code has no errors)
import java.io.File;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.ServerInterceptors;
import io.grpc.ServerServiceDefinition;
public class EmployeeServiceServer {
private Server server;
public static void main(String[] args) {
try {
EmployeeServiceServer service = new EmployeeServiceServer();
service.start();
} catch (Exception e) {
System.err.println(e);
}
}
private void start() throws InterruptedException {
File certificate = new File("/Users/i/certificates/cert.pem");
File key = new File("/Users/i/certificates/key.pem");
final int port = 9000;
EmployeeService employeeService = new EmployeeService();
ServerServiceDefinition serverServiceDefinition = ServerInterceptors.interceptForward(employeeService,
new HeaderServerInterceptor());
server = ServerBuilder.forPort(port).useTransportSecurity(certificate, key).addService(serverServiceDefinition)
.build();
System.out.println("Listening on Port " + port);
Runtime.getRuntime().addShutdownHook(new Thread() {
#Override
public void run() {
System.out.println("Shuttin Down Server");
EmployeeServiceServer.this.stop();
}
});
server.awaitTermination();
}
private void stop() {
if (server != null) {
server.isShutdown();
}
}
}
Below is the log from but when I ping it, I get nothing.
Listening on Port 9000
My client is throwing this error as well:
Exception in thread "main" io.grpc.StatusRuntimeException: UNAVAILABLE: io exception
at io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:233)
at io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:214)
at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:139)
at com.base.services.EmployeeServiceGrpc$EmployeeServiceBlockingStub.getBadgebyNumber(EmployeeServiceGrpc.java:373)
at com.base.client.Client.sendMetaData(Client.java:66)
at com.base.client.Client.main(Client.java:37)
Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/0:0:0:0:0:0:0:1:9000
You may want to start() your server, as build suggests:
Builds a server using the given parameters.
The returned service will not been started or be bound a port. You will need to start it with Server.start().
Perhaps that server.awaitTermination(); line could become
server.start().awaitTermination();
though I am not entirely sure.
I have faced the same error. The reason being grpc server didn't start.
Server server = ServerBuilder.forPort(port).useTransportSecurity(certificate, key).addService(serverServiceDefinition)
.build().start();
I had written start method as chain to build.
To make it work i had to call start() method separately.
server.start();
This solved the error for me.
PS: I'm writing this answer as the above solution didn't clarify much and had to research alot before finding the solution. Hope this will be helpful for other developers.

RMI Bind ip at Amazon Server ec2 Wes

I have created a RMI project with server and client. I run the server on my ec2 instance. I had some problems finding out how to run the server property on ec2 in order to accept my windows client. I followed the following steps :
Transfer the files with Filezilla.
Compiled the project as administrator.
I opened the port I still use for my server to my IP address.
The problem I had is that I couldnt connect to my server with the public ip that amazon gives me. So I ran ifconfig and used the private ip.
So inside the code as you will see I used the public amazon ip but when I run it. I run it with the following command
java SkyCorpServer "172.31.31.94" 666 (private ip)(port)
I don't know how but that way the client with the public IP was able to connect.
I have read about the bind and the registry of RMI and how it works (creating a registry and forward it to port. Connect to port and then find the registry and use the Servers stub).
The problem now is that the client connects after 30 secs and when I try to use the remote object (look_op) client crashes with the following message:
java.rmi.ConnectException: Connection refused to host: 172.31.31.94; nested exception is:
java.net.ConnectException: Connection timed out: connect
I have used the default permissions (accept all) because when i tried to do this the Security Manager didn't let me create the registry so I guess that is not the problem (permissions).
Server Code:
private static int port =666;
private static String hostname = "35.167.2xx.xx";
public static void main(String[] args) throws RemoteException {
String bindLocation = "//" + hostname + ":" + port + "/Hello";
System.setProperty("java.rmi.server.hostname",hostname);
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
try { // special exception handler for registry creation
LocateRegistry.createRegistry(port);
System.out.println("java RMI registry created.");
} catch (RemoteException e) {
// do nothing, error means registry already exists
System.out.println("java RMI registry already exists.");
}
SkyCorpServer server = new SkyCorpServer();
try {
Naming.bind(bindLocation, server);
System.out.println("Addition Server is ready at:" + bindLocation);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
System.out.println("Addition Serverfailed: " + e);
}
Server output (running without error)(command i used scroll up) :
java RMI registry created.
SYSTEM: Checking for back-up files. // other staff
SYSTEM: Users file exists. Updating values.
SYSTEM: Checking for back-up files.
SYSTEM: Users file exists. Updating values.
Addition Server is ready at://172.31.31.94:666/Hello
As you can see server listens at 172.31.31.94 port:666
Client Code :
private final static String remoteHostName = "35.167.2xx.xx";
private final static int remotePort = 666;
public SkyCorpClient() throws NotBoundException, MalformedURLException, RemoteException {
String connectLocation = "//" + remoteHostName + ":" + remotePort + "/Hello";
look_op = (ChatInterface) Naming.lookup(connectLocation);
System.out.println("Connecting to client at : " + connectLocation);
}
Client's output (when connected)
Connecting to client at : //35.167.203.111:666/Hello
Connected at : 35.167.203.111
When try to use the remote object look_op :
java.rmi.ConnectException: Connection refused to host: 172.31.31.94; nested exception is:
java.net.ConnectException: Connection timed out: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:130)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:227)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:179)
at com.sun.proxy.$Proxy0.registerUser(Unknown Source)
at SkyCorpClient.actionPerformed(SkyCorpClient.java:206)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
My guess is that it connects to public ip 35.xxx and because the server has created the registry to 172.* I can't use the object. Is there something I am missing?

h2 doesn't start programmatically

I have wrote following code:
private static void startH2(){
Server server = null;
try {
server = Server.createTcpServer("-tcpAllowOthers").start();
Class.forName("org.h2.Driver");
Connection conn = DriverManager.
getConnection("jdbc:h2:tcp://localhost/~/test;MODE=PostgreSQL", "sa", "");
} catch (Exception e) {
LOG.error("Error while initialize", e);
}
System.out.println("finish");
}
public static void main(String [] args){
startH2();
}
I run my main method and see following situation:
Looks like Server.createTcpServer creates new non daemon thread.
but by url localhost:8082 I don't see h2 web console(actual result - ERR_CONNECTION_REFUSED)
How to fix this?
P.S.
I have noticed that by url
http://localhost:9092/
my browser downlods file with strange content:
if to decode this text I see following message:
Version mismatch, driver version is “0” but server version is “15”
I use h2 version 1.4.182
H2 contains multiple servers:
the TCP Server (for H2 JDBC clients),
the Web Server (for browsers, the H2 Console application), and
the PG Server (for PostgreSQL clients).
You have started the TCP Server. If you want to use a browser, you also need to start the Web Server:
private static void startH2(){
Server tcpServer = null;
Server webServer = null;
try {
tcpServer = Server.createTcpServer("-tcpAllowOthers").start();
System.out.println("TCP Server Port: " + tcpServer.getPort());
Class.forName("org.h2.Driver");
Connection conn = DriverManager.
getConnection("jdbc:h2:tcp://localhost/~/test22;MODE=PostgreSQL", "sa", "");
webServer = Server.createWebServer().start();
System.out.println("Web Server (H2Console) Port: " + webServer.getPort());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("finish");
}

Categories

Resources