在微服务中,首先需要面对的问题就是如何查找服务(软件即服务),其次,就是如何在不同的服务之间进行通信?如何更好更方便的管理应用中的每一个服务,如何建立各个服务之间联系的纽带,由此注册中心诞生(例如淘宝网卖家提供服务,买家调用服务)。
市面上常用注册中心有Zookeeper(雅虎Apache),Eureka(Netfix),Nacos(Alibaba),Consul(Google),那他们分别都有什么特点,我们如何进行选型呢?我们主要从社区活跃度,稳定性,功能,性能等方面进行考虑即可.本次微服务的学习,我们选择Nacos,它很好的支持了阿里的双11活动,不仅可以做注册中心,还可以作为配置中心,稳定性和性能都很好。
Nacos(DynamicNaming and Configuration Service)是一个应用于服务注册与发现、配置管理的平台。它孵化于阿里巴巴,成长于十年双十一的洪峰考验,沉淀了简单易用、稳定可靠、性能卓越的核心竞争力。其官网地址如下:
第一:确保你电脑已配置JAVA_HOME环境变量(Nacos启动时需要),例如:

第二:确保你的MySQL版本为5.7以上(MariaDB10.5以上),例如

第一步:Nacos下载,可在浏览器直接输入如下地址:
第二步:选择对应版本,直接下载,如图所示:

第三步:解压Nacos(最好不要解压到中文目录下),其目录结构如下:

第一步:启动Nacos服务(nacos的bin目录去通过指令启动)。
Linux/Unix/Mac启动命令(standalone代表着单机模式运行,非集群模式):
./startup.sh -m standalone
Windows启动命令(standalone代表着单机模式运行,非集群模式):
startup.cmd -m standalone
Windows启动命令(standalone代表着单机模式运行,非集群模式):
也可以通过startup.cmd启动!

说明:
1)执行命令时要么配置环境变量,要么直接在nacos/bin目录下去执行.
2)nacos启动时需要本地环境变量中配置了JAVA_HOME(对应jdk的安装目录),
3)一定要确保你连接的数据库(nacos_config)是存在的.
4)假如所有的配置都正确,还连不上,检查一下你有几个数据库(mysql,…)
第二步:访问Nacos服务。
打开浏览器,输入http://localhost:8848/nacos地址,出现如下登陆页面:

其中,默认账号密码为nacos/nacos.
创建两个项目Module分别为服务提供者和服务消费者(假如已有则无需创建),两者都要注册到NacosServer中(这个server本质上就是一个web服务,端口默认为8848),然后服务提供者可以为服务消费者提供远端调用服务(例如支付服务为服务提供方,订单服务为服务消费方),如图所示:

- <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
- <parent>
- <artifactId>sinosoft-frameworkartifactId>
- <groupId>com.sinosoftgroupId>
- <version>2.0.2version>
- parent>
- <artifactId>aqsc-regulator-personnel-systemartifactId>
- <version>1.0.0version>
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
-
- <dependency>
- <groupId>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
- dependency>
-
- <dependency>
- <groupId>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-openfeignartifactId>
- dependency>
-
-
- dependencies>
-
- project>
- server:
- port: 8081
- spring:
- application:
- name: sca-provider #进行服务注册必须配置服务名
- cloud:
- nacos:
- discovery:
- server-addr: localhost:8848
或者创建bootstrap.yml配置文件(此配置文件优先级高于其他)
- #spring:
- # cloud:
- # nacos:
- # config:
- # server-addr: 127.0.0.1:8848
- # prefix: regulatorpersonnel
- # file-extension: yaml
- # group: DEFAULT_GROUP
- # namespace: e5194595-5626-404c-8387-ec6e73c075aa
- # discovery:
- # server-addr: 127.0.0.1:8848
- # group: DEFAULT_GROUP
- # namespace: e5194595-5626-404c-8387-ec6e73c075aa
注意:服务名不要使用下划线(“_”),应使用横杠(“-”),这是规则。
- @SpringBootApplication
- @MapperScan({"com.sinosoft.springbootplus.**.mapper"})
- @EnableFeignClients(basePackages={"com.sinosoft.springbootplus"})
- @EnableDiscoveryClient
- public class RegulatorPersonnelApplication {
-
- public static void main(String[] args) {
- // 启动spring-boot-plus
- ConfigurableApplicationContext context = SpringApplication.run(RegulatorPersonnelApplication.class, args);
- // 打印项目信息
- PrintApplicationInfo.print(context);
- }
-
- }
表示开启naocos
2.@EnableFeignClients(basePackages={"com.sinosoft.springbootplus"})
表示注入到spring容器中

