我计划创建一个微服务应用程序,其中包括一个专门处理数据(主要基于Mongodb的服务)的服务。我想知道是否有一种方法使得我的其他微服务能够与这个服务进行通信,以使用共享的数据。在JHipster API网关中是否可能实现?
如果不行,我应该如何实现?我不希望在每个微服务中保留多份相同的数据。
...
import org.springframework.cloud.openfeign.EnableFeignClients;
...
@SpringBootApplication
@EnableConfigurationProperties({LiquibaseProperties.class, ApplicationProperties.class})
@EnableDiscoveryClient
@EnableFeignClients
public class MyApp {
...
}
...
import org.springframework.cloud.openfeign.FeignClient;
...
@FeignClient("another-service")
public interface AnotherClient {
@RequestMapping(method = RequestMethod.GET, value = "/api/another")
List<AnotherDTO> getAll();
}
使用@Autowired
为Feign客户端注入并调用即可。它应该准备就绪。
@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();
}
}
对于我们来说,没有实现ClientHttpRequestInterceptor
和设置JWT令牌也能运行。
@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 );
}
}
我自定义了一个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;
}
}
@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);
}
}
@AuthorizedFeignClient(name = "UAA-SERVICE")
公共接口 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: 在注册表中运行应用程序实例时查找此名称。
2)在微服务B中(application.yml)
增加Feign客户端连接超时时间:
feign:
client:
config:
default:
connectTimeout:10000
readTimeout:50000
增加Hystrix线程超时时间:-
hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds:60000 shareSecurityContext:true
3)在主@SpringBootApplication类中添加@EnableFeignClients。 这个解决方案对我很有效。
book-service
将像这样调用 author-service
:
http://author-service/authors
完整示例请参见 https://spring.io/blog/2015/01/20/microservice-registration-and-discovery-with-spring-cloud-and-netflix-s-eureka
请注意,JHipster 是基于 Spring Cloud 的一种有主见的框架,因此您可以通过搜索 Spring 文档找到大部分相关内容。