Spring Cloud Alibaba | Nacos服務注冊與發現


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集成做一個注冊中心。

整體流程為:

  1. 先啟動注冊中心Nacos
  2. 啟動服務的提供者將提供服務,並將服務注冊到注冊中心Nacos上
  3. 啟動服務的消費者,在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就介紹到這里,有不清楚的可以給我留言~

Github-示例代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM