Camel SpringBoot rest servlet connection refused (camel 2.22.0) - java

I'm having troubles getting any of the simplest springboot servlet rest api examples to work on my machine. I'm just trying to create the simplest possible test api to practice the framework. I have the following code in my Routes RouteBuilder class:
#Component
public class Routes extends RouteBuilder {
#Override
public void configure() {
restConfiguration()//Bind the api servlet to the localhost port 8080
.component("servlet").host("localhost").port(8080)
.bindingMode(RestBindingMode.auto);
rest("/api")//Log any get requests
.get()
.route().to("log:DEBUG?showBody=true&showHeaders=true");
}
}
However, when I try to invoke this code with curl, I get the following error:
curl -X GET http://localhost:8080/api
curl: (7) Failed to connect to localhost port 8080: Connection refused
I'm using camel 2.22.0 and SpringBoot 2.0.4.RELEASE. I'm running this on Ubuntu 20.04 LTS.
EDIT:
I did the changes suggested, but I still get the same Connection refused by curl. My code now looks like this:
restConfiguration()//Bind the api servlet to the localhost port 8080
.component("servlet").host("localhost").port(8080)//Use camel default context path
.bindingMode(RestBindingMode.auto);
rest("/api")//Log any get requests
.get()
.route().to("log:DEBUG?showBody=true&showHeaders=true");
curl -X GET http://localhost:8080/camel/api -> Connection refused
I also now have the following in my application.yml:
server:
port: 8080 #Specify port for camel servlet
max-http-header-size: 32768 # Maximum size in bytes of the HTTP message header.

