Browse Source

🎉 增加nacos配置指定获取、刷新demo

test
smallchill 6 years ago
parent
commit
6af6a9e6e7
  1. 6
      blade-service/blade-demo/pom.xml
  2. 43
      blade-service/blade-demo/src/main/java/org/springblade/demo/config/DemoConfiguration.java
  3. 56
      blade-service/blade-demo/src/main/java/org/springblade/demo/controller/DemoController.java
  4. 42
      blade-service/blade-demo/src/main/java/org/springblade/demo/launcher/DemoLauncherServiceImpl.java
  5. 18
      blade-service/blade-demo/src/main/java/org/springblade/demo/props/DemoProperties.java
  6. 2
      doc/nacos/blade-demo-dev.yaml

6
blade-service/blade-demo/pom.xml

@ -32,6 +32,12 @@
<artifactId>blade-demo-api</artifactId>
<version>${bladex.project.version}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-auto</artifactId>
<version>${bladex.tool.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>

43
blade-service/blade-demo/src/main/java/org/springblade/demo/config/DemoConfiguration.java

@ -0,0 +1,43 @@
/*
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the dreamlu.net developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package org.springblade.demo.config;
import org.springblade.core.secure.registry.SecureRegistry;
import org.springblade.demo.props.DemoProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* Demo配置
*
* @author Chill
*/
@Configuration
@EnableConfigurationProperties(DemoProperties.class)
public class DemoConfiguration implements WebMvcConfigurer {
@Bean
public SecureRegistry secureRegistry() {
SecureRegistry secureRegistry = new SecureRegistry();
secureRegistry.excludePathPatterns("/demo/**");
return secureRegistry;
}
}

56
blade-service/blade-demo/src/main/java/org/springblade/demo/controller/DemoController.java

@ -0,0 +1,56 @@
/*
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the dreamlu.net developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package org.springblade.demo.controller;
import org.springblade.demo.props.DemoProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Demo控制器
*
* @author Chill
*/
@RefreshScope
@RestController
@RequestMapping("demo")
public class DemoController {
@Value("${demo.name}")
private String name;
private final DemoProperties properties;
public DemoController(DemoProperties properties) {
this.properties = properties;
}
@GetMapping("name")
public String getName() {
return name;
}
@GetMapping("name-by-props")
public String getNameByProps() {
return properties.getName();
}
}

42
blade-service/blade-demo/src/main/java/org/springblade/demo/launcher/DemoLauncherServiceImpl.java

@ -0,0 +1,42 @@
/*
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the dreamlu.net developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package org.springblade.demo.launcher;
import org.springblade.core.auto.service.AutoService;
import org.springblade.core.launch.constant.NacosConstant;
import org.springblade.core.launch.service.LauncherService;
import org.springframework.boot.builder.SpringApplicationBuilder;
import java.util.Properties;
/**
* 启动参数拓展
*
* @author Chill
*/
@AutoService(LauncherService.class)
public class DemoLauncherServiceImpl implements LauncherService {
@Override
public void launcher(SpringApplicationBuilder builder, String appName, String profile, boolean isLocalDev) {
Properties props = System.getProperties();
props.setProperty("spring.cloud.nacos.config.ext-config[0].data-id", NacosConstant.dataId(appName, profile));
props.setProperty("spring.cloud.nacos.config.ext-config[0].group", NacosConstant.NACOS_CONFIG_GROUP);
props.setProperty("spring.cloud.nacos.config.ext-config[0].refresh", NacosConstant.NACOS_CONFIG_REFRESH);
}
}

18
blade-service/blade-demo/src/main/java/org/springblade/demo/props/DemoProperties.java

@ -0,0 +1,18 @@
package org.springblade.demo.props;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* DemoProperties
*
* @author Chill
*/
@Data
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {
/**
* 名称
*/
private String name;
}

2
doc/nacos/blade-demo-dev.yaml

@ -0,0 +1,2 @@
demo:
name: demo-name
Loading…
Cancel
Save