From f8df08c53fec6380fc1457ce01c2b225adfc6eb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johnny=20Miller=20=28=E9=94=BA=E4=BF=8A=29?= Date: Fri, 25 Dec 2020 18:07:24 +0800 Subject: [PATCH] feat($AuthCenter): access other service by RestTemplate --- .../controller/PermissionController.java | 13 ++++++++++++ .../RestTemplateConfiguration.java | 20 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 auth-center/src/main/java/com/jmsoftware/maf/authcenter/universal/configuration/RestTemplateConfiguration.java diff --git a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/permission/controller/PermissionController.java b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/permission/controller/PermissionController.java index 1e6084d2..a025f7ac 100644 --- a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/permission/controller/PermissionController.java +++ b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/permission/controller/PermissionController.java @@ -12,6 +12,7 @@ import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; import java.util.LinkedHashMap; import java.util.List; @@ -33,7 +34,16 @@ public class PermissionController { private final DiscoveryClient discoveryClient; private final HttpApiScanHelper httpApiScanHelper; private final ProjectProperty projectProperty; + private final RestTemplate restTemplate; + /** + * Services info response body bean. + * + * @return the response body bean + * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 12/25/2020 5:44 PM + * @see + * RestTemplate Excample + */ @GetMapping("/permissions/services-info") @ApiOperation(value = "Get services info", notes = "Get services info") public ResponseBodyBean servicesInfo() { @@ -44,6 +54,9 @@ public ResponseBodyBean servicesInfo() { val instances = discoveryClient.getInstances(service); log.info("Instances: {}", instances); resultMap.put(service, instances); + Object httpApiResources = restTemplate.getForObject( + "http://" + service + "/http-api-resources", Object.class); + log.info("httpApiResources: {}", httpApiResources); }); val httpApiMap = httpApiScanHelper.scan(projectProperty.getBasePackage()); resultMap.put("httpApiMap", httpApiMap.toString()); diff --git a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/universal/configuration/RestTemplateConfiguration.java b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/universal/configuration/RestTemplateConfiguration.java new file mode 100644 index 00000000..16a317a5 --- /dev/null +++ b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/universal/configuration/RestTemplateConfiguration.java @@ -0,0 +1,20 @@ +package com.jmsoftware.maf.authcenter.universal.configuration; + +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +/** + * Description: RestTemplateConfiguration, change description here. + * + * @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 12/25/2020 5:42 PM + **/ +@Configuration +public class RestTemplateConfiguration { + @Bean + @LoadBalanced + public RestTemplate restTemplate() { + return new RestTemplate(); + } +}