什麼是 Nacos
Nacos 是 Spring Cloud Alibaba 生態中的重要組件之一,用於發現、配置和管理微服務。Nacos 提供了一組簡單易用的特性集,可以快速實現
動態服務發現、服務配置、服務元數據及流量管理。下載安裝
<code># 下載源碼
git clone https://github.com/alibaba/nacos.git
# 安裝到本地倉庫
cd nacos/
mvn -Prelease-nacos clean install -U/<code>
啟動服務
<code>cd distribution/target/nacos-server-0.7.0/nacos/bin
# Linux
./startup.sh -m standalone
# Windows
startup.cmd/<code>
啟動完成後,訪問 http://192.168.127.132:8848/nacos
登錄
賬號:nacos
密碼:nacos
Nacos 控制檯
服務提供者
引入依賴
在 pom.xml 中主要添加依賴 spring-cloud-starter-alibaba-nacos-discovery
<code><dependency>
<groupid>org.springframework.cloud/<groupid>
<artifactid>spring-cloud-starter-alibaba-nacos-discovery/<artifactid>
/<dependency>/<code>
相關配置
在 application.yml 中主要配置 Nacos 的地址
<code>spring:
application:
# 服務名
name: service-provider
cloud:
nacos:
discovery:
# 服務註冊中心
server-addr: 192.168.127.132:8848/<code>
在 Application 入口類中主要添加註解 @EnableDiscoveryClient 開啟服務註冊發現功能
<code>import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}/<code>
Controller
創建一個 RestController 提供 RestFul API 用於測試提供服務
<code>import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EchoController {
@GetMapping(value = "/echo/{keyword}")
public String echo(@PathVariable(value = "keyword") String keyword) {
return "Hello Nacos Provider " + keyword + ";
}
}/<code>
驗證是否成功
啟動項目,瀏覽器訪問 http://192.168.127.132:8848/nacos
提供者服務已上線
會發現有一個服務已經註冊在 Nacos 中,服務名為 service-provider
此時訪問網址 http://localhost:8848/echo/hi,瀏覽器會響應以下內容:
<code>Hello Nacos Provider hi/<code>
服務消費者
引入依賴
在 pom.xml 中主要添加 spring-cloud-starter-alibaba-nacos-discovery 和 spring-cloud-starter-openfeign 依賴
<code><dependency>
<groupid>org.springframework.cloud/<groupid>
<artifactid>spring-cloud-starter-alibaba-nacos-discovery/<artifactid>
/<dependency>
<dependency>
<groupid>org.springframework.cloud/<groupid>
<artifactid>spring-cloud-starter-openfeign/<artifactid>
/<dependency>/<code>
相關配置
在 Application 入口類中同樣添加註解 @EnableDiscoveryClient 開啟服務註冊與發現功能,添加註解 @EnableFeignClients開啟 Feign 客戶端
<code>import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}/<code>
Service
創建業務層接口,通過 @FeignClient("服務名") 註解來指定具體調用哪個服務
<code>import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(value = "service-provider")
public interface EchoService {
@GetMapping(value = "/echo/{keyword}")
String echo(@PathVariable(value = "keyword") String keyword);
}/<code>
Controller
創建 Controller 調用服務
<code>import com.funtl.spring.cloud.alibaba.consumer.service.EchoService;
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 TestEchoController {
@Autowired
private EchoService echoService;
@GetMapping(value = "/feign/echo/{keyword}")
public String echoFeign(@PathVariable(value = "keyword") String keyword) {
return echoService.echo(keyword);
}
}/<code>
驗證是否成功
啟動項目,訪問網址 http://localhost:8080/feign/echo/hi
會發現有一個名為 service-consumer 的服務已經註冊到 Nacos 中
此時再訪問 http://localhost:8080/echo/hi,瀏覽器會同樣響應以下內容:
<code>Hello Nacos Provider hi/<code>
資料
閱讀更多 程序員諸葛 的文章