使用 Spring Cloud Alibaba Nacos Discovery 实现服务注册与发现

Posted by 彭超 on 2020-01-16
Estimated Reading Time 4 Minutes
Words 953 In Total
Viewed Times

什么是 Nacos

Nacos 是 Spring Cloud Alibaba 生态中的重要组件之一,用于发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,可以快速实现 动态服务发现服务配置服务元数据及流量管理

部署 Nacos 控制台

下载地址:https://github.com/alibaba/nacos/releases

解压后启动服务

1
2
3
4
5
$ cd bin
# Linux 环境下启动
$ startup.sh -m standalone
# Windows 环境下启动
$ startup.cmd

启动完成后,访问 Nacos 控制台 http://{ip}:8848/nacos

用户名和密码均为 nacos

服务提供者

引入依赖

在统一依赖管理项目中继承 Spring Boot 2.0.6.RELEASE 父项目,并声明 Spring Cloud Alibaba 0.2.1.RELEASE 依赖版本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

在当前项目 pom.xml 中主要添加依赖 spring-cloud-starter-alibaba-nacos-discovery

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

相关配置

application.yml 中主要配置 Nacos 的地址

1
2
3
4
5
6
7
8
9
spring:
application:
# 服务名
name: service-provider
cloud:
nacos:
discovery:
# 服务注册中心
server-addr: 192.168.127.132:8848

Application 入口类中主要添加注解 @EnableDiscoveryClient 开启服务注册发现功能

1
2
3
4
5
6
7
8
9
10
11
12
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);
}
}

Controller

创建一个 RestController 提供 RestFul API 用于测试提供服务

1
2
3
4
5
6
7
8
9
10
11
12
13
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 + ";
}
}

验证是否成功

启动项目,浏览器访问 http://192.168.127.132:8848/nacos

提供者服务已上线

会发现有一个服务已经注册在 Nacos 中,服务名为 service-provider

此时访问网址 http://localhost:8848/echo/hi,浏览器会响应以下内容:

1
Hello Nacos Provider hi

服务消费者

引入依赖

pom.xml 中主要添加 spring-cloud-starter-alibaba-nacos-discoveryspring-cloud-starter-openfeign 依赖

1
2
3
4
5
6
7
8
9
<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>

相关配置

Application 入口类中同样添加注解 @EnableDiscoveryClient 开启服务注册与发现功能,添加注解 @EnableFeignClients 开启 Feign 客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
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);
}
}

Service

创建业务层接口,通过 @FeignClient("服务名") 注解来指定具体调用哪个服务

1
2
3
4
5
6
7
8
9
10
11
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);

}

Controller

创建 Controller 调用服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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);
}
}

验证是否成功

启动项目,访问网址 http://localhost:8080/feign/echo/hi

会发现有一个名为 service-consumer 的服务已经注册到 Nacos 中

此时再访问 http://localhost:8080/echo/hi,浏览器会同样响应以下内容:

1
Hello Nacos Provider hi

If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !