前言
上一篇博客 我们创建好了微服务项目,本篇博客来体验一下Nacos作为注册中心和配置中心的功能。
注册中心
如果我们启动了一个Nacos注册中心,那么微服务比如订单服务,启动后就可以连上注册中心把自己注册上去,这过程就是服务注册。每个微服务,比如商品服务都应该注册上去,这些微服务是集群化启动的,在多台机器都有,也都应该注册到注册中心。这样注册中心就会保存一个微服务和它所在的机器清单列表,某一天订单服务想要调用商品服务,就先要问一下注册中心这些商品服务在哪些机器上,注册中心会给它返回服务器列表,这样订单服务就可以随便挑一台机器,发起远程调用,这个过程就是服务发现。
注册中心的两个核心功能就是服务注册和服务发现。
Nacos安装
Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service的首字母简称,一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。
-
官网
https://nacos.io/docs/next/quickstart/quick-start/ -
下载安装
-
下载版本:
直接下载解压即可: -
启动命令
切换到bin目录,cmd中输入启动命令即可
startup.cmd -m standalone
启动成功后我们可以访问nacos:
http://localhost:8848/nacos
重要的两个功能,一个是配置管理,就是作为配置中心,另外一个是服务管理,就是作为注册中心。
注册中心-服务注册
服务注册流程如下:
我们来到创建好的项目中进行服务注册测试,先以订单服务为例,先启动这个微服务,由于每个微服务都是springboot的web应用,而我们最大的项目cloud-demo已经继承了springboot,所以每个微服务也是springboot应用,我们只需要在微服务pom文件中导入web依赖,来开发web项目即可。
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
由于要把自己注册到注册中心,所以还要导入alibaba-nacos-discovery依赖,但是我们在services中已经做了公共导入,它下面的每个模块默认都有这个依赖了:
<!-- 服务发现 -->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
来到service-order模块来启动微服务,需要先编写一个主程序(OrderMainApplication):
springboot主程序需要的第一个注解叫@SpringBootApplication,代表它是一个springboot应用:
package com.example.order;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class OrderMainApplication {public static void main(String[] args) {SpringApplication.run(OrderMainApplication.class, args);}
}
启动之前先编写一下配置文件:
这个写好后,就可以启动微服务进行测试,看看能不能把自己注册到注册中心:
来到nacos注册中心:
可以看到service-order实例数为1,健康实例数为1,说明注册到nacos了。
同样把service-product也注册上来。端口用9000:
只要每个服务配置了nacos地址,只要启动了,就会自动连上nacos注册自己。而且我们从启动日志也能看到
来到nacos服务列表中查询:
那再注册中心注册的微服务都是什么东西呢?可以点进详情查看,其实就是微服务的名字以及其所在服务的ip和端口:
我们现在看到的实例数都是1,是因为我们是单机模式,代表之启动了一个微服务,如果启动多个效果是什么呢?我们可以在本机通过启动不同端口的方式模拟一个集群。
同理为了查看效果,我们把商品模块复制两份进行启动,端口分别为9001和9002,然后把所有微服务全部启动
这样我们就在本机通过不同端口模拟启动一个集群,当我们所有服务都启动完成后:
我们去nacos服务列表查询:
这样服务注册的功能,我们就测试通过了,其实非常简单,只要引入了nacos-discovery依赖,配置了nacos地址,项目启动自己就会注册到注册中心,而注册中心保存的就是所有服务的可以访问的ip和对应的端口列表。
注意:测试期间cmd窗口不能关闭,否则相当于nacos服务就没有运行。
注册中心-服务发现
想要通过代码获取到注册中心中注册的所有微服务以及所在机器的ip地址和端口列表,可以通过下面流程进行:
以商品服务为例,先标注开启服务发现功能注解
package com.example.product;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@EnableDiscoveryClient // 开启服务发现功能
@SpringBootApplication
public class ProductMainApplication {public static void main(String[] args) {SpringApplication.run(ProductMainApplication.class, args);}
}
编写一个测试类来测试一下服务发现的api如何使用,测试类的包名要和主程序的包名一样:
要写单元测试,首先得在项目中导入测试依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>
测试类代码:
package com.example.product;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;import java.util.List;@SpringBootTest
public class DiscoveryTest {@AutowiredDiscoveryClient discoveryClient;@Testvoid discoveryClientTest() {for (String service : discoveryClient.getServices()) {System.out.println(service);// 获取ip+portList<ServiceInstance> instances = discoveryClient.getInstances(service);for (ServiceInstance instance : instances) {System.out.println(instance.getHost() + ":" + instance.getPort());}}}
}
运行测试代码:
除了使用DiscoveryClient,nacos组件也提供了叫做NacosServiceDiscovery,用哪个都可以,唯一的区别就是DiscoveryClient是Spring家族的规范,无论你用哪个注册中心,都可以用它的api,而NacosServiceDiscovery是引入nacos时用的api
package com.example.product;import com.alibaba.cloud.nacos.discovery.NacosServiceDiscovery;
import com.alibaba.nacos.api.exception.NacosException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;import java.util.List;@SpringBootTest
public class DiscoveryTest {@AutowiredNacosServiceDiscovery nacosServiceDiscovery;@Testvoid nacosServiceDiscoveryTest() throws NacosException {for (String service : nacosServiceDiscovery.getServices()) {System.out.println(service);// 获取ip+portList<ServiceInstance> instances = nacosServiceDiscovery.getInstances(service);for (ServiceInstance instance : instances) {System.out.println(instance.getHost() + ":" + instance.getPort());}}}
}
这两个api仅作为了解,我们要进行远程调用,第一步就是要服务发现,但是服务发现后来要封装成自动化的过程,无需我们调用底层代码。我们要做的仅仅就是在每个微服务中引入@EnableDiscoveryClient注解,因为微服务之间可能要互相调用,因此在每个微服务中服务发现功能是必备的。
注册中心-编写微服务api
上面我们测试了服务发现功能,基于它可以实现微服务之间的调用,基本流程就是:
我们模拟一个场景来实现上面的流程:
我们来编码实现上面的业务流程,我们先来编写商品服务,由于远程调用都是发起http请求,所以我们在商品服务里面编写一个controller。这controller可以按照商品id查询商品:
这里由于要写javabean类,需要用到Lombok,每个模块都要用,所以这里在services模块的pom中添加lombok依赖:
我们这里就快速简单生成controller、service、bean
代码如下:
Product类:
package com.example.product.bean;import lombok.Data;import java.math.BigDecimal;@Data
public class Product {private Long id;private BigDecimal price;private String productName;private int num;
}
ProductController类:
package com.example.product.controller;import com.example.product.bean.Product;
import com.example.product.service.ProductService;
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;@RestController
public class ProductController {@AutowiredProductService productService;// 查询商品@RequestMapping("/product/{id}")public Product getProduct(@PathVariable("id") Long productId) {