SpringCloud Alibaba从入门到精通教程(三)- 项目中快速集成配置中心·Nacos配置中心管理功能

需求背景

项目中快速集成配置中心·Nacos配置中心管理功能

Tips技术点

1. @Value注解

  • 值绑定

2. 区分不同环境,对应不同配置中心管理功能

代码演示

1. 项目目录结构

2. pom.xml依赖组件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<artifactId>microservice-alibaba-nacos-config</artifactId>
	<packaging>jar</packaging>

	<parent>
		<groupId>com.minbo.cloud.alibaba</groupId>
		<artifactId>spring-cloud-alibaba-study-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
		</dependency>
		<dependency>
			<groupId>com.github.xiaoymin</groupId>
			<artifactId>swagger-bootstrap-ui</artifactId>
		</dependency>
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib-ext-spring</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
		</dependency>
	</dependencies>
</project>

3. bootstrap.yaml配置文件

这里的namespace值,使用的是:dev环境对应的命名空间id值

spring:
   application:
      name: microservice-alibaba-nacos-config
   profiles:
      active: dev
   cloud:
      nacos:
         discovery:
            server-addr: 127.0.0.1:8848
            namespace: 2bc5565f-453f-4b1c-bde4-eafe23bdb2f0
         config:
            server-addr: 127.0.0.1:8848
            namespace: 2bc5565f-453f-4b1c-bde4-eafe23bdb2f0
            file-extension: properties
            shared-dataids: microservice-alibaba-nacos-config.properties
            refreshable-dataids: microservice-alibaba-nacos-config.properties
# server
server:
   port: 9091

点击+号,新增一个Data Id对应应用名的配置(Data Id名称是完整对应yaml配置文件的:microservice-alibaba-nacos-config.properties)

然后,点击“编辑”,新增“config.appKey=abc1234”内容,最后点击“发布”即可

4. 访问接口类

获取该值:

package com.minbo.cloud.alibaba.controller;

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.RestController;

import com.minbo.cloud.alibaba.util.JsonResult;
import com.minbo.cloud.alibaba.util.ResultCode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Minbo
 */
@RestController
@Slf4j
@Api(tags = { "init" })
@RefreshScope
public class InitController {

	@Value("${config.appKey}")
	private String appKey;

	@ApiOperation(value = "获得AppKey值", httpMethod = "GET")
	@GetMapping("/getAppKey")
	public JsonResult getAppKey() {
		return new JsonResult(ResultCode.SUCCESS, this.appKey);
	}
}

注:@RefreshScope注解,支持实时修改配置值,而不需要重启项目

5. 启动类

package com.minbo.cloud.alibaba;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;

/**
 * 程序主入口
 * 
 * @author Minbo
 *
 */
@SpringBootApplication
@EnableSwaggerBootstrapUI
@EnableDiscoveryClient
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}

5. 值是否获取成功

启动后,访问地址:http://localhost:9091/doc.html

 

区分不同环境用法

1. 支持DB和Redis配置中心下发

bootstrap.yaml.bak

spring:
   application:
      name: microservice-alibaba-nacos-config
   profiles:
      active: dev
   cloud:
      nacos:
         discovery:
            server-addr: 127.0.0.1:8848
            namespace: 2bc5565f-453f-4b1c-bde4-eafe23bdb2f0
         config:
            server-addr: 127.0.0.1:8848
            namespace: 2bc5565f-453f-4b1c-bde4-eafe23bdb2f0
            file-extension: properties
            shared-dataids: microservice-alibaba-nacos-config.properties
            refreshable-dataids: microservice-alibaba-nacos-config.properties
   datasource:
      driverClassName: ${config.datasource.driverClassName}
      url: ${config.datasource.url}
      username: ${config.datasource.username}
      password: ${config.datasource.password}
      hikari:
         initial-size: ${config.datasource.initialSize}
         minimum-idle: ${config.datasource.minIdle}
         maximum-pool-size: ${config.datasource.maxActive}
         auto-commit: true
         idle-timeout: 20000
         pool-name: MyHikarifMain
         max-lifetime: 1800000
         connection-timeout: 20000
         connection-test-query: SELECT 1
         validation-timeout: 5000
   redis:
      host: ${config.redis.host}
      port: ${config.redis.port}
      password: ${config.redis.password}
      pool:
         max-idle: ${config.redis.pool.max-idle}
         min-idle: ${config.redis.pool.min-idle}
         max-active: ${config.redis.pool.max-active}
         max-wait: ${config.redis.pool.max-wait}
      timeout: ${config.redis.timeout}
      commandTimeout: ${config.redis.commandTimeout}
   
