JHipster中微服务如何与其他微服务通信

7
我计划创建一个微服务应用程序,其中包括一个专门处理数据(主要基于Mongodb的服务)的服务。我想知道是否有一种方法使得我的其他微服务能够与这个服务进行通信,以使用共享的数据。在JHipster API网关中是否可能实现? 如果不行,我应该如何实现?我不希望在每个微服务中保留多份相同的数据。

微服务之间的依赖应该尽可能避免,因为它会使您的整体解决方案变得更慢、更弱,这可能意味着您的领域边界是错误的(请参见DDD)。虽然您的问题缺乏细节,但网关会将JWT令牌传递给一个服务,该服务可以将其转发到另一个服务。 - Gaël Marziou
不要使用专用的数据存储。每个微服务都应该拥有自己的持久性。 - Philipp
@GaëlMarziou,如果请求是由批处理调用或直接从服务本身发起而不是从网关发起的呢? - ddsultan
我没有看到任何区别。也许你应该开一个更详细的问题。 - Gaël Marziou
4个回答

7
你也可以在JHipster中使用Feign客户端。
在你的SpringBootApplication上加注@EnableFeignClients。
...
import org.springframework.cloud.openfeign.EnableFeignClients;
...
@SpringBootApplication
@EnableConfigurationProperties({LiquibaseProperties.class, ApplicationProperties.class})
@EnableDiscoveryClient
@EnableFeignClients
public class MyApp {
    ...
}

在您的微服务中创建一个Feign客户端。
...
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令牌也能运行。


6
你可以将你的微服务注册到同一个注册表中,这样它们就可以相互调用。 更新:下面是我如何让它工作的方法。 在使用数据的微服务中,使用RestTemplate并在Authorization头中使用当前用户的jwt-token进行API调用。
@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);
        
        }

}

救命稻草!我已经试图解决这个问题几周了... - David I.

0
你可以使用以下解决方案: 微服务A(即UAA-SERVICE)和微服务B 微服务B想要连接微服务A并使用Feign客户端调用服务。
1)这是微服务B的代码 客户端代理:- @AuthorizedFeignClient(name = "UAA-SERVICE")
@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。 这个解决方案对我很有效。


0
通常微服务之间会相互通信,这也是整个设计的重点。使用 Eureka 服务发现后,您只需通过名称调用微服务,而不是像在没有微服务的情况下使用 FQDN。
例如,您的 book-service 将像这样调用 author-servicehttp://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 文档找到大部分相关内容。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接