2
votes

I am new to spring, trying to communicate between two micro services .I created four projects ProjectOne,ProjectTwo,ProjectZuul,ProjectEureka

ProjectOne ,ProjectTwo - microservices ProjectZuul - Zuul Api gateway ProjectEureka - Eureka Server

I am trying to call aget method from ProjectOne to ProjectTwo but always showing java.net.UnknownHostException projecttwo

ProjectOne Files

application.properties

#for refering in zuul
spring.application.name = projectone


#database configuration

spring.datasource.url= jdbc:postgresql://192.168.2.6:5432/cliff_test
spring.datasource.username=postgres
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=create-drop




#Eurekka server configuration



#eurekka client configuration
eureka.client.register-with-eureka = true
eureka.client.fetch-registry = true






  // my port
  server.port =4444

applicaion.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:9999/eureka/

TestController.java

@RestController
public class TestController {


    @Autowired
    UserRepository mRepository;

    @Autowired
    private RestTemplate mRestTemplate;



    @GetMapping(value = "/test/{username}/{lastname}")
    public  String  create(@PathVariable String username,@PathVariable String lastname){

        UserModel mModel = new UserModel(username,lastname);
        mRepository.save(mModel);
       String dataFromSecond = mRestTemplate.getForObject("http://projecttwo/test",String.class);
       // String dataFromSecond = mRestTemplate.getForObject("http://projecttwo/test",String.class);
        System.out.println("Data from :"+dataFromSecond);
        return "hello world";
    }

}

MainClass.Java

@EnableAutoConfiguration
@SpringBootApplication
@EnableEurekaClient
public class ProjectOneApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProjectOneApplication.class, args);
    }


    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

ProjectTwo Files

application.properties

spring.application.name = projecttwo

server.port = 2222


spring.datasource.url= jdbc:postgresql://192.168.2.6:5432/cliff_test
spring.datasource.username=postgres
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=create-drop

#eurekka client configuration
eureka.client.register-with-eureka = true
eureka.client.fetch-registry = true

application.yml

eureka: client: serviceUrl: defaultZone: http://localhost:9999/eureka/

TestController.java

@RestController public class TestController { @GetMapping(value = "/test") public String test(){ System.out.println("Hellow call arrived ............................"); return "Second"; } }

ProjectZuul Files

application.properties

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:9999/eureka/


zuul:
    prefix: /api
    routes:
        projectoneservice:
            path: /projectone/**
            serviceId: projectone
        zuulservice:
            path: /projectzuul/**
            serviceId: projectzuul
        projecttwoservice:
            path: /projecttwo/**
            serviceId: projecttwo

Eureka Server

enter image description here

Thank you for your help.

1

1 Answers

2
votes

I Identified my issue and solved

My application issue is with bean initilization in ProjectOne main class.

I added @LoadBalanced annotation in Bean initilization.

@EnableAutoConfiguration
@SpringBootApplication
@EnableEurekaClient
public class ProjectOneApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProjectOneApplication.class, args);
    }

    @LoadBalanced //adding this line solved the issue
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}