【发布时间】:2021-04-05 14:56:18
【问题描述】:
我有 2 个微服务 + 一个在其中注册的 Eureka 服务器。 我做了我能想到的一切,但是当我尝试从管理服务调用登录服务时,我总是得到“连接超时”。
POST http://localhost:9903/login
{
"username":"adm4",
"password":"adm4adm4"
}
我尝试使用 Spring RestTemplate 和 WebClient 以及 Apache HttpClient。 一直以来,流程都到达了 post 方法,我得到了相同的结果。 我想这一定是一些配置问题。
我正在使用所有模块在本地主机上工作。
它真的让我发疯!
请指教。我很感激。
相关信息如下。如果您需要更多信息,请告诉我。
首先您可以看到服务已注册并启动:
接下来是代码:
经理(呼叫)服务: (我之前的所有尝试都在评论中留下了)
@PostMapping("/login")
public void login(@RequestBody LoginRequest loginRequest) throws Exception {
String url = getBaseUrl("bbsim-login-service") + "/api/auth/signin";
/* CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost httpPost = new HttpPost(getBaseUrl("bbsim-login-service") + "/api/auth/signin");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("username", loginRequest.getUsername()));
params.add(new BasicNameValuePair("password", loginRequest.getPassword()));
httpPost.setEntity(new UrlEncodedFormEntity(params));
CloseableHttpResponse response = httpclient.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode());
} finally {
httpclient.close();
}
*/
/* HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
// Connect timeout: time is in milliseconds
clientHttpRequestFactory.setConnectTimeout(30000);
// Read timeout: time is in milliseconds
clientHttpRequestFactory.setReadTimeout(30000);
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
HttpEntity<LoginRequest> request = new HttpEntity<>(loginRequest);
JwtResponse res = restTemplate.postForObject(url, request, JwtResponse.class);
System.out.println(res);
*/
localApiClient
.post()
.uri(url)
.body(Mono.just(loginRequest), LoginRequest.class)
.retrieve()
.bodyToMono(JwtResponse.class)
.block();
}
private String getBaseUrl(String serviceName) {
Application application = eurekaClient.getApplication(serviceName);
InstanceInfo instanceInfo = application.getInstances().get(0);
String hostname = instanceInfo.getHostName();
int port = instanceInfo.getPort();
return "http://" + hostname + ":" + port;
}
application.yml:
server.port: 9903
spring:
application.name: bbsim-manager-service
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URI:http://localhost:8088/eureka}
registryFetchIntervalSeconds: 1
# register-with-eureka: true
# fetch-registry: true
instance:
leaseRenewalIntervalInSeconds: 1
如果我理解得很好,请求根本没有到达登录服务。
登录(调用)服务:
@PostMapping("/signin")
public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest) {
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()));
SecurityContextHolder.getContext().setAuthentication(authentication);
String jwt = jwtUtils.generateJwtToken(authentication);
UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();
List<String> roles = userDetails.getAuthorities().stream()
.map(item -> item.getAuthority())
.collect(Collectors.toList());
return ResponseEntity.ok().body(new JwtResponse(jwt,
userDetails.getId(),
userDetails.getUsername(),
userDetails.getEmail(),
roles));
}
application.yml 文件:
server.port: 9902
spring:
application:
name: bbsim-login-service
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8088/eureka/
registryFetchIntervalSeconds: 1
instance:
leaseRenewalIntervalInSeconds: 1
此外,我尝试了以下方法 - 给了我相同的结果:
curl -d "@data.json" -H "Content-Type: application/json" -X POST http://localhost:9903/login
data.json 包含正文内容。
【问题讨论】:
-
您可以使用
curl访问您的登录服务吗? -
@AbdelghaniRoussi 谢谢 Abdeighani。我试过 curl -d "@data.json" -H "Content-Type: application/json" -X POST localhost:9903/login 并收到:AnnotatedConnectException: Connection timed out: no further information: host.docker.internal/192.168.1.177 :9902","路径":"/login"
-
与
RestTemplate或WebClient无关。那么,当您启动 docker 容器时,您是否暴露了 9902 端口? -
@AbdelghaniRoussi 我必须说这是我的第一个春季项目。虽然我看到这里涉及到了docker(host.docker.internal),但是直接我并没有做任何与docker相关的事情。 yaml文件中的端口定义不是您问题的答案吗?如果没有 - 我该怎么办?
-
这似乎是连接到在 docker 内运行的应用程序的问题。我认为您可能需要在 docker tutorialspoint.com/docker/docker_managing_ports.htm runnable.com/docker/binding-docker-ports 上进行端口转发
标签: spring spring-boot spring-mvc spring-security netflix-eureka