Spring Boot - how to communicate between microservices?

后端 未结 6 2151
天涯浪人
天涯浪人 2021-02-07 15:41

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:36

    @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

提交回复
热议问题