Spring Boot Could not autowire field Kubernetes SERVICE_HOST

8/16/2017

I am using minikube. I have a service named updatedaddress. I am calling updateaddress service from updatecustomer service using auto injection of kubernetes environment variables .

My Java code looks like this

    @Value("${UPDATEADDRESS_SERVICE_HOST}")
    private String updateAddressHost;

    @Value("${UPDATEADDRESS_SERVICE_PORT}")
    private String updateAddressPort;
    ...
    greeting= restTemplate.getForEntity("http://"+updateAddressHost+":"+updateAddressPort+"/updateaddress", Greeting.class).getBody();

When I execute the following command

kubectl --namespace=default-staging exec updatecustomer-4023824433-r5r19 env

I can see the environment variables UPDATEADDRESS_SERVICE_HOST=10.0.0.180 and UPDATEADDRESS_SERVICE_PORT=80 among many other variables.

When I try building the spring boot service I get the error

 Error creating bean with name 'updateCustomerInfoResilient': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.amwater.UpdateCustomerInfoResilient.updateAddressHost; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'UPDATEADDRESS_SERVICE_HOST' in string value "${UPDATEADDRESS_SERVICE_HOST}"

Any advice would be helpful. Thank you .

-- Att A
kubectl
kubernetes
minikube
spring
spring-boot

1 Answer

8/16/2017

Instated of using server IP and port number, try to use the service DNS name and test the application.

It will be something like this my-svc.my-namespace.svc.cluster.local replace your servicename and namespace.

here is the documentation https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/

-- sfgroups
Source: StackOverflow