How can microservice can talk to other microservice in JHipster

Viewed 4730

I am planning to create a microservice aplication with a dedicated service for dealing with data (mostly a Mongodb based service). I am wondering if there is a way using which my other microservices will be able to communicate with this service to make use of the shared data. Is it possible with JHipster API Gateway ? If not how can I achieve this. I dont want to keep multiple copies of the same data within each microservice.

4 Answers

You can also use Feign clients with JHipster.

Annotate your SpringBootApplication with @EnableFeignClients

...
import org.springframework.cloud.openfeign.EnableFeignClients;
...
@SpringBootApplication
@EnableConfigurationProperties({LiquibaseProperties.class, ApplicationProperties.class})
@EnableDiscoveryClient
@EnableFeignClients
public class MyApp {
    ...
}

Create a Feign client in your microservice

...
import org.springframework.cloud.openfeign.FeignClient;
...
@FeignClient("another-service")
public interface AnotherClient {

    @RequestMapping(method = RequestMethod.GET, value = "/api/another")
    List<AnotherDTO> getAll();
}

Inject the Feign client with @Autowired and call it. It should be ready to use.

@RestController
@RequestMapping("/api")
public class MyResource {
    ...
    @Autowired
    private AnotherClient anotherClient;
    ...
    @GetMapping("/another")
    @Timed
    public List<AnotherDTO> getAll() {
        log.debug("REST request to get all");
        return anotherClient.getAll();
    }
}

For us, it worked without implementing a ClientHttpRequestInterceptor and setting a JWT token.

you can use below solution : Microservice A (i.e UAA-SERVICE), and Microservice B Microservice B want to connect microservice A and call services with Feign client.

1)This code for Microservice B Client proxy :- @AuthorizedFeignClient(name = "UAA-SERVICE")

@AuthorizedFeignClient(name = "UAA-SERVICE")

public interface UaaServiceClient {

@RequestMapping(method = RequestMethod.GET, path = "api/users")
public List<UserDTO> getUserList();

@RequestMapping(method = RequestMethod.PUT, path = "api/user-info")
public String updateUserInfo(@RequestBody UserDTO userDTO);

}

UAA-SERVICE : find this name with running Application Instances with registry.

2) In Microservice B (application.yml) Increase feign client connection Time Out: feign: client: config: default:
connectTimeout: 10000 readTimeout: 50000

Increase hystrix Thread time out:-

hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 60000 shareSecurityContext: true

3) add @EnableFeignClients in main @SpringBootApplication class. This solution is working fine for me.

Related