By default, Camel uses the context path /camel/*.
So, your curl command should look like this:
curl -X GET http://localhost:8080/camel/api
You can control the context path in the following ways.
With restConfiguration
restConfiguration()//Bind the api servlet to the localhost port 8080
.component("servlet").host("localhost").port(8080)
.contextPath("/test/*")
.bindingMode(RestBindingMode.auto);
With application.properties
camel.component.servlet.mapping.context-path=/test/*
For me, only the latter works.
Something worth mentioning here.
You are using the servlet component in your rest definition. In this case, Camel ignores the port configuration and uses the underlying servlet component. As you are using spring-boot, the tomcat port is being used, which by default happens to be 8080.
If for some reason, you change the tomcat port, your rest service port will change.
For example, if you change the server port in the application.properties.
server.port=8180
Your rest service uses that port, ignoring the definition in the restConfiguration.
curl -X GET http://localhost:8180/camel/api
Rest DSL docs

Related

Configuring server and port for Tomcat Spring Boot application

I am deploying my Spring Boot application via uploading a JAR to Elastic Beanstalk.
I am getting the following error:
*1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.43.15, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "172.31.42.2"
and
I do not understand which configurations are missing. Here it says:
The Tomcat platform uses nginx (the default) or Apache HTTP Server as the reverse proxy to relay requests from port 80 on the instance to your Tomcat web container listening on port 8080. Elastic Beanstalk provides a default proxy configuration that you can extend or override completely with your own configuration.
I feel like this should give me some hint, but I still don't get what exactly I need to change.
Similar issue: 502 bad gateway Elastic Beanstalk Spring Boot
From the suggestions there I tried:
defining server.port=8080 in my application.properties
adding PORT or SERVER_PORT environment properties directly in EB
changing Java to version 8 in pom.xml
changing load balancers ports here in EB to 8080:
I might be changing things to a wrong port, or maybe in an unfortunate combination - but nothing seems to work for me.
I never had to do such things, so for me it's all super unclear and I would be thankful for all kinds of help.

Spring Boot configuration overrides are being ignored

I have written a number of services that communicate via REST API calls. The services can be configured to use HTTP or HTTPS. Any given client has security configuration that defines the connection to a server. "Default" configuration properties are set by values in application.yml and this has worked well up to this point.
I've come to realize, however, that this does not work for a more realistic situation. The problem is that I'm attempting to set specific arguments, like a server host/port when a client is launched, and the values I'm setting are being ignored.
An example:
Service A (client) will communicate with Service B (server) for some purpose.
Service A has security configuration set up like the following:
#Configuration
#EnableConfigurationProperties({ClientConnectionProperties.class,
SecurityCertificateProperties.class})
public class ClientSecurityConfiguration {
...
}
#ConfigurationProperties("client.connection")
public class ClientConnectionProperties {
...
}
#ConfigurationProperties("server.ssl")
public class SecurityCertificateProperties {
...
}
application.yml has defnitions for the various properties that are assigned to values in the configuration objects.
client:
connection:
scheme: https
host: localhost
port: 8443
server:
port: 7081
ssl:
enabled: true
protocol: TLS
trust-store-type: JKS
trust-store: classpath:server.truststore
trust-store-password: <password>
key-store-type: JKS
key-store: classpath:server.keystore
key-store-password: <password>
This is all well and good when both client and server are running on localhost. However, I'm testing running them where they reside on different hosts, so I need to override client.connection.host from localhost to the actual host. I do this by specifying -Dclient.connection.host=<host> as a VM argument when launching the client. Unfortunately, this does not work. The client ends up trying to connect to localhost and fails (for obvious reasons).
How can I get my override values set into these configuration items? Is there a way to defer or delay the "default" loading so that my values take effect? Is there some other technique to use to get them in there?
You can override Spring Boot properties by using Spring arguments instead of JVM arguments.
Example:
java -jar your-app.jar --client.connection.host=<host>
Related question: https://stackoverflow.com/a/37053004/12355118

Windows Server 2012, Apache Tomcat, Spring MVC: Websocket connection blocked for external IP

We've deployed our Spring MVC web application on Windows Server 2012. Our web-app uses Spring Websockets for updates with stomp.js and sock.js.
Our websocket configuration:
#Configuration
#EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
#Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/calcApp");
}
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/add").setAllowedOrigins("*").withSockJS();
}
}
Websocket works on localhost and logs are following:
Opening Web Socket...
Web Socket Opened...
>>> CONNECT
accept-version:1.1,1.0
heart-beat:10000,10000
<<< CONNECTED
version:1.1
heart-beat:0,0
user-name:admin
connected to server undefined
>>> SUBSCRIBE
id:sub-0
destination:/topic/resident
...
Strangely, it doesn't work when I enter external ip, on same machine and browser:
Opening Web Socket...
WebSocket connection to 'ws://192.168.5.50:8080/autopark/add/629/i148hb1c/websocket' failed: WebSocket is closed before the connection is established.
Whoops! Lost connection to undefined
We thought that for external access, there is some firewall and totally disabled it:
But it didn't solve our problem.
How can we solve this issue?
I'm not really sure and not a spring expert.
but it seems you need to call the server by a domain name over its ip addr which is logical.
Since an ip would be used for more than one domain, so it seems the context need to know which context should be called(even if one) in spring context.
in other word, calling the context by ip would confuse the spring context to select/invoke which context/domain, so it refuses the connection.
have atry, binf the 192.168.5.50 to a domain name, then try to call the path using the domain(not ip). Hope it works this way.
The first step in debugging this would be verifying that your application server is actually listening on the external interface.
You can verify what IP your container is bound to by looking for 8080 entries in the output of netstat.
netstat -a -n -o | find "8080"
If you don't see an entry bound to either 0.0.0.0 or the external IP, then we know it is a configuration issue with your application server.
Example for embedded tomcat - How to set a IP address to the tomcat?
Example for standalone tomcat - How do you configure tomcat to bind to a single ip address (localhost) instead of all addresses?
The next step should be verifying an external computer can "see" the port on the external IP. There are various ways to do this, but using the telnet command will suffice.
telnet 192.168.5.50 8080
If this does not work, then we know there is something blocking communication between the two applications
If we get to this point, then there is likely an issue with the configuration of the application itself.

Communication between spring boot dockerized apps

I new using spring boot and docker and I faced a problem running the docker containers.
On debug mode, there is no problem on applications boot, but when I run them as a container, there is something wrong.
For example, I have my server config with all the yml files, also eureka properties.
The config server boot perfectly, but not the eureka server, it must look for it`s configuration to the config server becouse of these:
uri: ${vcap.services.config-service.credentials.uri:http://127.0.0.1:8888}
In the eureka`s log I can found:
Could not locate PropertySource: I/O error on GET request for
"http://127.0.0.1:8888/server-eureka/default":Connection refused;
nested exception is java.net.ConnectException: Connection refused
So I see that eureka cant connect to the config server for a reason I cant understund.
Maybe I miss something in my docker file.
If you are not using docker linked containers you'll have to use only the public ip addresses. Docker will assign every running container an own ip address which is per default not accessible. Only when you start to expose ports there will be an entry to iptables that is linking the hosts public ip address and given port to the internal used port and (dynamically assigned) ip address of the docker container. This is also why 127.0.0.1 does not work cause it would look into the containers local context but tgere the service is not running.

Heroku Java Websocket Endpoint

I am trying to build a chat application.
+Server : I use java websocket to create an endpoint. Here is my endpoint:
#ServerEndpoint("/server")
public class ChatServer {
//My code is ok ! Tested in localhost
}
+Client : I use java swing to create GUI. In my localhost, I use this URL :
ws://localhost:8080/ChatServer-1.0/server
to connect to Server Endpoint and my app run successful.
-> I have deployed my server endpoint to Heroku
My Heroku Server Endpoint
How can my client to connect to server endpoint ?
I have tried to use:
wss://jp-chatting-server.herokuapp.com/server
But it gives a 404 code.
I think you have used instruction for webapp-runner. I'm afraid this solution will not work for websockets. It is because support of websocket is not implemented yeat. Issue with websocket in webapp-runner. I see one of the options in using Tomcat 7 and it's custom implementation. Like in this example.

Categories

Resources