Spring Boot - how to communicate between microservices?

后端 未结 6 2079
逝去的感伤
逝去的感伤 2021-02-07 15:59

I\'m currently working on a Spring Boot microservices project. I have created services and each service is running separately. With this, I need some services to communicate wit

6条回答
  •  天涯浪人
    2021-02-07 16:19

    @Autowired
        private RestTemplate restTemplate;
    
    @Autowired
        private EurekaClient eurekaClient;
    
    
    @RequestMapping("/dashboard/{myself}")
        public EmployeeInfo findme(@PathVariable Long myself) {
            Application application = eurekaClient.getApplication(employeeSearchServiceId);
            InstanceInfo instanceInfo = application.getInstances().get(0);
            String url = "http://" + instanceInfo.getIPAddr() + ":" + instanceInfo.getPort() + "/" + "employee/find/" + myself;
            System.out.println("URL" + url);
            EmployeeInfo emp = restTemplate.getForObject(url, EmployeeInfo.class);
            System.out.println("RESPONSE " + emp);
            return emp;
        }
    

    source: https://dzone.com/articles/microservices-communication-service-to-service

提交回复
热议问题