From e57eeb2bca8a9607d9cc45ed933bf8a96dcbd2ca Mon Sep 17 00:00:00 2001 From: y2w Date: Tue, 27 Nov 2018 18:17:33 +0800 Subject: [PATCH 1/2] Update DbPersistenceService.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit configserver 存在安全校验时,添加用户名和密码的请求头,rest-ui可以正常获取到数据和提交数据 --- .../persistence/db/DbPersistenceService.java | 163 ++++++++++-------- 1 file changed, 93 insertions(+), 70 deletions(-) diff --git a/scca-persistence-db/src/main/java/com/didispace/scca/service/persistence/db/DbPersistenceService.java b/scca-persistence-db/src/main/java/com/didispace/scca/service/persistence/db/DbPersistenceService.java index 52e12ac..e9dadd0 100644 --- a/scca-persistence-db/src/main/java/com/didispace/scca/service/persistence/db/DbPersistenceService.java +++ b/scca-persistence-db/src/main/java/com/didispace/scca/service/persistence/db/DbPersistenceService.java @@ -1,12 +1,19 @@ package com.didispace.scca.service.persistence.db; +import com.didispace.scca.core.config.SccaConfigServerProperties; import com.didispace.scca.core.domain.Env; import com.didispace.scca.core.domain.EnvRepo; import com.didispace.scca.core.domain.Label; import com.didispace.scca.core.service.PersistenceService; import com.didispace.scca.core.service.UrlMakerService; import lombok.extern.slf4j.Slf4j; +import okhttp3.Credentials; + import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import java.util.Properties; @@ -14,79 +21,95 @@ /** * Created by 程序猿DD/翟永超 on 2018/4/24. *

- * Blog: http://blog.didispace.com/ - * Github: https://github.com/dyc87112/ + * Blog: http://blog.didispace.com/ Github: https://github.com/dyc87112/ */ @Slf4j public class DbPersistenceService implements PersistenceService { - @Autowired - private UrlMakerService urlMakerService; - - private RestTemplate restTemplate = new RestTemplate(); - - @Autowired - private EnvRepo envRepo; - - @Override - public Properties readProperties(String application, String profile, String label) { - String url = urlMakerService.configServerBaseUrl(profile) + "/readProperties?application={1}&profile={2}&label={3}"; - Properties properties = restTemplate.getForObject(url, Properties.class, application, profile, label); - return properties; - } - - @Override - public void deletePropertiesByEnv(String profile) { - // 删除某个环境下的所有配置 - String url = urlMakerService.configServerBaseUrl(profile) + "/deletePropertiesByEnv?profile={1}"; - Integer rows = restTemplate.getForObject(url, Integer.class, profile); - log.info("delete env [{}] property rows {}", profile, rows); - } - - @Override - public void deletePropertiesByProject(String application) { - // 删除某个项目所有环境下的配置 - for(Env env : envRepo.findAll()) { - String url = urlMakerService.configServerBaseUrl(env.getName()) + "/deletePropertiesByProject?application={1}"; - Integer rows = restTemplate.getForObject(url, Integer.class, application); - log.info("delete project [{}] in env [{}] property rows {}", application, rows); - } - } - - @Override - public void deletePropertiesByProjectAndEnv(String application, String profile) { - String url = urlMakerService.configServerBaseUrl(profile) + "/deletePropertiesByProjectAndEnv?application={1}&profile={2}"; - Integer rows = restTemplate.getForObject(url, Integer.class, application, profile); - log.info("delete project [{}] in env [{}] property rows {}", application, profile, rows); - } - - @Override - public void deletePropertiesByLabel(Label label) { - // TODO -// int rows = propertyRepo.deleteAllByLabel(label); -// log.info("delete project [{}] label [{}] property rows {}", -// label.getProject().getName(), label.getName(), rows); - } - - @Override - public void deletePropertiesByProjectAndEnvAndLabel(String application, String profile, String label) { - String url = urlMakerService.configServerBaseUrl(profile) + "/deletePropertiesByProjectAndEnvAndLabel?application={1}&profile={2}&label={3}"; - Integer rows = restTemplate.getForObject(url, Integer.class, application, profile, label); - log.info("delete {}-{}-{} property rows {}", application, profile, label, rows); - } - - @Override - public void saveProperties(String application, String profile, String label, Properties update) { - String url = urlMakerService.configServerBaseUrl(profile) + "/saveProperties?application={1}&profile={2}&label={3}"; - Integer rows = restTemplate.postForObject(url, update, Integer.class, application, profile, label); - log.info("add {}-{}-{} rows {}", application, profile, label, rows); - } - - @Override - public void updateProfileName(String oldName, String newName) { - String url = urlMakerService.configServerBaseUrl(oldName) + "/updateProfileName?oldName={1}&newName={2}"; - Integer rows = restTemplate.getForObject(url, Integer.class, oldName, newName); - log.info("update profile name {} -> {}, change property rows {}", oldName, newName, rows); - } + @Autowired + private UrlMakerService urlMakerService; + + private RestTemplate restTemplate = new RestTemplate(); + + @Autowired + private EnvRepo envRepo; + + @Autowired + protected SccaConfigServerProperties sccaConfigServerProperties; + + @Override + public Properties readProperties(String application, String profile, String label) { + String url = urlMakerService.configServerBaseUrl(profile) + + "/readProperties?application={1}&profile={2}&label={3}"; + Properties properties=request(url,HttpMethod.GET,Properties.class,application, profile, label); + return properties; + } + + @Override + public void deletePropertiesByEnv(String profile) { + // 删除某个环境下的所有配置 + String url = urlMakerService.configServerBaseUrl(profile) + "/deletePropertiesByEnv?profile={1}"; + Integer rows =request(url,HttpMethod.GET,Integer.class,profile); + log.info("delete env [{}] property rows {}", profile, rows); + } + + @Override + public void deletePropertiesByProject(String application) { + // 删除某个项目所有环境下的配置 + for (Env env : envRepo.findAll()) { + String url = urlMakerService.configServerBaseUrl(env.getName()) + + "/deletePropertiesByProject?application={1}"; + Integer rows =request(url,HttpMethod.GET,Integer.class,application); + log.info("delete project [{}] in env [{}] property rows {}", application, rows); + } + } + + @Override + public void deletePropertiesByProjectAndEnv(String application, String profile) { + String url = urlMakerService.configServerBaseUrl(profile) + + "/deletePropertiesByProjectAndEnv?application={1}&profile={2}"; + Integer rows =request(url,HttpMethod.GET,Integer.class,profile); + log.info("delete project [{}] in env [{}] property rows {}", application, profile, rows); + } + + @Override + public void deletePropertiesByLabel(Label label) { + // TODO + // int rows = propertyRepo.deleteAllByLabel(label); + // log.info("delete project [{}] label [{}] property rows {}", + // label.getProject().getName(), label.getName(), rows); + } + + @Override + public void deletePropertiesByProjectAndEnvAndLabel(String application, String profile, String label) { + String url = urlMakerService.configServerBaseUrl(profile) + + "/deletePropertiesByProjectAndEnvAndLabel?application={1}&profile={2}&label={3}"; + Integer rows =request(url,HttpMethod.GET,Integer.class,application, profile, label); + log.info("delete {}-{}-{} property rows {}", application, profile, label, rows); + } + + @Override + public void saveProperties(String application, String profile, String label, Properties update) { + String url = urlMakerService.configServerBaseUrl(profile) + + "/saveProperties?application={1}&profile={2}&label={3}"; + Integer rows =request(url,HttpMethod.POST,Integer.class,application, profile, label); + log.info("add {}-{}-{} rows {}", application, profile, label, rows); + } + + @Override + public void updateProfileName(String oldName, String newName) { + String url = urlMakerService.configServerBaseUrl(oldName) + "/updateProfileName?oldName={1}&newName={2}"; + Integer rows =request(url,HttpMethod.POST,Integer.class,oldName, newName); + log.info("update profile name {} -> {}, change property rows {}", oldName, newName, rows); + } + + private T request(String url, HttpMethod method, Class cls, Object... param) { + HttpHeaders headers = new HttpHeaders(); + headers.add("Authorization", + Credentials.basic(sccaConfigServerProperties.getUsername(), sccaConfigServerProperties.getPassword())); + HttpEntity entity = new HttpEntity<>(headers); + ResponseEntity response = restTemplate.exchange(url, method, entity, cls, param); + return response.getBody(); + } } From 661edefaffb29024abb50346a376fcfc20a0d923 Mon Sep 17 00:00:00 2001 From: y2w Date: Tue, 27 Nov 2018 18:48:58 +0800 Subject: [PATCH 2/2] Update DbPersistenceService.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复post时候的bug --- .../persistence/db/DbPersistenceService.java | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/scca-persistence-db/src/main/java/com/didispace/scca/service/persistence/db/DbPersistenceService.java b/scca-persistence-db/src/main/java/com/didispace/scca/service/persistence/db/DbPersistenceService.java index e9dadd0..a27f363 100644 --- a/scca-persistence-db/src/main/java/com/didispace/scca/service/persistence/db/DbPersistenceService.java +++ b/scca-persistence-db/src/main/java/com/didispace/scca/service/persistence/db/DbPersistenceService.java @@ -14,6 +14,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; +import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import java.util.Properties; @@ -41,15 +42,15 @@ public class DbPersistenceService implements PersistenceService { public Properties readProperties(String application, String profile, String label) { String url = urlMakerService.configServerBaseUrl(profile) + "/readProperties?application={1}&profile={2}&label={3}"; - Properties properties=request(url,HttpMethod.GET,Properties.class,application, profile, label); - return properties; + + return getForObject(url, Properties.class, application, profile, label); } @Override public void deletePropertiesByEnv(String profile) { // 删除某个环境下的所有配置 String url = urlMakerService.configServerBaseUrl(profile) + "/deletePropertiesByEnv?profile={1}"; - Integer rows =request(url,HttpMethod.GET,Integer.class,profile); + Integer rows = getForObject(url, Integer.class, profile); log.info("delete env [{}] property rows {}", profile, rows); } @@ -59,7 +60,7 @@ public void deletePropertiesByProject(String application) { for (Env env : envRepo.findAll()) { String url = urlMakerService.configServerBaseUrl(env.getName()) + "/deletePropertiesByProject?application={1}"; - Integer rows =request(url,HttpMethod.GET,Integer.class,application); + Integer rows = getForObject(url, Integer.class, application); log.info("delete project [{}] in env [{}] property rows {}", application, rows); } } @@ -68,7 +69,7 @@ public void deletePropertiesByProject(String application) { public void deletePropertiesByProjectAndEnv(String application, String profile) { String url = urlMakerService.configServerBaseUrl(profile) + "/deletePropertiesByProjectAndEnv?application={1}&profile={2}"; - Integer rows =request(url,HttpMethod.GET,Integer.class,profile); + Integer rows = getForObject(url, Integer.class, profile); log.info("delete project [{}] in env [{}] property rows {}", application, profile, rows); } @@ -84,7 +85,7 @@ public void deletePropertiesByLabel(Label label) { public void deletePropertiesByProjectAndEnvAndLabel(String application, String profile, String label) { String url = urlMakerService.configServerBaseUrl(profile) + "/deletePropertiesByProjectAndEnvAndLabel?application={1}&profile={2}&label={3}"; - Integer rows =request(url,HttpMethod.GET,Integer.class,application, profile, label); + Integer rows = getForObject(url, Integer.class, application, profile, label); log.info("delete {}-{}-{} property rows {}", application, profile, label, rows); } @@ -92,23 +93,32 @@ public void deletePropertiesByProjectAndEnvAndLabel(String application, String p public void saveProperties(String application, String profile, String label, Properties update) { String url = urlMakerService.configServerBaseUrl(profile) + "/saveProperties?application={1}&profile={2}&label={3}"; - Integer rows =request(url,HttpMethod.POST,Integer.class,application, profile, label); + Integer rows = postForObject(url, update, Integer.class, application, profile, label); log.info("add {}-{}-{} rows {}", application, profile, label, rows); } @Override public void updateProfileName(String oldName, String newName) { String url = urlMakerService.configServerBaseUrl(oldName) + "/updateProfileName?oldName={1}&newName={2}"; - Integer rows =request(url,HttpMethod.POST,Integer.class,oldName, newName); + Integer rows = postForObject(url, null, Integer.class, oldName, newName); log.info("update profile name {} -> {}, change property rows {}", oldName, newName, rows); } - private T request(String url, HttpMethod method, Class cls, Object... param) { + private T getForObject(String url, Class cls, Object... param) { HttpHeaders headers = new HttpHeaders(); headers.add("Authorization", Credentials.basic(sccaConfigServerProperties.getUsername(), sccaConfigServerProperties.getPassword())); HttpEntity entity = new HttpEntity<>(headers); - ResponseEntity response = restTemplate.exchange(url, method, entity, cls, param); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, entity, cls, param); + return response.getBody(); + } + + private T postForObject(String url, Object request, Class cls, Object... param) { + MultiValueMap headers = new HttpHeaders(); + headers.add("Authorization", + Credentials.basic(sccaConfigServerProperties.getUsername(), sccaConfigServerProperties.getPassword())); + HttpEntity entity = new HttpEntity<>(request, headers); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, entity, cls, param); return response.getBody(); }