Spring configuration to access Openshift elasticsearch - java

I'm implementig a page that has a search field and is calling a elasticsearch service using spring.
The connection to the elasticsearch engine was done by using the spring bean provided by spring-elasticsearch (https://github.com/dadoonet/spring-elasticsearch):
<elasticsearch:client id="esClient" esNodes="localhost:9300" />
This worked well but I want to move my engine to a different server, so to development and test I created an app in Openshift with elasticsearch using this https://github.com/caruccio/openshift-elasticsearch and change the transported client esNode link. The problem here is always giving me errors like this one (or similar to this one):
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'indexController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.projectx.standard.services.common.SearchEngineService com.projectx.standard.app.controler.common.IndexController.searchEngineService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'searchEngineService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.elasticsearch.client.Client com.projectx.standard.services.common.implementation.ElasticSearchServiceImpl.esClient; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'esClient': Invocation of init method failed; nested exception is java.lang.NumberFormatException: For input string: "//elastic-projectxserver.rhcloud.com/"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
I think the problem is in the link because I'm using the http://... in the esNodes field, but I'm not sure.
Someone know how to put the application to connect to an engine in the openshift?
btw, I found this https://github.com/spring-projects/spring-data-elasticsearch that looks interesting but I don't know if this will help solve my problem or is worth to change!
Thanks in advance

This will not work as the https://github.com/dadoonet/spring-elasticsearch code is expecting the configuration in the form of host:port. Looking at the exception, you are providing as host as http://elastic-projectxserver.rhcloud.com/. The library will split it by : and you will //elastic-projectxserver.rhcloud.com/ as port. This is the reason you are getting NumberFormatException as port should be an int. In your client configuration, try using elastic-projectxserver.rhcloud.com:80.
Also, I would recommend you use https://github.com/spring-projects/spring-data-elasticsearch as that is official Spring ElasticSearch library.

Related

Spring Cloud Gateway serving both Frontend service(s) as well as Backend service(s) in an OAuth2 Spring security setup

I am trying to develop a web application as per below micro-service architecture with Spring Cloud, Spring Boot, OAuth2 Spring Security:
Github repo: https://github.com/vedrm/ms_arch-spring_cloud_gateway-springboot_services-oauth2
Here, I am able to establish Browser client (desktop) to Gateway (using TokenRelayGatewayFilterFactory for oauth2) to project API service flow (authorization code flow in OAuth2 terms) and get the json response.
Similarly for UI service (a thymeleaf app), I am able to get html response for the API requests but not for the static resource requests which are triggered after loading of former returned html in browser. Though, sometimes only one or two static resources (png/js) load. Below are the points:
In Gateway, I have configured a route to check "/**" path and forward to UI service url.
Oauth2 flow is getting triggered for each static resource request, which ultimately ends up in "/" or "/login?error" request but results in cancelled status. First, how to solve this? Second, is there a way to suppress oauth2 flow for these static resources here (in Gateway)? (similar to the way we ignore security for the static resources by customizing WebSecurity by overriding WebSecurityConfigurerAdapter in a regular thymeleaf app??)
Is this a valid approach - keeping UI service behind a Gateway like this? Also, considering this would later be replaced by a React App.
Another challenge I am facing is to call Project service from Front-end via Gateway using client credentials flow. I have kept the code tried out so far, commented in the repo. The Front-end app always failed to start with below message:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'navController': Unsatisfied dependency expressed through field 'webClient'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webClient' defined in class path resource [com/sample/portal/config/WebClientConfig.class]: Unsatisfied dependency expressed through method 'webClient' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authorizedClientManager' defined in class path resource [com/sample/portal/config/WebClientConfig.class]: Unsatisfied dependency expressed through method 'authorizedClientManager' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.oauth2.client.registration.ClientRegistrationRepository' available: expected at least 1 bean which qualifies as autowire candidate.
I have not been able to resolve this by going through various articles/posts over SO/web yet and requiring help here. Also, is client credentials a right choice for this flow or should be something else?

It is possible to use Spring Cloud Zuul without SpringBoot?

I want to use Spring Cloud Zuul without SpringBoot. We have an existing project with Spring but I'm having trouble to configure Zuul without SpringBootApplication.
When I use #EnableZuulProxy annotation, I get the following error:
Caused by:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name
'org.springframework.cloud.netflix.zuul.ZuulProxyConfiguration':
Unsatisfied dependency expressed through field 'discovery'; nested
exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
'org.springframework.cloud.client.discovery.DiscoveryClient'
available: expected at least 1 bean which qualifies as autowire
candidate. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
No qualifying bean of type
'org.springframework.cloud.client.discovery.DiscoveryClient' available
And why it needs a discovery client?
Can I use spring-cloud-starter-zuul dependency without spring-boot?
No, but you can use zuul directly via the Netflix OSS bits https://github.com/Netflix/zuul/wiki/Getting-Started

MetadataMBeanInfoAssembler does not support JDK dynamic proxies

Recently I added JMX support to my Spring web application.
This works fine for beans with #Component, however, for some services there seems to be a problem when adding #ManagedResource
I have a spring service annotated with #Service method.
I'm trying to add JMX to that service using #ManagedResource but it seems like spring is having troubles registering the beans:
org.springframework.beans.factory.BeanCreationException:
Caused by: org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'mbeanExporter': Invocation of init method
failed; nested exception is
org.springframework.jmx.export.UnableToRegisterMBeanException: Unable
to register MBean [...] with key 'appAnalysisServiceImpl'; nested
exception is org.springframework.jmx.export.MBeanExportException:
Could not create ModelMBean for managed resource [
l#6142152] with key 'myservice'; nested exception is java.lang.IllegalArgumentException: MetadataMBeanInfoAssembler does
not support JDK dynamic proxies - export the target beans directly or
use CGLIB proxies instead
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
From the exception message it seems like spring requires a special exporter for this kind of class.
How do i export the bean directly without writing my own exporter ?
If your service uses #Transactional then Spring won't be able to create a ModelBean since the service is proxied.

Disable Spring from Starting up after BeanInstantiationException (Tomcat)

for the project I'm currently working at there is the requirement to disable the webapp context to be started when spring fails to initialize some beans (that call webservices during initialization and therefore are likely to crash then).
However, when a bean throws any exception during initialization, it looks like this:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'latestAdsRepository' defined in file [...]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [...]: Constructor threw exception; nested exception is java.io.IOException: SHUT DOWN NOW!!
...
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [...]: Constructor threw exception; nested exception is java.io.IOException: SHUT DOWN NOW!!
however the context starts and all references to the bean that was not able to start are null. Needless to say, this causes various bad Nullpointer Exceptions all over the place. Especially because this bean is very important for the webapp.
So I need a way to explicitly tell Spring that this webapp has failed to start if a bean could not be initialized. System.exit(1) however is not an option, because there are also other webapps on this Tomcat server.
Any ideas?
As far as I know, by default, the context startup fails when Spring fails to instantiate a bean, if the exception is propagated through the startup listener to the servlet container.
You can use the #Required annotation to mark a certain injection point as mandatory.
How are you loading the Spring context?
Sounds like you might be doing it manually (i.e. you are calling new ApplicationContext() somewhere), instead you should use the ContextLoaderListener so that your context is loaded automatically upon application startup (and shutdown during application shutdown). Exceptions during the Spring context startup will propogate to failing the web application context startup.

Weblogic 10.3.1 gives 'Dependency injection failure' warning when publishing code

Weblogic 10.3.1 gives me a "Dependency injection failure" when I publish my code; the publish itself succeeds. I am working via Eclipse.
Basically I publish an ear with a web service aaa.MyWebServicePort that has a bean bbb.MyBean declared as local variable with the ejb 3.0 #EJB annotation. bbb.MyBean is also in the ear as well as a client project with the interfaces defined.
After publishing the web service works on the appserver and calls bbb.MyBean. So why the warning?
Details of the warning in the console window of Eclipse: Error creating bean with name 'aaa.MyWebServicePort': Initialization of bean failed; nested exception is [...] BeanCreationException: Dependency injection failure: can't find the bean definition about class interface bbb.MyBean; nested exception is [...] NoSuchBeanDefinitionException: No unique bean of type bbb.MyBean is defined: No beans of type bbb.MyBean;
There may be a couple of issues at work here. I'm getting a very similar error in just a straight servlet that is trying to load an ejb that isn't packaged in the same ear, I'm still trying to track that down. I also know that there is a difficulty when loading EJB v2.x that is supposed to be fixed in Weblogic 10.3.3.
If you find anything please let me know, and I'll do the same. One thing I have noticed is that if I load the EJB via initialContext.lookup, it works fine.
YMMV

Categories