Gir*_* NJ 6 jhipster microservices
我计划创建一个微服务应用程序,其中包含用于处理数据的专用服务(主要是基于 Mongodb 的服务)。我想知道是否有一种方法可以让我的其他微服务与此服务进行通信以利用共享数据。JHipster API 网关可以吗?如果不是,我怎么能做到这一点。我不想在每个微服务中保留相同数据的多个副本。
您可以将您的微服务注册到同一个注册中心,然后它们可以相互调用。
更新:这是我如何使它工作。在消费数据的微服务中,在 API 调用的 Authorization-header 中使用 RestTemplate 和当前用户的 jwt-token:
@Component
public class AuthenticateClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
String token = SecurityUtils.getCurrentUserJWT();
httpRequest.getHeaders().add("Authorization","Bearer "+token);
return clientHttpRequestExecution.execute( httpRequest, bytes );
}
}
Run Code Online (Sandbox Code Playgroud)
我的自定义 restTemplate 使用 ClientHttpRequestInterceptor 在标头中添加令牌。
@Configuration
public class CustomBean {
@Autowired
AuthenticateClientHttpRequestInterceptor interceptor;
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setInterceptors(Collections.singletonList(interceptor));
return restTemplate;
}
}
Run Code Online (Sandbox Code Playgroud)
在您调用数据的资源控制器中:
@RestController
@RequestMapping("/api")
public class DataResource {
@Autowired
RestTemplate restTemplate;
@PostMapping("/hello")
@Timed
public ResponseEntity<Hello> createHello(@RequestBody Hello Hello) throws URISyntaxException {
//The name your data micro service registrated in the Jhipster Registry
String dataServiceName = "data_micro_service";
URI uri = UriComponentsBuilder.fromUriString("//" + dataServiceName + "/api/datas")
.build()
.toUri();
//call the data microservice apis
List<Data> result = restTemplate.getForObject(uri, Data[].class);
return ResponseEntity.created(new URI("/api/hellos/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
.body(result);
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以将 Feign 客户端与 JHipster 一起使用。
注释你SpringBootApplication的@EnableFeignClients
...
import org.springframework.cloud.openfeign.EnableFeignClients;
...
@SpringBootApplication
@EnableConfigurationProperties({LiquibaseProperties.class, ApplicationProperties.class})
@EnableDiscoveryClient
@EnableFeignClients
public class MyApp {
...
}
Run Code Online (Sandbox Code Playgroud)
在你的微服务中创建一个 Feign 客户端
...
import org.springframework.cloud.openfeign.FeignClient;
...
@FeignClient("another-service")
public interface AnotherClient {
@RequestMapping(method = RequestMethod.GET, value = "/api/another")
List<AnotherDTO> getAll();
}
Run Code Online (Sandbox Code Playgroud)
注入 Feign 客户端@Autowired并调用它。它应该可以使用了。
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
对我们来说,它在没有实现ClientHttpRequestInterceptor和设置 JWT 令牌的情况下工作。
| 归档时间: |
|
| 查看次数: |
4097 次 |
| 最近记录: |