第五步:停掉sca-provider服务,然后不断刷新nacos服务列表,检查服务的健康状态。
https://blog.csdn.net/qq_58148854/article/details/125881638
- <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
- <parent>
- <artifactId>sinosoft-frameworkartifactId>
- <groupId>com.sinosoftgroupId>
- <version>2.0.2version>
- parent>
- <artifactId>aqsc-systemartifactId>
- <version>1.0.0version>
- <dependencies>
- <dependency>
- <groupId>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
- dependency>
-
- <dependency>
- <groupId>com.sinosoftgroupId>
- <artifactId>system-cloud-apiartifactId>
- <version>1.0.0version>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>com.sinosoftgroupId>
- <artifactId>ascs-service-center-apiartifactId>
- <version>1.0.0version>
- dependency>
- dependencies>
-
- project>
- <dependency>
- <groupId>com.sinosoftgroupId>
- <artifactId>ascs-service-center-apiartifactId>
- <version>1.0.0version>
- dependency>
- server:
- port: 8090
- spring:
- application:
- name: system #服务注册时,服务名必须配置
- cloud:
- nacos:
- discovery:
- server-addr: localhost:8848 #从哪里去查找服务
- @SpringBootApplication
- @MapperScan({"com.sinosoft.springbootplus.**.mapper"})
- @EnableDiscoveryClient
- @EnableFeignClients(basePackages = "com.sinosoft.springbootplus")
- public class SystemApplication {
-
- public static void main(String[] args) {
- // 启动spring-boot-plus
- ConfigurableApplicationContext context = SpringApplication.run(SystemApplication.class, args);
- // 打印项目信息
- PrintApplicationInfo.print(context);
- }
-
- }
- @SpringBootTest(classes = SystemApplication.class)
- @RunWith(SpringRunner.class)
- @Slf4j
- public class CnareaApiTest {
-
- @Autowired
- private CnareaApiImpl cnareaApi;
-
- @Autowired
- private RegulatorPersonnelServiceApi regulatorPersonnelServiceApi;
-
-
- @Test
- public void test6(){
- List<JgjgOrgTreeDto> jgjgOrgTree = regulatorPersonnelServiceApi.getJgjgOrgTree(1);
- log.info("数据结果:{}",JSON.toJSONString(jgjgOrgTree));
- }
-
- @Test
- public void test7(){
- JgjgOrgTreeDto jgjgOrgTreeDto = regulatorPersonnelServiceApi.getjgjgOrgTreeDto(1539856129095815169L);
- log.info("数据结果:{}",JSON.toJSONString(jgjgOrgTreeDto));
- }
-
- }
1. 首先将接口依赖注入.(pom文件已经依赖进来了)
2. 见text7()方法:通过对象名.方法名()调用. regulatorPersonnelServiceApi.getjgjgOrgTreeDto(1539856129095815169L);
进入到getjgjgOrgTreeDto()方法里面可以看到

图一

图二
由此可见通过图一访问到提供者,在通过图二访问到具体的接口,如下所示


- @SpringBootTest(classes = RegulatorPersonnelApplication.class)
- @RunWith(SpringRunner.class)
- @Slf4j
- public class CnareaApiTest {
-
- @Autowired
- private CnareaApi cnareaApi;
-
- @Test
- public void findByCodeTest(){
- CnareaDto xz = cnareaApi.findByCode("540000000000");
- log.info("获取结果:{}",JSON.toJSONString(xz));
- }
- @Test
- public void toTreeNodeByCodeTest(){
- CnareaTreeNode xz = cnareaApi.toTreeNodeByCode(cnareaApi.findByCode("540000000000"));
- log.info("获取结果:{}",JSON.toJSONString(xz));
- }
- }
1. 首先将接口依赖注入.(pom文件已经依赖进来了)
2. 见text7()方法:通过对象名.方法名()调用. cnareaApi.toTreeNodeByCode(cnareaApi.findByCode("540000000000"));
进入到toTreeNodeByCode()方法里面可以看到

通过system找到项目,通过url地址找到方法.