Spring Cloud Alibaba | Nacos服務註冊與發現
Springboot: 2.1.6.RELEASE
SpringCloud: Greenwich.SR1
如無特殊說明,本系列文章全採用以上版本
上一篇《Spring Cloud Alibaba | Nacos服務中心初探》我們聊了什麼是Nacos以及Nacos如何搭建,這一篇我們接著聊Nacos如何簡單使用。
首先,Nacos是一個服務註冊和服務發現的註冊中心,在Spring Cloud中,可以替代Eureka的功能,我們先聊一下Nacos如何和Spring Cloud整合做一個註冊中心。
整體流程為:
- 先啟動註冊中心Nacos
- 啟動服務的提供者將提供服務,並將服務註冊到註冊中心Nacos上
- 啟動服務的消費者,在Nacos中找到服務並完成消費
1. 服務提供者
新建一個producer的專案,專案依賴如下:
1.1 pom.xml專案依賴
<?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 http://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.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springcloud</groupId>
<artifactId>producer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>producer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.9.0.RELEASE</version>
</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>
增加Nacos的服務發現的依賴:spring-cloud-starter-alibaba-nacos-discovery,根據版本SpringCloud和SpringBoot的版本,這裡我們使用0.9.0.RELEASE版本。其他版本使用請看上一篇《Spring Cloud Alibaba | Nacos服務中心初探》。
1.2 配置檔案application.yml
server:
port: 9000
spring:
application:
name: spring-cloud-nacos-producer
cloud:
nacos:
discovery:
server-addr: localhost:8848
1.3 啟動類ProducerApplication.java
package com.springcloud.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
@EnableDiscoveryClient 註冊服務至Nacos。
1.4 controller
package com.springcloud.producer.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Created with IntelliJ IDEA.
*
* @Date: 2019/7/14
* @Time: 10:04
* @email: inwsy@hotmail.com
* Description:
*/
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(@RequestParam String name) {
return "hello "+name+",producer is ready";
}
}
1.5 測試
啟動服務producer,在瀏覽器訪問連結:http://localhost:9000/hello?name=nacos, 可以看到頁面顯示hello nacos,producer is ready。
開啟Nacos顯示頁面,可以看到服務spring-cloud-nacos-producer正常上線。
到這裡,我們的服務提供者已經正常搭建完畢。
2. 服務消費者
2.1 pom.xml專案依賴
<?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 http://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.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.spring</groupId>
<artifactId>nacos-cosumers</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-cosumers</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
這裡增加了spring-cloud-starter-openfeign依賴包
2.2 配置檔案application.yml
server:
port: 8080
spring:
application:
name: spring-cloud-nacos-consumers
cloud:
nacos:
discovery:
server-addr: localhost:8848
2.3 啟動類NacosCosumersApplication.java
package com.spring.nacoscosumers;
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 NacosCosumersApplication {
public static void main(String[] args) {
SpringApplication.run(NacosCosumersApplication.class, args);
}
}
@EnableFeignClients這個註解是宣告Feign遠端呼叫
2.4 Feign遠端呼叫
建立一個remote介面
package com.spring.nacoscosumers.remote;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(name= "spring-cloud-nacos-producer")
public interface HelloRemote {
@RequestMapping(value = "/hello")
String hello(@RequestParam(value = "name") String name);
}
2.5 web層呼叫遠端介面 Controller
package com.spring.nacoscosumers.controller;
import com.spring.nacoscosumers.remote.HelloRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created with IntelliJ IDEA.
*
* @Date: 2019/7/14
* @Time: 10:24
* @email: inwsy@hotmail.com
* Description:
*/
@RestController
public class HelloController {
@Autowired
HelloRemote helloRemote;
@RequestMapping("/hello/{name}")
public String index(@PathVariable("name") String name) {
return helloRemote.hello(name);
}
}
2.6 測試
啟動服務消費者nacos-consumers,開啟瀏覽器訪問連結:http://localhost:8080/hello/nacos, 這時頁面正常返回hello nacos,producer is ready,證明我們的已經通過Nacos作為註冊中心已經正常提供了服務註冊與發現。
3. 整合Gateway
上面介紹了Nacos可以和Feign整合使用,更多的情況下,我們需要和API閘道器來整合。
這裡我們還是使用之前的服務提供者,新建一個服務閘道器。
這裡我們使用了Gateway做演示,想使用Zuul的朋友可以作為參考,在原有Zuul+Eureka的基礎上只需要更換配置和依賴包就可以,無需其他過多的修改。
3.1 pom.xml專案依賴
<?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 http://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.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.spring</groupId>
<artifactId>nacos-gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-gateway</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2 配置檔案application.xml
server:
port: 8088
spring:
application:
name: spring-cloud-nacos-gateway
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true
routes:
- id: hello_route
#格式為:lb://應用註冊服務名
uri: lb://spring-cloud-nacos-producer
predicates:
- Method=GET
3.3 啟動類NacosGatewayApplication.java
package com.spring.nacosgateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class NacosGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(NacosGatewayApplication.class, args);
}
}
3.4 測試
我們啟動Gateway,開啟瀏覽器訪問連結:http://localhost:8088/spring-cloud-nacos-producer/hello?name=nacos, 瀏覽器正常返回:hello nacos,producer is ready, 證明我們通過服務閘道器來訪問服務是正常的。
最後,我們開啟看一下Nacos的服務列表:
這裡我把消費者服務停止掉了,可以看到Nacos可以實時的顯示出來。
Nacos就介紹到這裡,有不清楚的可以給我留言~