| | |
| | | import com.alibaba.cloud.nacos.NacosDiscoveryProperties; |
| | | import com.alibaba.cloud.nacos.NacosServiceManager; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.alibaba.nacos.api.annotation.NacosInjected; |
| | | import com.alibaba.nacos.api.exception.NacosException; |
| | | import com.alibaba.nacos.api.naming.NamingService; |
| | | import com.alibaba.nacos.api.naming.pojo.Instance; |
| | | import com.alibaba.nacos.common.http.client.NacosRestTemplate; |
| | | import com.vci.ubcs.omd.vo.OmdBtmTypeVO; |
| | | import org.apache.http.Header; |
| | | import org.apache.http.client.methods.CloseableHttpResponse; |
| | |
| | | import org.springblade.core.tool.api.R; |
| | | import org.springblade.core.tool.utils.StringPool; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.HttpEntity; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.stereotype.Repository; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.io.IOException; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.Optional; |
| | | |
| | | /** |
| | | * Description: 领域转发器 |
| | |
| | | * @author LiHang |
| | | * @date 2023/4/25 |
| | | */ |
| | | @Repository |
| | | public class DomainRepeater { |
| | | private DomainRepeater() { |
| | | } |
| | | |
| | | /** |
| | | * 控制器路径 |
| | |
| | | */ |
| | | public static final String API_GET_ALL_TABLE_INFO = API_PREFIX + StringPool.SLASH +"getAllTableInfo"; |
| | | |
| | | @NacosInjected |
| | | private NamingService nacosNamingService; |
| | | /** |
| | | * Nacos服务列表 |
| | | */ |
| | | private static NamingService namingService = SINGLE.build(); |
| | | |
| | | @Component |
| | | public static class SINGLE{ |
| | | private static class SINGLE { |
| | | @Autowired |
| | | private NacosServiceManager nacosServiceManager; |
| | | private NacosServiceManager manager; |
| | | private static NacosServiceManager nacosServiceManager; |
| | | |
| | | @Autowired |
| | | private NacosDiscoveryProperties nacosDiscoveryProperties; |
| | | |
| | | private static NamingService nacosNamingService; |
| | | private NacosDiscoveryProperties properties; |
| | | private static NacosDiscoveryProperties nacosDiscoveryProperties; |
| | | |
| | | @PostConstruct |
| | | public NamingService build(){ |
| | | nacosNamingService = nacosServiceManager.getNamingService(nacosDiscoveryProperties.getNacosProperties()); |
| | | return nacosNamingService; |
| | | private void init(){ |
| | | nacosServiceManager = manager; |
| | | nacosDiscoveryProperties = properties; |
| | | } |
| | | private static NamingService build(){ |
| | | return nacosServiceManager.getNamingService(nacosDiscoveryProperties.getNacosProperties()); |
| | | } |
| | | } |
| | | |
| | | private RestTemplate restTemplate = new RestTemplate(); |
| | | |
| | | public Object submitBtmType (String serviceName, OmdBtmTypeVO omdBtmTypeVO) throws NacosException { |
| | | |
| | | if (nacosNamingService == null){ |
| | | nacosNamingService = SINGLE.nacosNamingService; |
| | | } |
| | | Instance service = nacosNamingService.selectOneHealthyInstance(AppConstant.APPLICATION_GATEWAY_NAME); |
| | | public static R submitBtmType (String serviceName, OmdBtmTypeVO omdBtmTypeVO) throws NacosException { |
| | | Instance service = namingService.selectOneHealthyInstance(AppConstant.APPLICATION_GATEWAY_NAME); |
| | | try { |
| | | String url = "http://" + service.getIp() +StringPool.COLON+ service.getPort() + StringPool.SLASH + serviceName + StringPool.SLASH + API_SUBMIT_BTM_TYPE; |
| | | HttpPost httpPost = buildPost(url); |
| | | httpPost.setEntity(new StringEntity(JSON.toJSONString(omdBtmTypeVO),"utf-8")); |
| | | CloseableHttpClient httpClient = HttpClientBuilder.create().build(); |
| | | CloseableHttpResponse response = httpClient.execute(httpPost); |
| | | return response; |
| | | ResponseEntity<R> responseEntity = executePost(url, JSONObject.toJSONString(omdBtmTypeVO), null); |
| | | if (responseEntity.getStatusCode().equals(HttpStatus.OK)){ |
| | | return responseEntity.getBody(); |
| | | } |
| | | return R.fail(String.valueOf(responseEntity.getStatusCode().value())); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | private HttpPost buildPost(String url){ |
| | | HttpPost httpPost = new HttpPost(url); |
| | | httpPost.setHeader("Content-Type","application/json;charset=UTF-8"); |
| | | httpPost.setHeader("Blade-Auth", AuthUtil.getHeader()); |
| | | return httpPost; |
| | | private static ResponseEntity<R> executePost(String url, String body,Map<String,String> extraHeaders) throws IOException { |
| | | RestTemplate restTemplate = new RestTemplate(); |
| | | HttpHeaders httpHeaders = new HttpHeaders(); |
| | | httpHeaders.add("Content-Type","application/json;charset=UTF-8"); |
| | | httpHeaders.add("Blade-Auth", AuthUtil.getHeader()); |
| | | Optional.ofNullable(extraHeaders).orElseGet(HashMap::new).forEach(httpHeaders::add); |
| | | HttpEntity<String> request = new HttpEntity<>(body,httpHeaders); |
| | | return restTemplate.postForEntity(url, request, R.class); |
| | | } |
| | | } |