Spring - Service registration with Eureka ignored by Ribbon - spring

I have a Spring Boot app trying to fetch an instance of a service through Eureka/Ribbon:
#LoadBalanced
#Autowired
RestTemplate restTemplate;
#RequestMapping("/hi")
public String hi(#RequestParam(value="name", defaultValue="superuser") String name) {
AccountRest account = this.restTemplate.getForObject("http://AUTHENTICATION-SERVICE/authservice/v1/accounts/userId/"+name,
AccountRest.class);
return "hi, " + account.getId();
}
I am manually registering the "AUTHENTICATION-SERVICE", note that this service only needs to register itself, it does not need to query eureka:
public class ServiceDiscoveryManager {
private ApplicationInfoManager applicationInfoManager;
private EurekaClient eurekaClient;
public void start() {
MyDataCenterInstanceConfig instanceConfig = new MyDataCenterInstanceConfig();
DefaultEurekaClientConfig clientConfig = new DefaultEurekaClientConfig();
InstanceInfo instanceInfo = new EurekaConfigBasedInstanceInfoProvider(instanceConfig).get();
applicationInfoManager = new ApplicationInfoManager(instanceConfig, instanceInfo);
eurekaClient = new DiscoveryClient(applicationInfoManager, clientConfig);
applicationInfoManager.getInfo();
eurekaClient.getApplications();
instanceInfo.setStatus(InstanceStatus.UP);
}
public void stop() {
eurekaClient.shutdown();
}
}
Spring wiring for the ServiceDiscoveryManager:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="serviceDiscoveryManager" class="com.embotics.authervice.serviceDiscovery.ServiceDiscoveryManager" init-method="start"/>
</beans>
the authentication-service eureka-client.properties file:
eureka.name=AUTHENTICATION-SERVICE
eureka.appGroup=AUTHENTICATION-SERVICE
eureka.vipAddress=http://localhost:9080
eureka.port.enabled=true
eureka.port=9080
eureka.traffic.enabled=true
eureka.preferSameZone=true
eureka.serviceUrl.default=http://localhost:8000/eureka/
eureka.decoderName=JacksonJson
eureka.healthCheckUrl=http://localhost:9080/authservice/health
eureka.healthCheckPath=/authservice/health
The Eureka Dashboard shows my authentication service as registered:
When I try to get an instance of the authentication-service with the load balancer I receive the following exception:
java.lang.IllegalStateException: No instances available for AUTHENTICATION-SERVICE
at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:90) ~[spring-cloud-netflix-core-1.2.5.RELEASE.jar:1.2.5.RELEASE]
I've confirmed that the load balancer is aware of the authentication-service, but the DynamicServerListLoadBalancer has the allServerList property as empty.
I've been playing with this for a few hours, wondering if some configuration is invalid. Thanks for any help.

Related

Connect to hbase from remote machine

I have VM where habse is installed.
IP:192.168.20.10
I want to try to connect to hbase from my desktop:
Here is what I am trying>
public static void main(String[] args) throws IOException {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/hbase-beans.xml", HBaseConnection.class);
context.registerShutdownHook();
UserRepository userRepository = context.getBean(UserRepository.class);
List<User> users = userRepository.findAll();
System.out.println("Number of users = " + users.size());
System.out.println(users);
}
public List<User> findAll() {
return hbaseTemplate.find(tableName, "cfInfo", new RowMapper<User>() {
public User mapRow(Result result, int rowNum) throws Exception {
return new User(Bytes.toString(result.getValue(CF_INFO, qUser)),
Bytes.toString(result.getValue(CF_INFO, qEmail)),
Bytes.toString(result.getValue(CF_INFO, qPassword))
);
}
});
}
And this is my bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:hadoop="http://www.springframework.org/schema/hadoop" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<context:component-scan base-package="com.hbase.dao"/>
<context:component-scan base-package="com.hbase.object"/>
<hadoop:configuration id="hadoopConfiguration">fs.default.name=hdfs://192.168.20.10:9000</hadoop:configuration>
<hadoop:hbase-configuration configuration-ref="hadoopConfiguration" zk-port="2181" zk-quorum="192.168.20.10"></hadoop:hbase-configuration>
<bean id="hbaseTemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">
<property name="configuration" ref="hbaseConfiguration" />
</bean>
</beans>
This code works fine when I run directly in remote machine.When I am running same code from my windows machine by providing ip of hbase .,it doesnot return any value.
Configure your windows machine C:\Windows\System32\drivers\etc\hosts
file to resolve your HBase cluster node details. Recommended Make sure
DNS configuration is right!. i.e. addresses of all
nodes(Regionservers) of HBase Cluster is resolved from windows
machine.

Autowiring a Spring Bean from a JSF Controller

I receive NullPoin Exception while calling any method of a Spring Bean, as it seems it is not injected in the container. And I can' t understand why.
Th particularity is that the Controller is using JSF and the Beans are Spring Bean: may be is this the problem? Or just configuration mistake?
The (simplified) code and config is:
Context.xml (called from root context)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:plugin="http://www.springframework.org/schema/plugin"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/plugin http://www.springframework.org/schema/plugin/spring-plugin.xsd">
<!--===========LANGUAGE_TO_LOCALE SERVICE CONFIG BEGIN===========-->
<bean
id="languagesCountryLocaleHelper"
class="com.i18n.MyControllerHelper"
scope="request" />
</beans>
JSF COntroller:
#RequestScoped
#Named
public class MyController {
#Autowired
private MyControllerHelper helper;
public void doSomething() {
helper.doSomething ();
}
}
MyControllerHelper:
#Component
public class MyControllerHelper {
public void doSomething() {
// do something useful
}
}
So, this is the simplified case.. do you have any idea on where my error can be?
Thank you in advance!
#Autowired
private MyControllerHelper helper = new MyControllerHelper();
change to this
#Autowired
private MyControllerHelper languagesCountryLocaleHelper;
I solved the problem injecting MyControllerHelper through:
helper = AppContext.getBean(MyControllerHelper.class);
After that, the bean is instantiated and injected and at cascade all the other beans after it.
I suppose it was due by the fact as JSF Controller the Controller instance Object was in a different container now automatically aware of Spring Beans.

