• springcloud整合nacos实现服务注册


    Nacos是一个开源的分布式系统服务和基础设施解决方案,用于实现动态服务发现、配置管理和服务治理。它可以帮助开发人员和运维团队更好地管理微服务架构中的服务实例、配置信息和服务调用。

    Nacos提供了服务注册与发现、动态配置管理、服务路由和负载均衡等功能,可以帮助企业构建和管理基于微服务架构的应用程序。它支持多种环境和语言,包括Java、Go、Python和Node.js等,可以灵活地集成到各种不同的应用程序中。

    Nacos的特点包括高可用性、可扩展性、易用性和丰富的功能,使其成为构建和管理分布式系统的理想选择。作为一个开源项目,Nacos还拥有一个活跃的社区和丰富的文档资源,可以帮助用户更好地理解和使用该系统。

     环境准备

    插件版本
    jdk21
    springboot

    3.0.11

    springcloud

    2022.0.4

    springcloudalibaba

    2022.0.0.0

    nacos2.2.3(稳定版)

    代码编写 

     pom

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>3.0.11version>
    9. <relativePath/>
    10. parent>
    11. <groupId>com.examplegroupId>
    12. <artifactId>platformartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>platformname>
    15. <description>platformdescription>
    16. <properties>
    17. <java.version>21java.version>
    18. <spring-cloud.version>2022.0.0spring-cloud.version>
    19. <spring-cloud-alibaba.version>2022.0.0.0spring-cloud-alibaba.version>
    20. properties>
    21. <dependencies>
    22. <dependency>
    23. <groupId>org.springframework.bootgroupId>
    24. <artifactId>spring-boot-starter-webartifactId>
    25. dependency>
    26. <dependency>
    27. <groupId>org.mybatis.spring.bootgroupId>
    28. <artifactId>mybatis-spring-boot-starterartifactId>
    29. <version>3.0.2version>
    30. dependency>
    31. <dependency>
    32. <groupId>com.baomidougroupId>
    33. <artifactId>mybatis-plus-boot-starterartifactId>
    34. <version>3.5.3.2version>
    35. dependency>
    36. <dependency>
    37. <groupId>com.mysqlgroupId>
    38. <artifactId>mysql-connector-jartifactId>
    39. <scope>runtimescope>
    40. dependency>
    41. <dependency>
    42. <groupId>org.springframework.bootgroupId>
    43. <artifactId>spring-boot-starter-testartifactId>
    44. <scope>testscope>
    45. dependency>
    46. <dependency>
    47. <groupId>org.mybatis.spring.bootgroupId>
    48. <artifactId>mybatis-spring-boot-starter-testartifactId>
    49. <version>3.0.2version>
    50. <scope>testscope>
    51. dependency>
    52. <dependency>
    53. <groupId>org.projectlombokgroupId>
    54. <artifactId>lombokartifactId>
    55. <version>edge-SNAPSHOTversion>
    56. dependency>
    57. <dependency>
    58. <groupId>javax.validationgroupId>
    59. <artifactId>validation-apiartifactId>
    60. <version>2.0.1.Finalversion>
    61. dependency>
    62. <dependency>
    63. <groupId>org.apache.commonsgroupId>
    64. <artifactId>commons-lang3artifactId>
    65. dependency>
    66. <dependency>
    67. <groupId>com.alibaba.cloudgroupId>
    68. <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
    69. dependency>
    70. <dependency>
    71. <groupId>com.alibaba.cloudgroupId>
    72. <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    73. dependency>
    74. <dependency>
    75. <groupId>org.springframework.cloudgroupId>
    76. <artifactId>spring-cloud-starter-openfeignartifactId>
    77. dependency>
    78. <dependency>
    79. <groupId>org.springframework.cloudgroupId>
    80. <artifactId>spring-cloud-starter-bootstrapartifactId>
    81. dependency>
    82. <dependency>
    83. <groupId>com.alibabagroupId>
    84. <artifactId>fastjsonartifactId>
    85. <version>2.0.41version>
    86. dependency>
    87. <dependency>
    88. <groupId>com.auth0groupId>
    89. <artifactId>java-jwtartifactId>
    90. <version>4.4.0version>
    91. dependency>
    92. <dependency>
    93. <groupId>org.springframework.cloudgroupId>
    94. <artifactId>spring-cloud-loadbalancerartifactId>
    95. dependency>
    96. dependencies>
    97. <dependencyManagement>
    98. <dependencies>
    99. <dependency>
    100. <groupId>org.springframework.cloudgroupId>
    101. <artifactId>spring-cloud-dependenciesartifactId>
    102. <version>${spring-cloud.version}version>
    103. <type>pomtype>
    104. <scope>importscope>
    105. dependency>
    106. <dependency>
    107. <groupId>com.alibaba.cloudgroupId>
    108. <artifactId>spring-cloud-alibaba-dependenciesartifactId>
    109. <version>${spring-cloud-alibaba.version}version>
    110. <type>pomtype>
    111. <scope>importscope>
    112. dependency>
    113. dependencies>
    114. dependencyManagement>
    115. <repositories>
    116. <repository>
    117. <id>projectlombok.orgid>
    118. <url>https://projectlombok.org/edge-releasesurl>
    119. repository>
    120. repositories>
    121. <build>
    122. <plugins>
    123. <plugin>
    124. <groupId>org.springframework.bootgroupId>
    125. <artifactId>spring-boot-maven-pluginartifactId>
    126. plugin>
    127. plugins>
    128. build>
    129. project>

    配置文件

    1. spring:
    2. application:
    3. name: platform
    4. cloud:
    5. nacos:
    6. server-addr: ip:port
    7. config:
    8. file-extension: yml
    9. group: DEFAULT_GROUP
    10. prefix: ${sping.application.name}

    启动类

    1. @SpringBootApplication
    2. // 这里必须有
    3. @EnableFeignClients
    4. public class PlatformApplication {
    5. public static void main(String[] args) {
    6. SpringApplication.run(PlatformApplication.class, args);
    7. }
    8. }

    测试类 

    1. @SpringBootApplication
    2. @EnableFeignClients
    3. @EnableCaching
    4. @EnableScheduling
    5. @EnableDiscoveryClient
    6. @RefreshScope
    7. public class PlatformApplication {
    8. public static void main(String[] args) {
    9. SpringApplication.run(PlatformApplication.class, args);
    10. }
    11. }

    启动后就可以将服务注册到nacos中

     

  • 相关阅读:
    从源码视角彻底搞懂Linux线程实现原理
    PyTorch:GPU的使用
    PostgreSQL安全
    常见的应用层协议都有哪些?【面试官可能会问系列】
    OKhttp上传图片视频
    提前预体验阿里大模型“通义千问”的方法来了!
    Java基础(十九)Map
    【LeetCode刷题】--39.组合总和
    YOLO目标检测——跌倒摔倒数据集【含对应voc、coco和yolo三种格式标签】
    acwing算法提高之图论--单源最短路的综合应用
  • 原文地址:https://blog.csdn.net/weixin_44808225/article/details/134467546