I am using spring boot to listen for messages on my rabbitmq instance. I have it working locally using an application.properties file.
However, when I want to connect to the remote rabbit instance I am getting number format exceptions and unknown host exceptions because of my url.
I've tried setting spring.rabbitmq.host to:
amqp://myurl/dev and myurl/dev and amqp%3A%2F%2Fmyurl%2Fdev
Nothing is working. Any ideas what could be up. I have set my user name and pass for the remote instance as well.
Try removing the amqp:// part. The host property should just be that, the hostname:
spring.rabbitmq.host=myurl
Not sure what the /dev part is. Are you saying that is your virtual-host? If so then just set this property too:
spring.rabbitmq.virtual-host=dev
For username/password, set these properties:
spring.rabbitmq.username=
spring.rabbitmq.password=
A better way to do this is to set
spring.rabbitmq.addresses=amqps://user:password#10.0.0.123/virtualhost
This will get auto-resolved, and correctly set your hostname, password etc
Github issue that fixes this:
https://github.com/spring-projects/spring-boot/issues/6401
Related
We are using grpc spring boot starter on our Java application service in order to establish a connection to another 'server' service, so I define in the application.properties the following address:
grpc.client.name.address=static://service-name:port
When tried to connect it I got the following error message:
StatusRuntimeException: UNAVAILABLE: io exception
So I know for sure I have a connectivity issue. On the documentation it says regarding the static scheme:
A simple static list of IPs (both v4 and v6), that can be use connect to the server
So I guess this is not what I need to use. It seems the best option in my case is using the discovery scheme, but it doesn't contains any port...
What is the right scheme configuration I need to use to set the server address?
Wanted to share the resolution for this very annoying issue for those who will encounter the same problem in the future like I did.
So first, the scheme needs to be set indeed of dns type, like the following: grpc.client._name_.address=dns:///<service-name>:26502
but this alone is not enough. (at least in my case) The server was configured to run in PLAINTEXT, while my client, by default, was configured to run with TLS mode, so it must be set with grpc.client.__name__.negotiationType=PLAINTEXT property.
See the following documentation for further information
It caused by gRPC can't resolve addresss service-name:port;
If you use static, the value must be ip:port; The service-name need to be resolved as ip address;
If you are using register center like consul or eureka etc., you should use discovery:///service-name without specify port.
If you didn't use register center, only end to end with server, replace service-name as a ip like 127.0.0.1 which belong to server;
Or modify host config for parse service-name like below, the file on Linux is /etc/hosts
127.0.0.1 service-name
Hello i have an issue after i have added spring mail to my application.These are the app.propreties where the issue is.
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.auth=true
server.port=${PORT}
this is how i am binding it in the EmailConfig class
#Value("${spring.mail.port}")
private int port;
also this is the mail i am using
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
Every time i start the app i receive this error:
Description:
Failed to bind properties under 'server.port' to java.lang.Integer:
Property: server.port
Value: ${PORT}
Origin: class path resource [application.properties]:20:13
Reason: failed to convert java.lang.String to java.lang.Integer
Action:
Update your application's configuration
For some reason it seems that i cannot make requests on basic 8080 port.So i have modified the server.port to this server.port=${PORT:9191} and it works but i get some requests issue with some of my pages for some reason.
Thing is , i have this same configuration on another application and it works without me having to modify the port to a different number.Why is this happening? i would like to remain on standard port.
Any help would be greatly appreciated !
If you want to remain on standard port, remove the line server.port from your application.properties (so it runs on 8080).
server.port - used to configure the application to run in different port other than standard port.
spring.mail.port - used to configure smtp server port. If your SMTP server is using standard port, you don't need to configure this property.
The other application that has the same configuration would have the PORT value set in either environment variables or system properties. It's not possible to run the application without assigning any value to PORT variable as it is a configuration parameter.
app.properties have server.port property while in the code you try to access ${spring.mail.port} which is empty.
I have created a simple Spring cloud config server application and corresponding 'client' application. The config server reads config files from a git repo and the client app pulls them from the config server. It works with config files stored in a local git repo. I now want to connect to a remote Bitbucket server.
I have seen a few examples using username and password hardcoded, but I don't want to hardcode these. I'd like to use SSH keys.
I have found this example of using ssh keys in the docs.
I have a public key added to my repo in Bitbucket and i have the following application.yml file:
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: git#mybitbucketserver/ubp/config-server.git
cloneOnStart: true
ignoreLocalSshSettings: true
hostKey: publicKeyLabel
hostKeyAlgorithm: ssh-rsa
privateKey: "-----BEGIN RSA PRIVATE KEY-----MIIEoAIBAAKmJyC-----END RSA PRIVATE KEY-----"
Where publicKeyLabel is the name of the label added to the repo on bitbucket. When I run this config server application i get the following error:
org.eclipse.jgit.api.errors.InvalidRemoteException: Invalid remote: origin
With a nested error of:
Caused by: org.eclipse.jgit.errors.NoRemoteRepositoryException: git#mybitbucketserver/ubp/config-server.git: not found.
I have double checked url. The public key attached to the repo has worked with the private key in my code in connecting from Openshift so i know it works (i have remove some of the key for this example).
Any suggestions of what i can try? Seen so few examples of people using SSH for this, everyone seems to hardcode the PW :|
If there is a better way of doing this I am all ears? My end goal would ideally be not to have the private key etc in code as well but I was hoping to get this working first then figure out where I can store the PK.
Please check and see below. I hope these help you isolate and achieve your objectives of the setup 1 at a time:
Let's make your current setup work first and establish connection between your config server and bitbucket before we move on to loading the SSH key externally (not hard coded).
privateKey is a multiline value. Hence, it has to be registered as multiline in YAML. As you saw in the example, it was using "|" which indicate a block-style indicator. You can also read more here for more details.
This shouldn't be an issue, but just in case, consider writing the git URI explicitly with SSH protocol such as: ssh://git#mybitbucketserver/ubp/config-server.git
If (and only if) after that change you come across another error e.g. "not a valid private key", you may refer on this later.
Avoiding writing the privateKey in the YAML. These are some suggestions I can share:
1 way is to register the key on the local SSH e.g. /root/.ssh/id_rsa and then set ignoreLocalSshSettings: false
Another way is something like this: privateKey: ${git.repository.key}. We'll have to define git.repository.key externally in options we can follow from here.
I'm going to use Amazon RDS for my Spring boot web application
So, I created RDS with 'mainrds' instance id Screen Capture. then I configured application.properties like this:
#RDS
cloud.aws.rds.mainrds
cloud.aws.rds.mainrds.username=dbadmin
cloud.aws.rds.mainrds.password=password
cloud.aws.rds.mainrds.readReplicaSupport=false
cloud.aws.rds.mainrds.databasename=maindata
When I run the application, I encountered these error messages:
Caused by: java.lang.IllegalStateException: No database instance with id:'mainrds' found. Please specify a valid db instance
at org.springframework.cloud.aws.jdbc.rds.AmazonRdsDataSourceFactoryBean.getDbInstance(AmazonRdsDataSourceFactoryBean.java:170)
at org.springframework.cloud.aws.jdbc.rds.AmazonRdsDataSourceFactoryBean.createDataSourceInstance(AmazonRdsDataSourceFactoryBean.java:151)
at org.springframework.cloud.aws.jdbc.rds.AmazonRdsDataSourceFactoryBean.createInstance(AmazonRdsDataSourceFactoryBean.java:129)
at org.springframework.cloud.aws.jdbc.rds.AmazonRdsDataSourceFactoryBean.createInstance(AmazonRdsDataSourceFactoryBean.java:45)
at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:134)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
... 178 common frames omitted
I can't figure out what the problem is. what is the missing point?
I had the same problem.
spring-cloud-aws probably can't resolve the proper aws region on it's own. If you're only on one region, insert this into your properties file with the region where your RDS instance is placed.
cloud.aws.region.static=us-east-1
This solved the problem for me.
You could also try with:
cloud.aws.region.auto=true
to see if the EC2 meta data service can resolve the correct region.
It is also possible to connect via. normal spring-boot settings, where you simply specify the RDS instance endpoint in the url of the datasource like so: (for a postgresql instance)
spring.datasource.url=jdbc:postgresql://endpoint.of.rds.instance.amazonaws.com:5432/dbname
and of course supply the datasource.platform, datasource.username, datasource.password, spring.database.driverClassName and spring.jpa.database in your application.properties aswell.
The accepted answer did not work for me.
After some debugging I found the application.properties as given in the question is not correct:
#RDS
cloud.aws.rds.mainrds #this line has to be deleted
cloud.aws.rds.mainrds.username=dbadmin
cloud.aws.rds.mainrds.password=password
cloud.aws.rds.mainrds.readReplicaSupport=false
cloud.aws.rds.mainrds.databasename=maindata
After deleting the line cloud.aws.rds.mainrds everything worked just fine.
Using spring.datasource.* is not an option because we are using IAM authentication for RDS and therefore the password has to be regenerated every 15 minutes.
I'm in need to develop a java library which allows a traffic to be directed via proxy only for specified hosts.
The library is almost ready and working, but there is problem with resolving dns addresses via proxy.
In short words I extended CustomProxySelector class which has following logic:
public class CustomProxySelector extends ProxySelector {
public List<Proxy> select(URI uri) {
if (customProxyDefinedFor(uri)) {
return getCustomProxyFor(uri);
} else {
// use direct connection
}
}
}
All works fine if local dns can resolve host given as "uri" parameter (for example if I want stackoverflow.com to go via proxy it will work because my local dns can resolve stackoverflow.com).
The problem comes when there is a host which is not known to my local dns. For example the dns behind proxy knows how to resolve address like "host1.private.dmz" because this is special host only known behind proxy (the proxy acts really as reverse proxy here). JVM seems to first try to resolve "host1.private.dmz" to ip, and when it fails it ends with folowing stacktrace:
Caused by: java.net.UnknownHostException: host1.private.dmz
at java.net.InetAddress.getAllByName0(InetAddress.java:1259)
at java.net.InetAddress.getAllByName(InetAddress.java:1171)
at java.net.InetAddress.getAllByName(InetAddress.java:1105)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:247)
(...)
Because it fails to resolve the ip, my Custom ProxySelector is never used. Is there any option to force java not to resolve ip via localdns but via proxy?
If I give the ip address of host1.private.dmz (for example 10.100.12.13) all works ok. The communication is directed to my Custom Proxy Selector and the traffic goes via custom proxy without problem.
I solved this issue. The important thing to fix this problem is correct understanding that the problem does not lay in jvm but in application. Jvm does not try to resolve host1.private.dmz before calling custom proxy selector, it is the application itself.
If we have a look at last line of the stacktrace you can see that exception comes from mysql jdbc driver, so it is mysql driver who trys to resolve host1.private.dmz to IP address, before actually opening connection to that host. Therefore because application does not open a connection (because exception occurs when application trys to resolve dns), no proxy selector is called ("no connection" == "no proxy selector").
What can we do in such case?
If it is you who writes the application, simply don't resolve the IP by calling InetAddress.getAllByName() and directly open connection to host domain name (host1.private.dmz). If for some reason you need an IP than handle the exception (in case of exception try to open connection without resolving the address). If still this is not acceptable for you there is one more option. You can instruct jvm to use extra DNS server which is able to resolve IP of this domain. You can do this by setting following properties:
System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun");
System.setProperty("sun.net.spi.nameservice.nameservers", "10.200.2.3,100.40.70.5);
This should set extra dns server for your application.
There can however be one more problematic situation. An attempt to resolve domain name to ip might take place before you have the chance to set up extra dns servers. For example you might be running web application on Tomcat with database connection pool configured in Tomcat's context. In such case the exception "UnknownHostException" can happen before you set up extra dnses. In such case you can run this application by "proxifying it". Strictly in java you can do this by using jProxyLoader library (http://jproxyloader.sourceforge.net) , for example by running the application with following parameters:
-Djava.system.class.loader=net.sf.jproxyloader.JProxyLoader -DjplDnsServers=10.0.1.18
Above example will set up 10.0.1.18 as extra dns server (which is able to resolve the uknown domain name) at application startup. Thanks to this extra dns will already be available when application boots up.
You understand more about this problem, by having a look at jProxyLoader troubleshooting page: http://jproxyloader.sourceforge.net/troubleshooting.html