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

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從入門到精通教程

我的專欄

 

 

至此,全部介紹就結束了

 

 

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

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

 

我的CSDN主頁

關於我(個人域名)

我的開源項目集Github

 

期望和大家一起學習,一起成長,共勉,O(∩_∩)O謝謝

歡迎交流問題,可加個人QQ 469580884,

或者,加我的羣號 751925591,一起探討交流問題

不講虛的,只做實幹家

Talk is cheap,show me the code

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章