最近搞一個配置中心,看了很多大牛的講解愣是沒有學會,於是終於學廢了~.~
再後來,使用了一個簡單的方法,版本啥的也沒管,然後就成了~,特此記錄一下(炫耀一下。。。)
1、項目創建
1)idea中創建項目 選擇創建springboot的項目:
2)選擇java8,填寫項目名demo後點擊next
3)選擇web的,就這一個就行,因爲是服務,需要一直啓動。然後點擊next再點擊finish結束。
4)配置pom(依賴就這四個就行)
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version> <!-- Spring Boot版本要低於2.4,否則啓動應用會報錯-->
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>centers</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>centers</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
如果你沒有倉庫可以下載這些依賴,那配置一下alibaba的庫,一個就夠用了。
5)創建boostrap.yml
spring:
application:
name: nacos-config
## 當前環境,這個和dataId有關-> ${prefix}-${spring.profiles.active}.${file-extension}
##
profiles:
active: dev
cloud:
nacos:
config:
## nacos的地址,作爲配置中心
server-addr: 127.0.0.1:8848
## 配置內容的數據格式,目前只支持 properties 和 yaml 類型,這個和dataId有關-> ${prefix}-${spring.profiles.active}.${file-extension}
file-extension: properties
management:
endpoints:
web:
exposure:
## yml文件中存在特殊字符,必須用單引號包含,否則啓動報錯
include: '*'
上面三個有顏色的文字後面有用(nacos中的配置文件名:nacos-config-dev.properties)
6)創建測試類
package com.example.centers;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//refreshscope這個註解用於動態加載配置,即你遠程改了配置後立即生效,不加的話,每次改都要重啓項目
@RestController
@RefreshScope
@RequestMapping("/nacos")
public class NacosController {
@Value("${config.version}")
private String version;
@GetMapping("/test/{id}")
public String test(@PathVariable("id")Integer id){
return "accept one msg id="+id+"----- version="+version;
}
}
7)項目最終結構:
2、nacos下載、啓動、配置項
1)下載地址:https://github.com/alibaba/nacos/releases 使勁向下劃,找到zip,這個是windows上啓動的,上面gz的那個是linux上用的。
2)啓動:
解壓後進入bin目錄,雙擊startup.cmd ,等一會彈出運行框。
出現 2022-04-15 21:29:36,967 INFO Completed initialization in 6 ms 就是啓動完成了。
3)配置:
啓動完成後,進入http://localhost:8848/nacos
1)點擊新增的加號
2)填寫信息,照着填寫,然後點擊發布,就好了
3、測試
1)啓動項目
2)瀏覽器輸入:http://localhost:8080/nacos2/test/1
頁面顯示:accept one msg id=1----- version=1314
大功告成!!
3)修改一下nacos的nacos-config-deb.properties配置裏的值,隨便設置一個值,如:
config.version=110
4)刷新瀏覽器的頁面發現,頁面顯示:accept one msg id=1----- version=110
完美~!
完美~!
完美~!
nacos還可以鏈接mysql、還可以作爲註冊中心、還可以配置分佈式的等等,要學習的地方還有很多~!