spring-boot 配置中心 nacos 簡易部署+ 手把手教
最近搞一個配置中心,看了很多大牛的講解愣是沒有學會,於是終於學廢了~.~
再後來,使用了一個簡單的方法,版本啥的也沒管,然後就成了~,特此記錄一下(炫耀一下。。。)
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、還可以作為註冊中心、還可以配置分散式的等等,要學習的地方還有很多~!