赞
踩
- 1、@EnableConfigServer 用途:--- 开关
- 注册 org.springframework.cloud.config.server.config.ConfigServerConfiguration.Marker 对象到spring上下文
-
- 2、自动加载 ConfigServerAutoConfiguration 进行配置
- 1、检查spring上下文是否有 ConfigServerConfiguration.Marker 对象
- 2、如果有存在,注册各种bean
org.springframework.cloud.config.server.environment.EnvironmentController
org.springframework.cloud.config.server.resource.ResourceController
.
- @RestController
- @RequestMapping(method = RequestMethod.GET, path = "${spring.cloud.config.server.prefix:}")
- public class EnvironmentController {
-
- // ...
-
- @RequestMapping("/{name}/{profiles:.*[^-].*}")
- public Environment defaultLabel(@PathVariable String name,
- @PathVariable String profiles) {
- return labelled(name, profiles, null);
- }
-
- @RequestMapping("/{name}/{profiles}/{label:.*}")
- public Environment labelled(@PathVariable String name, @PathVariable String profiles,
- @PathVariable String label) {
- if (name != null && name.contains("(_)")) {
- // "(_)" is uncommon in a git repo name, but "/" cannot be matched
- // by Spring MVC
- name = name.replace("(_)", "/");
- }
- if (label != null && label.contains("(_)")) {
- // "(_)" is uncommon in a git branch name, but "/" cannot be matched
- // by Spring MVC
- label = label.replace("(_)", "/");
- }
- Environment environment = this.repository.findOne(name, profiles, label);
- return environment;
- }
-
- @RequestMapping("/{name}-{profiles}.properties")
- public ResponseEntity<String> properties(@PathVariable String name,
- @PathVariable String profiles,
- @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
- throws IOException {
- return labelledProperties(name, profiles, null, resolvePlaceholders);
- }
-
- @RequestMapping("/{label}/{name}-{profiles}.properties")
- public ResponseEntity<String> labelledProperties(@PathVariable String name,
- @PathVariable String profiles, @PathVariable String label,
- @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
- throws IOException {
- validateProfiles(profiles);
- Environment environment = labelled(name, profiles, label);
- Map<String, Object> properties = convertToProperties(environment);
- String propertiesString = getPropertiesString(properties);
- if (resolvePlaceholders) {
- propertiesString = resolvePlaceholders(prepareEnvironment(environment),
- propertiesString);
- }
- return getSuccess(propertiesString);
- }
-
- @RequestMapping("{name}-{profiles}.json")
- public ResponseEntity<String> jsonProperties(@PathVariable String name,
- @PathVariable String profiles,
- @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
- throws Exception {
- return labelledJsonProperties(name, profiles, null, resolvePlaceholders);
- }
-
- @RequestMapping("/{label}/{name}-{profiles}.json")
- public ResponseEntity<String> labelledJsonProperties(@PathVariable String name,
- @PathVariable String profiles, @PathVariable String label,
- @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
- throws Exception {
- validateProfiles(profiles);
- Environment environment = labelled(name, profiles, label);
- Map<String, Object> properties = convertToMap(environment);
- String json = this.objectMapper.writeValueAsString(properties);
- if (resolvePlaceholders) {
- json = resolvePlaceholders(prepareEnvironment(environment), json);
- }
- return getSuccess(json, MediaType.APPLICATION_JSON);
- }
-
- private String getPropertiesString(Map<String, Object> properties) {
- StringBuilder output = new StringBuilder();
- for (Entry<String, Object> entry : properties.entrySet()) {
- if (output.length() > 0) {
- output.append("\n");
- }
- String line = entry.getKey() + ": " + entry.getValue();
- output.append(line);
- }
- return output.toString();
- }
-
- @RequestMapping({ "/{name}-{profiles}.yml", "/{name}-{profiles}.yaml" })
- public ResponseEntity<String> yaml(@PathVariable String name,
- @PathVariable String profiles,
- @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
- throws Exception {
- return labelledYaml(name, profiles, null, resolvePlaceholders);
- }
-
- @RequestMapping({ "/{label}/{name}-{profiles}.yml",
- "/{label}/{name}-{profiles}.yaml" })
- public ResponseEntity<String> labelledYaml(@PathVariable String name,
- @PathVariable String profiles, @PathVariable String label,
- @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
- throws Exception {
- validateProfiles(profiles);
- Environment environment = labelled(name, profiles, label);
- Map<String, Object> result = convertToMap(environment);
- if (this.stripDocument && result.size() == 1
- && result.keySet().iterator().next().equals("document")) {
- Object value = result.get("document");
- if (value instanceof Collection) {
- return getSuccess(new Yaml().dumpAs(value, Tag.SEQ, FlowStyle.BLOCK));
- }
- else {
- return getSuccess(new Yaml().dumpAs(value, Tag.STR, FlowStyle.BLOCK));
- }
- }
- String yaml = new Yaml().dumpAsMap(result);
-
- if (resolvePlaceholders) {
- yaml = resolvePlaceholders(prepareEnvironment(environment), yaml);
- }
-
- return getSuccess(yaml);
- }
-
- // ...
-
- @ExceptionHandler(RepositoryException.class)
- public void noSuchLabel(HttpServletResponse response) throws IOException {
- response.sendError(HttpStatus.NOT_FOUND.value());
- }
-
- @ExceptionHandler(IllegalArgumentException.class)
- public void illegalArgument(HttpServletResponse response) throws IOException {
- response.sendError(HttpStatus.BAD_REQUEST.value());
- }
-
- // ...
-
- }
.
- @RestController
- @RequestMapping(method = RequestMethod.GET, path = "${spring.cloud.config.server.prefix:}")
- public class ResourceController {
-
- // ....
-
- @RequestMapping("/{name}/{profile}/{label}/**")
- public String retrieve(@PathVariable String name, @PathVariable String profile,
- @PathVariable String label, HttpServletRequest request,
- @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
- throws IOException {
- String path = getFilePath(request, name, profile, label);
- return retrieve(name, profile, label, path, resolvePlaceholders);
- }
-
- @RequestMapping(value = "/{name}/{profile}/**", params = "useDefaultLabel")
- public String retrieve(@PathVariable String name, @PathVariable String profile,
- HttpServletRequest request,
- @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
- throws IOException {
- String path = getFilePath(request, name, profile, null);
- return retrieve(name, profile, null, path, resolvePlaceholders);
- }
-
- // ....
-
- @RequestMapping(value = "/{name}/{profile}/{label}/**", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
- public synchronized byte[] binary(@PathVariable String name,
- @PathVariable String profile, @PathVariable String label,
- HttpServletRequest request) throws IOException {
- String path = getFilePath(request, name, profile, label);
- return binary(name, profile, label, path);
- }
-
- // ....
-
- @ExceptionHandler(NoSuchResourceException.class)
- @ResponseStatus(HttpStatus.NOT_FOUND)
- public void notFound(NoSuchResourceException e) {
- }
-
- }
.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。