Nacos是Dynamic Naming and Configuration Service的縮寫。What’s Nacos?
下面結合SpringBoot項目,為你介紹Nacos的基本功能以及如何使用Feign進行微服務間的通信。
一、Nacos的基本功能
Nacos是阿里巴巴開源的一個更易于構建云原生應用的動態服務發現、配置管理和服務管理平臺。它的主要功能包括:
- 服務發現與注冊:微服務可以在Nacos中注冊自己,并發現其他服務。
- 配置管理:集中管理所有微服務的配置。
- 服務健康監測:檢查服務的健康狀態。
- 動態DNS服務:通過域名來訪問服務。
二、依賴配置
要在SpringBoot項目中使用Nacos和Feign,需要在pom.xml
中添加以下依賴:
<!-- 服務注冊與發現 -->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency><!-- 配置管理 -->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency><!-- Feign客戶端 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency><!-- Spring Cloud 依賴管理 -->
<dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR12</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.9.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>
三、配置文件
在application.properties
或application.yml
中配置Nacos服務地址:
# 應用名稱
spring:application:name: service-consumercloud:nacos:discovery:server-addr: 127.0.0.1:8848 # Nacos服務器地址config:server-addr: 127.0.0.1:8848 # Nacos配置中心地址file-extension: yaml # 配置文件格式# 端口號
server:port: 8080
四、代碼實現
1. 服務提供者(service-provider)
首先創建一個簡單的服務提供者,提供一個API接口:
package com.example.provider.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@GetMapping("/hello/{name}")public String sayHello(@PathVariable String name) {return "Hello, " + name + "! I'm from service-provider.";}
}
啟動類上添加@EnableDiscoveryClient
注解:
package com.example.provider;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDiscoveryClient
public class ServiceProviderApplication {public static void main(String[] args) {SpringApplication.run(ServiceProviderApplication.class, args);}
}
2. 服務消費者(service-consumer)
創建Feign客戶端接口,調用服務提供者的API:
package com.example.consumer.feign;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;@FeignClient(name = "service-provider") // 服務提供者的應用名稱
public interface HelloFeignClient {@GetMapping("/hello/{name}") // 服務提供者的API路徑String sayHello(@PathVariable String name);
}
創建Controller,使用Feign客戶端調用服務:
package com.example.consumer.controller;import com.example.consumer.feign.HelloFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ConsumerController {@Autowiredprivate HelloFeignClient helloFeignClient;@GetMapping("/call/{name}")public String callProvider(@PathVariable String name) {return helloFeignClient.sayHello(name);}
}
啟動類上添加@EnableFeignClients
注解:
package com.example.consumer;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableFeignClients
public class ServiceConsumerApplication {public static void main(String[] args) {SpringApplication.run(ServiceConsumerApplication.class, args);}
}
五、Nacos配置中心使用示例
創建一個配置類,讀取Nacos配置中心的配置:
package com.example.consumer.config;import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;@Component
@RefreshScope // 支持配置動態刷新
public class AppConfig {@Value("${app.name:default}")private String appName;@Value("${app.version:1.0.0}")private String appVersion;public String getAppName() {return appName;}public String getAppVersion() {return appVersion;}
}
創建Controller,獲取配置信息:
package com.example.consumer.controller;import com.example.consumer.config.AppConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ConfigController {@Autowiredprivate AppConfig appConfig;@GetMapping("/config")public String getConfig() {return "App Name: " + appConfig.getAppName() + ", Version: " + appConfig.getAppVersion();}
}
在Nacos配置中心創建配置文件service-consumer.yaml
:
app:name: MyServiceConsumerversion: 1.0.1
六、測試步驟
- 啟動Nacos服務器(下載并解壓Nacos,運行
startup.sh -m standalone
)。 - 啟動服務提供者(service-provider)。
- 啟動服務消費者(service-consumer)。
- 訪問服務消費者的API:
http://localhost:8080/call/World
,會返回Hello, World! I'm from service-provider.
- 訪問配置信息:
http://localhost:8080/config
,會返回Nacos配置中心的配置信息。
通過上述示例,你可以看到Nacos的服務注冊與發現、配置管理功能,以及Feign如何簡化微服務間的通信。