Nacos - SpringBoot 快速開始

Nacos - SpringBoot 快速開始


1、主要實現

通過依賴 nacos-config-spring-boot-starter來實現配置的動態變更

通過依賴 nacos-discovery-spring-boot-starter來實現服務的註冊與發現

2、集成Nacos 進行配置管理

1、添加依賴

<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>nacos-config-spring-boot-starter</artifactId>
    <version>${latest.version}</version>
</dependency>

注意:版本 0.2.x.RELEASE 對應的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 對應的是 Spring Boot 1.x 版本。

2、然後配置nacos 的地址

nacos.config.server-addr=127.0.0.1:8848

3、使用 @NacosPropertySource加載 dataId 爲 example 的配置源,並開啓自動更新:

@SpringBootApplication
@NacosPropertySource(dataId = "example", autoRefreshed = true)
public class NacosConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosConfigApplication.class, args);
    }
}

4、通過 Nacos 的 @NacosValue註解設置屬性值。

@Controller
@RequestMapping("config")
public class ConfigController {

    @NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
    private boolean useLocalCache;

    @RequestMapping(value = "/get", method = GET)
    @ResponseBody
    public boolean get() {
        return useLocalCache;
    }
}

5、啓動 NacosConfigApplication,調用 curl http://localhost:8080/config/get,返回內容是 false

6、nacos 發佈一個配置 dataId 爲example,內容爲useLocalCache=true

7、再次訪問 http://localhost:8080/config/get,此時返回內容爲true,說明程序中的useLocalCache值已經被動態更新了

3、啓用服務發現

1、添加依賴

<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>nacos-discovery-spring-boot-starter</artifactId>
    <version>${latest.version}</version>
</dependency>

注意:版本 0.2.x.RELEASE 對應的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 對應的是 Spring Boot 1.x 版本

2、配置nacos 的地址

nacos.discovery.server-addr=127.0.0.1:8848

3、使用 @NacosInjected注入 Nacos 的 NamingService 實例

@Controller
@RequestMapping("discovery")
public class DiscoveryController {

    @NacosInjected
    private NamingService namingService;

    @RequestMapping(value = "/get", method = GET)
    @ResponseBody
    public List<Instance> get(@RequestParam String serviceName) throws NacosException {
        return namingService.getAllInstances(serviceName);
    }
}

@SpringBootApplication
public class NacosDiscoveryApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosDiscoveryApplication.class, args);
    }
}

4、啓動 NacosDiscoveryApplication,調用 curl http://localhost:8080/discovery/get?serviceName=example,此時返回爲空 JSON 數組[]

5、通過nacos 註冊一個名爲 example 的服務

6、再次訪問 curl http://localhost:8080/discovery/get?serviceName=example,此時返回內容爲:

[
  {
    "instanceId": "127.0.0.1-8080-DEFAULT-example",
    "ip": "127.0.0.1",
    "port": 8080,
    "weight": 1.0,
    "healthy": true,
    "cluster": {
      "serviceName": null,
      "name": "",
      "healthChecker": {
        "type": "TCP"
      },
      "defaultPort": 80,
      "defaultCheckPort": 80,
      "useIPPort4Check": true,
      "metadata": {}
    },
    "service": null,
    "metadata": {}
  }
]
發佈了442 篇原創文章 · 獲贊 1379 · 訪問量 235萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章