Set resource annotated field of a class in unit tests

I have a class.
public class Definitions{
#Resource(name="schemas")
private Collection<String> schemas;
}
This class is initialized via spring.
Spring file:test.xml
<util:list id="schemas">
<value>"A"</value>
<value>"b"</value>
</util:list
<bean id="Definitions" />
Is there some way I can insert value to private field schemas(annotated with Resource) in my unit test without using spring. I tried using setting private variable via Reflection but that also did not help(probably due to security restrictions).
Even using spring,
ApplicationContext context = new ClassPathXmlApplicationContext("test.xml");
It is not able to load schemas in Definitions bean. I get "NullPointerException" while accessing schemas.
Add a setter for it:
public class Definitions{
private Collection<String> schemas;
#Resource(name="schemas")
public void setSchemas(Collection<String> schemas) {
this.schemas = schemas;
}
}
This is the principle of dependency injection: you inject dependencies manually, vis constructor or setter injection, in your unit tests.
Try to do the following:
Insert the list in you spring.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<util:list id="listTest">
<value>Valor 1</value>
<value>Valor 2</value>
</util:list>
</beans>
Reference the list in your code:
#Resource(name = "listTest")
private List<String> listTest;
I've tested here and it works fine on Spring 4 and 3, there's no need to implement a setter method.

Inject property to spring bean using annotation

As explained here and here it is quite clear how to do it but still can't seem to make it work.
I simply like to use the #Value annotation in order to inject a property to a spring bean. I created a basic spring MVC project with one controller and one bean.
Here is my application context:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<context:component-scan base-package="me.co.fatsecret" />
<!-- Properties -->
<bean id="props"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:fatProperties.properties" />
</bean>
</beans>
I have one bean called Configuration:
package me.co.fatsecret;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
#Component
public class Configuration {
/*--- Members ---*/
#Value("${api_key}")
protected String API_KEY;
#Value("${api_secret}")
protected String API_SECRET;
#Value("${api_url}")
protected String API_URL;
/*--- Constructors ---*/
public Configuration() {
}
/*--- Getters & Setters ---*/
public String getAPI_KEY() {
return API_KEY;
}
public void setAPI_KEY(String aPI_KEY) {
API_KEY = aPI_KEY;
}
public String getAPI_SECRET() {
return API_SECRET;
}
public void setAPI_SECRET(String aPI_SECRET) {
API_SECRET = aPI_SECRET;
}
public String getAPI_URL() {
return API_URL;
}
public void setAPI_URL(String aPI_URL) {
API_URL = aPI_URL;
}
}
Now I have only one controller, injected with this Configuration class and as I call this controller I see that the values in the Configuration class are not populated right.
My properties file is located under the resources folder (src/main/resources) and is a part of my classpath (done by default since this is a maven project). Here it is:
api_url=http://platform.fatsecret.com/js?
api_key=SomeKey
api_secret=SomeSecret
The file name is fatProperties.properties.
As I debug my server when calling the controller I see that the content of the Configuration class is:
${api_key}
${api_secret}
${api_url}
This is the actual value of the Strings, wich means that the vales from the properties file are not getting injected for some reason.
Am I missing something here?
UPDATE1: I replaced the PropertyPlaceholderConfigurer bean with:
<context:property-placeholder location="classpath:fatProperties.properties"/>
Getting the same result
Ok, got it!
I'm using a spring MVC project, which means I have a separated context for my web layer (the controllers). The "Configuration" bean which hods the properties using the #Value annotation is injected to a controller. My property-placeholder is defined within my root-context hence it cannot be seen from my controller. To resolve the issue I simply added the property-placeholder definition to my DispatcherServlet context and it works like a charm :)
Add this to your application context file:
<context:property-placeholder location="classpath:fatProperties.properties" />
Try
#Value("#{props['api_key']}")
private String apiKey;

spring 3.1 annotated autowiring on standalone application

i wrote this app with eclipse and it works. But when i deploy it as standalone/console application, it cannot find StartApp bean i've injected.
here's the codes:
main app:
#Component
public class StartApp {
#Autowired
private Processor proc;
public StartApp() {
System.out.println("Starting App!");
}
private void say() {
proc.say();
}
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
StartApp app = ctx.getBean(StartApp.class);
app.say();
}
}
service:
#Service
public class Processor {
public Processor() {
System.out.println("Processor initialized!");
}
public void say() {
System.out.println("hello!");
}
}
and applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="test.spring.desktop"/>
</beans>
i did put all spring libraries and logger including slf4j libraries.
and for console command i put these:
java -cp lib/*:lib/spring-3.1/*:test-spring-desktop.jar test.spring.desktop.StartApp
then i got these error message:
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [test.spring.desktop.StartApp] is defined: expected single bean but found 0:
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:271)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1101)
at test.spring.desktop.StartApp.main(StartApp.java:24)
Try to put in applicationCntext.xml this configuration:
<context:annotation-config />

Resources