# server
server:
   port: 9091

2. 支持多环境不同配置下发

bootstrap.yaml2.bak

# 不同的环境使用不同的配置,通过指定启动参数使用不同的profile,比如:
# 开发环境:java -jar xxx.jar --spring.profiles.active=dev
# 生产环境:java -jar xxx.jar --spring.profiles.active=prod
spring:
   application:
      name: microservice-alibaba-nacos-config
   profiles:
      active: dev
   datasource:
      driverClassName: ${config.datasource.driverClassName}
      url: ${config.datasource.url}
      username: ${config.datasource.username}
      password: ${config.datasource.password}
      hikari:
         initial-size: ${config.datasource.initialSize}
         minimum-idle: ${config.datasource.minIdle}
         maximum-pool-size: ${config.datasource.maxActive}
         auto-commit: true
         idle-timeout: 20000
         pool-name: MyHikarifMain
         max-lifetime: 1800000
         connection-timeout: 20000
         connection-test-query: SELECT 1
         validation-timeout: 5000
   redis:
      host: ${config.redis.host}
      port: ${config.redis.port}
      password: ${config.redis.password}
      pool:
         max-idle: ${config.redis.pool.max-idle}
         min-idle: ${config.redis.pool.min-idle}
         max-active: ${config.redis.pool.max-active}
         max-wait: ${config.redis.pool.max-wait}
      timeout: ${config.redis.timeout}
      commandTimeout: ${config.redis.commandTimeout}
   
# server
server:
   port: 9091

#下面这一行务必不能少,区分不同配置,而且必须是三个字符"-"
---
spring:
   profiles: dev
   cloud:
      nacos:
         discovery:
            server-addr: 127.0.0.1:8848
            namespace: 2bc5565f-453f-4b1c-bde4-eafe23bdb2f0
         config:
            server-addr: 127.0.0.1:8848
            namespace: 2bc5565f-453f-4b1c-bde4-eafe23bdb2f0
            file-extension: properties
            shared-dataids: microservice-alibaba-nacos-config.properties
            refreshable-dataids: microservice-alibaba-nacos-config.properties
---
spring:
   profiles: test
   cloud:
      nacos:
         discovery:
            server-addr: 127.0.0.1:8848
            namespace: 47247e4c-b018-4c63-92cd-ebbc119eef7f
         config:
            server-addr: 127.0.0.1:8848
            namespace: 47247e4c-b018-4c63-92cd-ebbc119eef7f
            file-extension: properties
            shared-dataids: microservice-alibaba-nacos-config.properties
            refreshable-dataids: microservice-alibaba-nacos-config.properties
---
spring:
   profiles: prod
   cloud:
      nacos:
         discovery:
            server-addr: 127.0.0.1:8848
            namespace: 9d3bb62a-14c5-43aa-ac05-6576caa5be2b
         config:
            server-addr: 127.0.0.1:8848
            namespace: 9d3bb62a-14c5-43aa-ac05-6576caa5be2b
            file-extension: properties
            shared-dataids: microservice-alibaba-nacos-config.properties
            refreshable-dataids: microservice-alibaba-nacos-config.properties

3. 通过profiles.active激活不同环境配置

更多关于此用法(不同环境的配置用法):

SpringCloud Alibaba从入门到精通教程(二)- 项目中快速集成配置中心·Nacos服务注册发现功能

这里不重复介绍了

 

完整源码下载

我的Github源码地址:

https://github.com/hemin1003/spring-cloud-study/tree/master/spring-cloud-alibaba/microservice-alibaba-nacos-config

其他技术

此项目代码案例中,还集成了以下功能:

1. 集成swagger

十分简单、简洁易用的在线接口文档组件swagger

Swagger入门教程用法:SpringBoot从入门到精通教程(二十四)- Swagger集成用法

下一章教程

SpringCloud Alibaba从入门到精通教程(四)- 流控组件Sentinel快速入门介绍·Server启动安装

该系列教程

SpringCloud Alibaba从入门到精通教程

我的专栏

 

 

至此,全部介绍就结束了

 

 

-------------------------------

-------------------------------

 

我的CSDN主页

关于我(个人域名)

我的开源项目集Github

 

期望和大家一起学习,一起成长,共勉,O(∩_∩)O谢谢

欢迎交流问题,可加个人QQ 469580884,

或者,加我的群号 751925591,一起探讨交流问题

不讲虚的,只做实干家

Talk is cheap,show me the code

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页