
Nacos 配置:


| Spring Cloud Version | Spring Cloud Alibaba Version | Spring Boot Version |
|---|---|---|
|
Spring Cloud Hoxton.SR8 |
2.2.3.RELEASE |
2.3.2.RELEASE |
|
Spring Cloud Greenwich.SR6 |
2.1.3.RELEASE |
2.1.13.RELEASE |
|
Spring Cloud Hoxton.SR8 |
2.2.2.RELEASE |
2.3.2.RELEASE |
|
Spring Cloud Hoxton.SR3 |
2.2.1.RELEASE |
2.2.5.RELEASE |
|
Spring Cloud Hoxton.RELEASE |
2.2.0.RELEASE |
2.2.X.RELEASE |
|
Spring Cloud Greenwich |
2.1.2.RELEASE |
2.1.X.RELEASE |
|
Spring Cloud Finchley |
2.0.3.RELEASE |
2.0.X.RELEASE |
|
Spring Cloud Edgware |
1.5.1.RELEASE(停止維護,建議升級) |
1.5.X.RELEASE |
pom.xml檔案添加包:
<!--nacos-config-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--nacos-discovery-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
bootstrap.yml優先于application.yml
bootstrap.yml配置:
server:
port: 3377
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery:
server-addr: localhost:8848 # 服務注冊中心地址
config:
server-addr: localhost:8848 #配置中心地址
file-extension: yaml #指定yaml格式的配置
#group: TEST_GROUP #默認:DEFAULT_GROUP
#namespace: 00ab1957-eb3f-4555-96ca-3d20817e854d #命名空間id 默認:public
# ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
# nacos-config-client-dev.yaml
application.yml配置:
spring:
profiles:
active: dev # 表示激活開發環境
類配置:
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;
@RestController
@RefreshScope //支持controller的動態重繪
public class ConfigClientController
{
@Value("${config.info}")
private String configInfo;
@GetMapping("/config/info")
public String getConfigInfo() {
return configInfo;
}
}
啟動類:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class NacosConfigClientApp {
public static void main(String[] args) {
SpringApplication.run(NacosConfigClientApp.class,args);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/213506.html
標籤:其他
上一篇:技術點10:EL運算式
下一篇:技術點11:JSTL標簽庫
