• nacos项目搭建


    注册中心简介

    背景分析

    在微服务中,首先需要面对的问题就是如何查找服务(软件即服务),其次,就是如何在不同的服务之间进行通信?如何更好更方便的管理应用中的每一个服务,如何建立各个服务之间联系的纽带,由此注册中心诞生(例如淘宝网卖家提供服务,买家调用服务)。
    市面上常用注册中心有Zookeeper(雅虎Apache),Eureka(Netfix),Nacos(Alibaba),Consul(Google),那他们分别都有什么特点,我们如何进行选型呢?我们主要从社区活跃度,稳定性,功能,性能等方面进行考虑即可.本次微服务的学习,我们选择Nacos,它很好的支持了阿里的双11活动,不仅可以做注册中心,还可以作为配置中心,稳定性和性能都很好。

    Nacos概述

    Nacos(DynamicNaming and Configuration Service)是一个应用于服务注册与发现、配置管理的平台。它孵化于阿里巴巴,成长于十年双十一的洪峰考验,沉淀了简单易用、稳定可靠、性能卓越的核心竞争力。其官网地址如下:

    Nacos 快速开始

    构建Nacos服务 (准备工作)

    第一:确保你电脑已配置JAVA_HOME环境变量(Nacos启动时需要),例如:

    在这里插入图片描述

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

    在这里插入图片描述

    下载与安装

    第一步:Nacos下载,可在浏览器直接输入如下地址:

    Releases · alibaba/nacos · GitHub

    第二步:选择对应版本,直接下载,如图所示:

    第三步:解压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),然后服务提供者可以为服务消费者提供远端调用服务(例如支付服务为服务提供方,订单服务为服务消费方),如图所示:

    在这里插入图片描述

    第一步:创建服务提供者工程(module名为aqsc-regulator-personnel-system,假如已有则无需创建),继承parent工程(01-sca),其pom.xml文件内容如下: 

    1. <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    3. <modelVersion>4.0.0modelVersion>
    4. <parent>
    5. <artifactId>sinosoft-frameworkartifactId>
    6. <groupId>com.sinosoftgroupId>
    7. <version>2.0.2version>
    8. parent>
    9. <artifactId>aqsc-regulator-personnel-systemartifactId>
    10. <version>1.0.0version>
    11. <dependencies>
    12. <dependency>
    13. <groupId>org.springframework.bootgroupId>
    14. <artifactId>spring-boot-starter-testartifactId>
    15. <scope>testscope>
    16. dependency>
    17. <dependency>
    18. <groupId>com.alibaba.cloudgroupId>
    19. <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
    20. dependency>
    21. <dependency>
    22. <groupId>com.alibaba.cloudgroupId>
    23. <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    24. dependency>
    25. <dependency>
    26. <groupId>org.springframework.cloudgroupId>
    27. <artifactId>spring-cloud-starter-openfeignartifactId>
    28. dependency>
    29. dependencies>
    30. project>

    第二步:创建并修改配置文件application.yml(或者application.properties),实现服务注册,关键代码如下:

    1. server:
    2. port: 8081
    3. spring:
    4. application:
    5. name: sca-provider #进行服务注册必须配置服务名
    6. cloud:
    7. nacos:
    8. discovery:
    9. server-addr: localhost:8848

            或者创建bootstrap.yml配置文件(此配置文件优先级高于其他)

    1. #spring:
    2. # cloud:
    3. # nacos:
    4. # config:
    5. # server-addr: 127.0.0.1:8848
    6. # prefix: regulatorpersonnel
    7. # file-extension: yaml
    8. # group: DEFAULT_GROUP
    9. # namespace: e5194595-5626-404c-8387-ec6e73c075aa
    10. # discovery:
    11. # server-addr: 127.0.0.1:8848
    12. # group: DEFAULT_GROUP
    13. # namespace: e5194595-5626-404c-8387-ec6e73c075aa

    注意:服务名不要使用下划线(“_”),应使用横杠(“-”),这是规则。


    第三步:创建启动类(假如已有则无需定义),关键代码如下:

    1. @SpringBootApplication
    2. @MapperScan({"com.sinosoft.springbootplus.**.mapper"})
    3. @EnableFeignClients(basePackages={"com.sinosoft.springbootplus"})
    4. @EnableDiscoveryClient
    5. public class RegulatorPersonnelApplication {
    6. public static void main(String[] args) {
    7. // 启动spring-boot-plus
    8. ConfigurableApplicationContext context = SpringApplication.run(RegulatorPersonnelApplication.class, args);
    9. // 打印项目信息
    10. PrintApplicationInfo.print(context);
    11. }
    12. }

            注意:                

              1.在SpringBoot 启动类添加@EnableDiscoveryClient注解

                   表示开启naocos

                2.@EnableFeignClients(basePackages={"com.sinosoft.springbootplus"})

                    表示注入到spring容器中

    第四步:启动启动类,然后刷先nacos服务,检测是否服务注册成功,如图所示:

            

    第五步:停掉sca-provider服务,然后不断刷新nacos服务列表,检查服务的健康状态。

    为消费者创建调用的方法:

     https://blog.csdn.net/qq_58148854/article/details/125881638

    消费者服务发现及调用

    第一步: 创建服务消费者工程(module名为aqsc-system,假如已有则无需创建),继承parent工程(01-sca),其pom.xml文件内容如下:

    1. <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    3. <modelVersion>4.0.0modelVersion>
    4. <parent>
    5. <artifactId>sinosoft-frameworkartifactId>
    6. <groupId>com.sinosoftgroupId>
    7. <version>2.0.2version>
    8. parent>
    9. <artifactId>aqsc-systemartifactId>
    10. <version>1.0.0version>
    11. <dependencies>
    12. <dependency>
    13. <groupId>com.alibaba.cloudgroupId>
    14. <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    15. dependency>
    16. <dependency>
    17. <groupId>com.sinosoftgroupId>
    18. <artifactId>system-cloud-apiartifactId>
    19. <version>1.0.0version>
    20. dependency>
    21. <dependency>
    22. <groupId>org.springframework.bootgroupId>
    23. <artifactId>spring-boot-starter-testartifactId>
    24. <scope>testscope>
    25. dependency>
    26. <dependency>
    27. <groupId>com.sinosoftgroupId>
    28. <artifactId>ascs-service-center-apiartifactId>
    29. <version>1.0.0version>
    30. dependency>
    31. dependencies>
    32. project>

    注意:因为要远程调用提供者的接口,所以必须依赖ascs-service-center-api的jar包.

    1. <dependency>
    2. <groupId>com.sinosoftgroupId>
    3. <artifactId>ascs-service-center-apiartifactId>
    4. <version>1.0.0version>
    5. dependency>

    第二步:创建aqsc-system服务中的配置文件application.yml,关键代码如下:

    1. server:
    2. port: 8090
    3. spring:
    4. application:
    5. name: system #服务注册时,服务名必须配置
    6. cloud:
    7. nacos:
    8. discovery:
    9. server-addr: localhost:8848 #从哪里去查找服务

    第四步:创建消费端启动类并实现服务消费,关键代码如下:

    1. @SpringBootApplication
    2. @MapperScan({"com.sinosoft.springbootplus.**.mapper"})
    3. @EnableDiscoveryClient
    4. @EnableFeignClients(basePackages = "com.sinosoft.springbootplus")
    5. public class SystemApplication {
    6. public static void main(String[] args) {
    7. // 启动spring-boot-plus
    8. ConfigurableApplicationContext context = SpringApplication.run(SystemApplication.class, args);
    9. // 打印项目信息
    10. PrintApplicationInfo.print(context);
    11. }
    12. }

    第五步:测试:通过测试类来实现远程调用

    1. @SpringBootTest(classes = SystemApplication.class)
    2. @RunWith(SpringRunner.class)
    3. @Slf4j
    4. public class CnareaApiTest {
    5. @Autowired
    6. private CnareaApiImpl cnareaApi;
    7. @Autowired
    8. private RegulatorPersonnelServiceApi regulatorPersonnelServiceApi;
    9. @Test
    10. public void test6(){
    11. List<JgjgOrgTreeDto> jgjgOrgTree = regulatorPersonnelServiceApi.getJgjgOrgTree(1);
    12. log.info("数据结果:{}",JSON.toJSONString(jgjgOrgTree));
    13. }
    14. @Test
    15. public void test7(){
    16. JgjgOrgTreeDto jgjgOrgTreeDto = regulatorPersonnelServiceApi.getjgjgOrgTreeDto(1539856129095815169L);
    17. log.info("数据结果:{}",JSON.toJSONString(jgjgOrgTreeDto));
    18. }
    19. }

                                   第五步(第一种方法)详解:

                                              1. 首先将接口依赖注入.(pom文件已经依赖进来了)

                                               2. 见text7()方法:通过对象名.方法名()调用.             regulatorPersonnelServiceApi.getjgjgOrgTreeDto(1539856129095815169L);

                                               进入到getjgjgOrgTreeDto()方法里面可以看到

                                                       

                                                                                                                     图一

                                                    

                                                                                                                     图二

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

                                            

            ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​        

    第五步(方法二):测试:通过测试类来实现远程调用          

    1. @SpringBootTest(classes = RegulatorPersonnelApplication.class)
    2. @RunWith(SpringRunner.class)
    3. @Slf4j
    4. public class CnareaApiTest {
    5. @Autowired
    6. private CnareaApi cnareaApi;
    7. @Test
    8. public void findByCodeTest(){
    9. CnareaDto xz = cnareaApi.findByCode("540000000000");
    10. log.info("获取结果:{}",JSON.toJSONString(xz));
    11. }
    12. @Test
    13. public void toTreeNodeByCodeTest(){
    14. CnareaTreeNode xz = cnareaApi.toTreeNodeByCode(cnareaApi.findByCode("540000000000"));
    15. log.info("获取结果:{}",JSON.toJSONString(xz));
    16. }
    17. }

       第五步(第一种方法)详解:

                                              1. 首先将接口依赖注入.(pom文件已经依赖进来了)

                                               2. 见text7()方法:通过对象名.方法名()调用.                                                                               cnareaApi.toTreeNodeByCode(cnareaApi.findByCode("540000000000"));

                                               进入到toTreeNodeByCode()方法里面可以看到

                                               

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

  • 相关阅读:
    redis的持久化
    springboot react 代码生成器
    怎么把本机设置成代理ip?
    Maven仓库Nexus安装部署
    【无标题】
    Android Studio adb WiFi插件的使用
    Qt线程池QThreadPool的使用
    记一次多个Java Agent同时使用的类增强冲突问题及分析
    总结数据结构常用树
    【redis-06】redis进阶知识点-订阅、主从、哨兵、缓存穿透击穿和雪崩
  • 原文地址:https://blog.csdn.net/qq_58148854/article/details/125880888