• Nacos--服务搭建以及SpringBoot整合--方法/实例


    原文网址:Nacos--服务搭建以及SpringBoot整合--方法/实例_IT利刃出鞘的博客-CSDN博客

    简介

    说明

            本文用示例介绍如何搭建Nacos服务并在SpringBoot中整合Nacos。

    官方网址

    官网文档:https://nacos.io/zh-cn/docs/what-is-nacos.html

    github网址:https://github.com/alibaba/nacos

    1.安装Nacos

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

    本处我下载的版本为:nacos-server-1.4.3

    2.运行Nacos

    Linux/Unix/Mac

    启动命令(standalone代表着单机模式运行,非集群模式):

    sh startup.sh -m standalone

    如果您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:

    bash startup.sh -m standalone

    Windows

    启动命令(standalone代表着单机模式运行,非集群模式):

    startup.cmd -m standalone

    本处是Windows,运行结果:

    3.访问nacos

    访问地址:http://localhost:8848/nacos
    账号:nacos
    密码:nacos

    配置管理

    服务管理

    4.创建SpringBoot项目

    依赖

    主要是nacos的依赖:

    1. <dependencyManagement>
    2. <dependencies>
    3. <dependency>
    4. <groupId>com.alibaba.cloud</groupId>
    5. <artifactId>spring-cloud-alibaba-dependencies</artifactId>
    6. <version>2021.1</version>
    7. <type>pom</type>
    8. <scope>import</scope>
    9. </dependency>
    10. </dependencies>
    11. </dependencyManagement>
    12. <dependencies>
    13. <dependency>
    14. <groupId>org.springframework.boot</groupId>
    15. <artifactId>spring-boot-starter-web</artifactId>
    16. </dependency>
    17. <dependency>
    18. <groupId>com.alibaba.cloud</groupId>
    19. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    20. </dependency>
    21. <dependency>
    22. <groupId>com.alibaba.cloud</groupId>
    23. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    24. <exclusions>
    25. <exclusion>
    26. <groupId> springframework.cloud</groupId>
    27. <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    28. </exclusion>
    29. </exclusions>
    30. </dependency>
    31. <dependency>
    32. <groupId>org.springframework.cloud</groupId>
    33. <artifactId>spring-cloud-loadbalancer</artifactId>
    34. </dependency>
    35. <dependencies>

    整个pom.xml:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <parent>
    6. <groupId>org.springframework.boot</groupId>
    7. <artifactId>spring-boot-starter-parent</artifactId>
    8. <version>2.4.13</version>
    9. </parent>
    10. <artifactId>order-core</artifactId>
    11. <version>0.0.1-SNAPSHOT</version>
    12. <name>order</name>
    13. <packaging>jar</packaging>
    14. <description>Demo project for Spring Boot</description>
    15. <properties>
    16. <maven.compiler.source>8</maven.compiler.source>
    17. <maven.compiler.target>8</maven.compiler.target>
    18. </properties>
    19. <dependencyManagement>
    20. <dependencies>
    21. <dependency>
    22. <groupId>com.alibaba.cloud</groupId>
    23. <artifactId>spring-cloud-alibaba-dependencies</artifactId>
    24. <version>2021.1</version>
    25. <type>pom</type>
    26. <scope>import</scope>
    27. </dependency>
    28. </dependencies>
    29. </dependencyManagement>
    30. <dependencies>
    31. <dependency>
    32. <groupId>org.springframework.boot</groupId>
    33. <artifactId>spring-boot-starter-web</artifactId>
    34. </dependency>
    35. <dependency>
    36. <groupId>com.alibaba.cloud</groupId>
    37. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    38. </dependency>
    39. <dependency>
    40. <groupId>com.alibaba.cloud</groupId>
    41. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    42. <exclusions>
    43. <exclusion>
    44. <groupId> springframework.cloud</groupId>
    45. <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    46. </exclusion>
    47. </exclusions>
    48. </dependency>
    49. <dependency>
    50. <groupId>org.springframework.cloud</groupId>
    51. <artifactId>spring-cloud-loadbalancer</artifactId>
    52. </dependency>
    53. <dependency>
    54. <groupId>org.springframework.cloud</groupId>
    55. <artifactId>spring-cloud-starter-openfeign</artifactId>
    56. </dependency>
    57. <!--支持feign缓存-->
    58. <dependency>
    59. <groupId>com.github.ben-manes.caffeine</groupId>
    60. <artifactId>caffeine</artifactId>
    61. <version>2.9.3</version>
    62. </dependency>
    63. <dependency>
    64. <groupId>org.springframework</groupId>
    65. <artifactId>spring-context-support</artifactId>
    66. </dependency>
    67. </dependencies>
    68. <build>
    69. <plugins>
    70. <plugin>
    71. <groupId>org.springframework.boot</groupId>
    72. <artifactId>spring-boot-maven-plugin</artifactId>
    73. <version>2.4.13</version>
    74. </plugin>
    75. </plugins>
    76. </build>
    77. </project>

    配置文件

    bootstrap.yml

    1. spring:
    2. application:
    3. name: order
    4. cloud:
    5. nacos:
    6. config:
    7. server-addr: 127.0.0.1:8848

    application.yml

    1. server:
    2. port: 9012
    3. spring:
    4. application:
    5. name: order
    6. cloud:
    7. nacos:
    8. discovery:
    9. server-addr: 127.0.0.1:8848
    10. feign:
    11. httpclient:
    12. enabled: false
    13. okhttp:
    14. enabled: true

    5.启动SpringBoot项目

    使用Idea启动order项目:

    nacos结果:

    可以看到,服务已经成功注册到nacos。

    此时即可使用SpringCloud的feign进行微服务调用,本处省略。

  • 相关阅读:
    剑指Offer 09.用两个栈实现队列
    使用ref如何获取到input标签中的值
    Java链接redis集群
    Vue3 - 路由 Vue-router 4.X(配置与使用教程)
    【SpringBoot】SpringBoot自定义banner,成千上万种可供选择,当然也可以自定义生成哦
    小干货~ NFS在Linux系统中的应用
    软件设计模式系列之二十四——模板方法模式
    互联网刚需岗位 前景一片大好?
    01_Linux字符设备驱动开发
    面试再也不怕问ThreadLocal了
  • 原文地址:https://blog.csdn.net/feiying0canglang/article/details/125415600