1. 程式人生 > 實用技巧 >Nacos整合Spring Cloud Gateway使用第三章:nacos配置中心

Nacos整合Spring Cloud Gateway使用第三章:nacos配置中心

目錄:

第一章:Nacos整合Spring Cloud Gateway使用第一章:理解解釋

第二章:Nacos整合Spring Cloud Gateway使用第二章,上手demo

第一章為對nacos與springcloud gateway進行解釋

第二章為Nacos整合Spring Cloud Gateway的配置demo實現服務間的相互呼叫

本章:新增nacos為配置中心

根據第二章的demo稍微改了一下

給父類的pom新增config

<!-- nacos-config
             通過 Nacos Server 和 spring-cloud-starter-alibaba-nacos-config 實現配置的動態變更
             注意:版本 2.1.x.RELEASE 對應的是 Spring Boot 2.1.x 版本。版本 2.0.x.RELEASE 對應的是 Spring Boot 2.0.x 版本,版本 1.5.x.RELEASE 對應的是 Spring Boot 1.5.x 版本。
         -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>

 然後修改gateway服務的配置檔案

  注意:在bootstrap.yml(一定是bootstrap.yml檔案,不是application.yml檔案)

相比上一章 新增了spring.cloud.nacos.config 與 spring.profiles.active

server:
  port: 13008
spring:
  application:
    name: gateway
  profiles:
    active: dev
  cloud:
    nacos:
      discovery:
        server-addr: nacos的地址換成自己的
        ip: 127.0.0.1
        group: gatewaynacos
        namespace: demo
      config:
        server-addr: nacos的地址換成自己的
        #配置內容的資料格式
        file-extension: yaml
        namespace: demo
        enabled: true

  配置了nacos配置中心的地址,與配置中心的擴充套件格式為yaml,和環境

  nacos的DATA ID的格式為:

${prefix}-${spring.profile.active}.${file-extension}

  解釋:

  • prefix: 預設為spring.application.name,當然也可以自己單獨的配置 在spring.cloud.nacos.config.prefix進行配置也是ok的    
  • spring.profile.active:表示為當前環境,當然也可以不配置。不配置話就是${prefix}.${file-extension} 這個樣子的
  • file-extension:這個就是配置內容的資料格式了 我這裡指定的是yaml格式

然後開啟nacos 把你需要的東西放進去。我這裡目前演示的是gateway閘道器的配置。我就把第二章配置的閘道器放進去

我這裡新建好了 新建成這樣的。

然後釋出儲存。到這裡基本的改動也就差不多了

為了演示效果。我先把其他兩個服務的配置也改動一下

serverone的配置檔案,servertwo同理 我就不發了
server:
  port: 9088
spring:
  application:
    name: serverone
  profiles:
    active: dev
  cloud:
    nacos:
      discovery:
        server-addr: 你的nacos的服務地址
        ip: 127.0.0.1
        group: gatewaynacos
        namespace: demo
      config:
        server-addr: 你的nacos的服務地址
        #配置內容的資料格式
        file-extension: yaml
        namespace: demo
        enabled: true

  

現在給serverone的controller添加註釋。@RefreshScope

package com.demo.serverone;

import org.springframework.beans.factory.annotation.Autowired;
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;

@RestController
@RequestMapping(value = "one")
@RefreshScope
public class OneHelloController {

@Value("${username}")
private String username;

@GetMapping("/config")
public String testConfig(){
return this.username;
}
}

  @RefreshScope:可以實現配置的熱載入

然後我在nacos上新建一個serverone-dev.yaml,之後儲存釋出

好了 serverone 服務我也改好了,現在看一下效果。

ok 完成

gateway的配置檔案遷移到nacos 和 serverone裡面的配置檔案也都能讀取到。服務的呼叫也是ok的