1.nacaosClient接入:
pom.xml引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency>
添加nacosconfig配置
@Configuration public class NacosConfig { @Bean public ConfigService configService() throws NacosException { final Properties properties = new Properties(); //设置Nacos节点,对应的IP地址。 properties.setProperty(PropertyKeyConst.SERVER_ADDR,"127.0.0.1:8848"); //设置命名空间。 properties.setProperty(PropertyKeyConst.NAMESPACE,"……"); //如果开启Nacos权限校验,设置用户名。 properties.setProperty(PropertyKeyConst.USERNAME,"nacos"); //如果开启Nacos权限校验,设置用户密码。 properties.setProperty(PropertyKeyConst.PASSWORD,"nacos"); //设置获取配置信息的长轮训超时时间。 properties.setProperty(PropertyKeyConst.CONFIG_LONG_POLL_TIMEOUT,"3000"); //设置获取配置信息失败之后,重试的次数。 properties.setProperty(PropertyKeyConst.CONFIG_RETRY_TIME,"5"); //设置是否开启客户端主动拉取最新的配置信息。 properties.setProperty(PropertyKeyConst.ENABLE_REMOTE_SYNC_CONFIG,"true"); //设置最大重试次数。 properties.setProperty(PropertyKeyConst.MAX_RETRY,"5"); //构造一个ConfigService实例 ConfigService configService = NacosFactory.createConfigService(properties); return configService; } }
调用获取nacos配置
@Component public class NacosConfigManager { @Resource private ConfigService configService; @PostConstruct private void init() throws NacosException { String data=configService.getConfig("use-nacos-client-nacos-config","use-nacos-client-nacos-config",2000); System.out.println("通过Nacos Client 从Nacos配置中心获取配置信息为:"+data); } }
2.api 接入
pom.mxl引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency>
初始化restTemplate
@Configuration public class OpenApiConfig { @Bean public RestTemplate restTemplate() { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout(2000); requestFactory.setReadTimeout(2000); RestTemplate restTemplate = new RestTemplate(requestFactory); return restTemplate; } }
通过restTemplate读取配置信息
@Component public class ReaderConfigManager { @Resource private RestTemplate restTemplate; //使用RestTemplate访问Nacos Config的Open API @PostConstruct public void readConfig(){ String url="http://127.0.0.1:8848/nacos/v1/cs/configs?dataId={dataId}&group={group}&tenant={tenant}"; Map<String,String> params=new HashMap<String,String>(16); params.put("dataId", "openapi-nacos-config"); params.put("group", "openapi-nacos-config"); params.put("tenant","c7ba173f-29e5-4c58-ae78-b102be11c4f9"); //入参 ResponseEntity<String> responseEntity=restTemplate.getForEntity(url,String.class,params); String result=responseEntity.getBody(); System.out.println("通过Open API 从Nacos配置中心获取配置信息为:"+result); } }
3.springcloudAlibabaConfig接入
pom.xml引入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency>
新增配置文件bootstrap.yaml
spring: application: name: spring-cloud-alibaba-config cloud: nacos: config: namespace: …… group: spring-cloud-alibaba-config password: nacos enable-remote-sync-config: true server-addr: 127.0.0.1:8848 username: nacos name: spring-cloud-alibaba-config file-extension: properties extension-configs: - data-id: spring-cloud-alibaba-config-test.yaml group: spring-cloud-alibaba-config namespace: …… refresh: true server: port: 8889
新增application.properties
spring.youxia.config.name=test2 spring.application.name=spring-cloud-alibaba-config
在nacos中新增配dataId 为spring-cloud-alibaba-config.properties的配置文件 ,其中配置
spring.youxia.config.name=test20
获取配置文件代码:
@Configuration @RefreshScope public class ConfigReader { @Autowired private NacosConfig nacosConfig; @PostConstruct public void init() { Executors.newCachedThreadPool().execute(new ConfigThread()); } class ConfigThread implements Runnable { @Override public void run() { while (true) { System.out.println("使用Spring Cloud Alibaba接入Nacos配置中心,获取配置信息name为:" + nacosConfig.getName()); System.out.println("使用Spring Cloud Alibaba接入Nacos配置中心,获取配置信息value为:"+nacosConfig.getValue()); try { Thread.sleep(6000); } catch (InterruptedException e) { System.out.println(e.getMessage()); } } } } }
@Data @Configuration @RefreshScope @ConfigurationProperties (prefix = "spring.youxia.config") public class NacosConfig { private boolean name; }
标签:接入,springcloud,boot,nacos,spring,org,config,properties From: https://www.cnblogs.com/heshana/p/16918193.html