当前位置:   article > 正文

spring-cloud Edgware.SR6 之 spring config server 1.4.7 (配置中心)配置机制、开放的接口(控制器)

edgware.sr6

配置机制

  1. 1、@EnableConfigServer 用途:--- 开关
  2. 注册 org.springframework.cloud.config.server.config.ConfigServerConfiguration.Marker 对象到spring上下文
  3. 2、自动加载 ConfigServerAutoConfiguration 进行配置
  4. 1、检查spring上下文是否有 ConfigServerConfiguration.Marker 对象
  5. 2、如果有存在,注册各种bean

 

注册的控制器

org.springframework.cloud.config.server.environment.EnvironmentController
org.springframework.cloud.config.server.resource.ResourceController

.

EnvironmentController

  1. @RestController
  2. @RequestMapping(method = RequestMethod.GET, path = "${spring.cloud.config.server.prefix:}")
  3. public class EnvironmentController {
  4. // ...
  5. @RequestMapping("/{name}/{profiles:.*[^-].*}")
  6. public Environment defaultLabel(@PathVariable String name,
  7. @PathVariable String profiles) {
  8. return labelled(name, profiles, null);
  9. }
  10. @RequestMapping("/{name}/{profiles}/{label:.*}")
  11. public Environment labelled(@PathVariable String name, @PathVariable String profiles,
  12. @PathVariable String label) {
  13. if (name != null && name.contains("(_)")) {
  14. // "(_)" is uncommon in a git repo name, but "/" cannot be matched
  15. // by Spring MVC
  16. name = name.replace("(_)", "/");
  17. }
  18. if (label != null && label.contains("(_)")) {
  19. // "(_)" is uncommon in a git branch name, but "/" cannot be matched
  20. // by Spring MVC
  21. label = label.replace("(_)", "/");
  22. }
  23. Environment environment = this.repository.findOne(name, profiles, label);
  24. return environment;
  25. }
  26. @RequestMapping("/{name}-{profiles}.properties")
  27. public ResponseEntity<String> properties(@PathVariable String name,
  28. @PathVariable String profiles,
  29. @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
  30. throws IOException {
  31. return labelledProperties(name, profiles, null, resolvePlaceholders);
  32. }
  33. @RequestMapping("/{label}/{name}-{profiles}.properties")
  34. public ResponseEntity<String> labelledProperties(@PathVariable String name,
  35. @PathVariable String profiles, @PathVariable String label,
  36. @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
  37. throws IOException {
  38. validateProfiles(profiles);
  39. Environment environment = labelled(name, profiles, label);
  40. Map<String, Object> properties = convertToProperties(environment);
  41. String propertiesString = getPropertiesString(properties);
  42. if (resolvePlaceholders) {
  43. propertiesString = resolvePlaceholders(prepareEnvironment(environment),
  44. propertiesString);
  45. }
  46. return getSuccess(propertiesString);
  47. }
  48. @RequestMapping("{name}-{profiles}.json")
  49. public ResponseEntity<String> jsonProperties(@PathVariable String name,
  50. @PathVariable String profiles,
  51. @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
  52. throws Exception {
  53. return labelledJsonProperties(name, profiles, null, resolvePlaceholders);
  54. }
  55. @RequestMapping("/{label}/{name}-{profiles}.json")
  56. public ResponseEntity<String> labelledJsonProperties(@PathVariable String name,
  57. @PathVariable String profiles, @PathVariable String label,
  58. @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
  59. throws Exception {
  60. validateProfiles(profiles);
  61. Environment environment = labelled(name, profiles, label);
  62. Map<String, Object> properties = convertToMap(environment);
  63. String json = this.objectMapper.writeValueAsString(properties);
  64. if (resolvePlaceholders) {
  65. json = resolvePlaceholders(prepareEnvironment(environment), json);
  66. }
  67. return getSuccess(json, MediaType.APPLICATION_JSON);
  68. }
  69. private String getPropertiesString(Map<String, Object> properties) {
  70. StringBuilder output = new StringBuilder();
  71. for (Entry<String, Object> entry : properties.entrySet()) {
  72. if (output.length() > 0) {
  73. output.append("\n");
  74. }
  75. String line = entry.getKey() + ": " + entry.getValue();
  76. output.append(line);
  77. }
  78. return output.toString();
  79. }
  80. @RequestMapping({ "/{name}-{profiles}.yml", "/{name}-{profiles}.yaml" })
  81. public ResponseEntity<String> yaml(@PathVariable String name,
  82. @PathVariable String profiles,
  83. @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
  84. throws Exception {
  85. return labelledYaml(name, profiles, null, resolvePlaceholders);
  86. }
  87. @RequestMapping({ "/{label}/{name}-{profiles}.yml",
  88. "/{label}/{name}-{profiles}.yaml" })
  89. public ResponseEntity<String> labelledYaml(@PathVariable String name,
  90. @PathVariable String profiles, @PathVariable String label,
  91. @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
  92. throws Exception {
  93. validateProfiles(profiles);
  94. Environment environment = labelled(name, profiles, label);
  95. Map<String, Object> result = convertToMap(environment);
  96. if (this.stripDocument && result.size() == 1
  97. && result.keySet().iterator().next().equals("document")) {
  98. Object value = result.get("document");
  99. if (value instanceof Collection) {
  100. return getSuccess(new Yaml().dumpAs(value, Tag.SEQ, FlowStyle.BLOCK));
  101. }
  102. else {
  103. return getSuccess(new Yaml().dumpAs(value, Tag.STR, FlowStyle.BLOCK));
  104. }
  105. }
  106. String yaml = new Yaml().dumpAsMap(result);
  107. if (resolvePlaceholders) {
  108. yaml = resolvePlaceholders(prepareEnvironment(environment), yaml);
  109. }
  110. return getSuccess(yaml);
  111. }
  112. // ...
  113. @ExceptionHandler(RepositoryException.class)
  114. public void noSuchLabel(HttpServletResponse response) throws IOException {
  115. response.sendError(HttpStatus.NOT_FOUND.value());
  116. }
  117. @ExceptionHandler(IllegalArgumentException.class)
  118. public void illegalArgument(HttpServletResponse response) throws IOException {
  119. response.sendError(HttpStatus.BAD_REQUEST.value());
  120. }
  121. // ...
  122. }

.

ResourceController

  1. @RestController
  2. @RequestMapping(method = RequestMethod.GET, path = "${spring.cloud.config.server.prefix:}")
  3. public class ResourceController {
  4. // ....
  5. @RequestMapping("/{name}/{profile}/{label}/**")
  6. public String retrieve(@PathVariable String name, @PathVariable String profile,
  7. @PathVariable String label, HttpServletRequest request,
  8. @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
  9. throws IOException {
  10. String path = getFilePath(request, name, profile, label);
  11. return retrieve(name, profile, label, path, resolvePlaceholders);
  12. }
  13. @RequestMapping(value = "/{name}/{profile}/**", params = "useDefaultLabel")
  14. public String retrieve(@PathVariable String name, @PathVariable String profile,
  15. HttpServletRequest request,
  16. @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
  17. throws IOException {
  18. String path = getFilePath(request, name, profile, null);
  19. return retrieve(name, profile, null, path, resolvePlaceholders);
  20. }
  21. // ....
  22. @RequestMapping(value = "/{name}/{profile}/{label}/**", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
  23. public synchronized byte[] binary(@PathVariable String name,
  24. @PathVariable String profile, @PathVariable String label,
  25. HttpServletRequest request) throws IOException {
  26. String path = getFilePath(request, name, profile, label);
  27. return binary(name, profile, label, path);
  28. }
  29. // ....
  30. @ExceptionHandler(NoSuchResourceException.class)
  31. @ResponseStatus(HttpStatus.NOT_FOUND)
  32. public void notFound(NoSuchResourceException e) {
  33. }
  34. }

.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/1000362
推荐阅读
相关标签
  

闽ICP备14008679号