1.使用restTemplate 常用语
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return new RestTemplate().postForObject(
requestUrl,
new HttpEntity<>(JSON.toJSONString(xxx), headers),
xxxx.class
);2. 通过注册中心拿到服务的信息(是所有的实例), 再去发起调用
ServiceInstance serviceInstance = loadBalancerClient.choose(
CommonConstant.AUTHORITY_CENTER_SERVICE_ID
);
log.info("Nacos Client Info: [{}], [{}], [{}]",
serviceInstance.getServiceId(), serviceInstance.getInstanceId(),
JSON.toJSONString(serviceInstance.getMetadata()));
String requestUrl = String.format(
"http://%s:%s/ecommerce-authority-center/authority/token",
serviceInstance.getHost(),
serviceInstance.getPort()
);
log.info("login request url and body: [{}], [{}]", requestUrl,
JSON.toJSONString(usernameAndPassword));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return new RestTemplate().postForObject(
requestUrl,
new HttpEntity<>(JSON.toJSONString(usernameAndPassword), headers),
JwtToken.class
);