• Spring Cloud Alibaba系列之nacos:(3)服务集成Nacos


    目录

    传送门

    启动服务发现

    版本说明

    毕业版本依赖关系(推荐使用)

    2021.x 分支

    2.2.x 分支

    项目结构

    添加依赖

    配置服务注册到 Nacos server

     开启服务注册发现功能 

    启动服务,观察效果

    服务注册

    服务调用

    服务消费

    服务提供

    服务调用

    示例工程代码

    可能出现的问题

    java.lang.NoClassDefFoundError: com/alibaba/nacos/client/logging/NacosLogging

    com.alibaba.nacos.api.exception.NacosException: Client not connected, current status:STARTING

    参考资料


    传送门

    Spring Cloud Alibaba系列之nacos:(1)安装

    Spring Cloud Alibaba系列之nacos:(2)单机模式支持mysql

    启动服务发现

    由于之前已经有了springboot项目,现在把它改造为spring cloud,并且集成nacos

    官方完整示例demo代码的版本比较低,这里选择最新的版本进行集成

    版本说明

    毕业版本依赖关系(推荐使用)

    由于 Spring Boot 2.4+ 和以下版本之间变化较大,目前企业级客户老项目相关 Spring Boot 版本仍停留在 Spring Boot 2.4 以下,为了同时满足存量用户和新用户不同需求,社区以 Spring Boot 2.4 为分界线,同时维护 2.2.x 和 2021.x 两个分支迭代。

    2021.x 分支

    适配 Spring Boot 2.4, Spring Cloud 2021.x 版本及以上的 Spring Cloud Alibaba 版本如下表(最新版本用*标记): (注意,该分支 Spring Cloud Alibaba 版本命名方式进行了调整, 未来将对应 Spring Cloud 版本, 前三位为 Spring Cloud 版本,最后一位为扩展版本,比如适配 Spring Cloud 2021.0.1 版本对应的 Spring Cloud Alibaba 第一个版本为:2021.0.1.0,第个二版本为:2021.0.1.1,依此类推)

    Spring Cloud Alibaba VersionSpring Cloud VersionSpring Boot Version

    2021.0.1.0*

    Spring Cloud 2021.0.1

    2.6.3

    2021.1

    Spring Cloud 2020.0.1

    2.4.2

    2.2.x 分支

    适配 Spring Boot 为 2.4, Spring Cloud Hoxton 版本及以下的 Spring Cloud Alibaba 版本如下表(最新版本用*标记):

    Spring Cloud Alibaba VersionSpring Cloud VersionSpring Boot Version

    2.2.8.RELEASE*

    Spring Cloud Hoxton.SR12

    2.3.12.RELEASE

    2.2.7.RELEASE

    Spring Cloud Hoxton.SR12

    2.3.12.RELEASE

    这里选取:

    • Spring Cloud Alibaba Version:2.2.8.RELEASE
    • Spring Cloud Version:Spring Cloud Hoxton.SR12
    • Spring Boot Version:2.3.12.RELEASE

    具体的版本依赖可以查看wiki:版本说明 · alibaba/spring-cloud-alibaba Wiki · GitHub

    项目结构

    Nacos 的服务发现功能,如下图示:

    • Nacos Server,nacos服务端,前面已经部署启动了
    • 2个APP,即业务服务,注册到nacos里面
    • 一个service-consumer服务消费方,一个server-provider服务提供方
    • 这里也仿照这个结构,maven项目,一个auth认证服务,一个cipher加解密服务

    添加依赖

    在主pom上面添加

    1. <properties>
    2. <spring-boot.version>2.3.12.RELEASEspring-boot.version>
    3. <spring-cloud.version>Hoxton.SR12spring-cloud.version>
    4. <spring-cloud-alibaba-nacos.version>2.2.7.RELEASEspring-cloud-alibaba-nacos.version>
    5. <java.version>1.8java.version>
    6. properties>
    7. <dependencyManagement>
    8. <dependencies>
    9. <dependency>
    10. <groupId>org.springframework.bootgroupId>
    11. <artifactId>spring-boot-dependenciesartifactId>
    12. <version>${spring-boot.version}version>
    13. <type>pomtype>
    14. <scope>importscope>
    15. dependency>
    16. <dependency>
    17. <groupId>org.springframework.cloudgroupId>
    18. <artifactId>spring-cloud-dependenciesartifactId>
    19. <version>${spring-cloud.version}version>
    20. <type>pomtype>
    21. <scope>importscope>
    22. dependency>
    23. <dependency>
    24. <groupId>com.alibaba.cloudgroupId>
    25. <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    26. <version>${spring-cloud-alibaba-nacos.version}version>
    27. dependency>
    28. <dependency>
    29. <groupId>com.alibaba.nacosgroupId>
    30. <artifactId>nacos-clientartifactId>
    31. <version>2.1.1version>
    32. dependency>
    33. dependencies>
    34. dependencyManagement>

    在auth服务pom里面引入 

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.bootgroupId>
    4. <artifactId>spring-boot-starter-webartifactId>
    5. dependency>
    6. <dependency>
    7. <groupId>com.alibaba.cloudgroupId>
    8. <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    9. <exclusions>
    10. <exclusion>
    11. <groupId>com.alibaba.nacosgroupId>
    12. <artifactId>nacos-clientartifactId>
    13. exclusion>
    14. exclusions>
    15. dependency>
    16. dependencies>

    配置服务注册到 Nacos server

    在 application.properties 中配置 Nacos server 的地址:

    1. server.port=8080
    2. spring.application.name=auth-service
    3. spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

     开启服务注册发现功能 

    通过 Spring Cloud 原生注解 @EnableDiscoveryClient 开启服务注册发现功能:

    1. @SpringBootApplication
    2. @EnableDiscoveryClient
    3. public class AuthApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(AuthApplication.class, args);
    6. }
    7. }

    启动服务,观察效果

    服务注册

    先启动nacos-server服务

    启动auth服务,如果打印类似日志,表示注册成功 

    进入nacos控制台,查看服务列表

    会看到刚才的auth服务已经成功注册到nacos了,同理可以注册启动cipher服务 

    服务调用

    服务消费

    在auth服务上创建一个接口,用作服务消费方,先创建RestTemplate实例

    1. @SpringBootApplication
    2. @EnableDiscoveryClient
    3. public class AuthApplication
    4. {
    5. public static void main(String[] args)
    6. {
    7. SpringApplication.run(AuthApplication.class, args);
    8. }
    9. @LoadBalanced
    10. @Bean
    11. public RestTemplate restTemplate()
    12. {
    13. return new RestTemplate();
    14. }
    15. }

    再创建controller

    1. @RestController
    2. public class TestController
    3. {
    4. @Autowired
    5. private RestTemplate restTemplate;
    6. @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
    7. public String echo(@PathVariable String str)
    8. {
    9. return restTemplate.getForObject("http://cipher-service/echo/" + str, String.class);
    10. }
    11. }

    服务提供

    在cipher服务创建一个接口,用作服务提供方

    1. @RestController
    2. public class EchoController {
    3. @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
    4. public String echo(@PathVariable String string) {
    5. return "Hello Nacos Discovery " + string;
    6. }
    7. }

    服务调用

    启动 AuthApplication和CipherApplication,调用 http://localhost:8080/echo/2022,返回内容为 Hello Nacos Discovery 2022

    示例工程代码

    示例代码地址:tsm

    可能出现的问题

    java.lang.NoClassDefFoundError: com/alibaba/nacos/client/logging/NacosLogging

    注意,如果是从官网的例子升级上来的,nacos-cliet版本也要升级到最新版本,比如1.1.0升到2.1.1,不然就会出现下面的错误

    1. java.lang.NoClassDefFoundError: com/alibaba/nacos/client/logging/NacosLogging
    2. at com.alibaba.cloud.nacos.discovery.logging.NacosLoggingListener.onApplicationEvent(NacosLoggingListener.java:46) ~[spring-cloud-starter-alibaba-nacos-discovery-2.2.7.RELEASE.jar:2.2.7.RELEASE]
    3. at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) [spring-context-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    4. at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) [spring-context-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    5. at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) [spring-context-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    6. at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) [spring-context-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    7. at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:80) [spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    8. at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53) [spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    9. at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:342) [spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    10. at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    11. at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:140) [spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    12. at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:212) [spring-cloud-context-2.2.9.RELEASE.jar:2.2.9.RELEASE]
    13. at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:117) [spring-cloud-context-2.2.9.RELEASE.jar:2.2.9.RELEASE]
    14. at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:74) [spring-cloud-context-2.2.9.RELEASE.jar:2.2.9.RELEASE]
    15. at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) [spring-context-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    16. at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) [spring-context-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    17. at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) [spring-context-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    18. at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) [spring-context-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    19. at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:80) [spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    20. at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53) [spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    21. at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:342) [spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    22. at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    23. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1247) [spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    24. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1236) [spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    25. at com.tw.tsm.auth.AuthApplication.main(AuthApplication.java:11) [classes/:na]
    26. Caused by: java.lang.ClassNotFoundException: com.alibaba.nacos.client.logging.NacosLogging

    com.alibaba.nacos.api.exception.NacosException: Client not connected, current status:STARTING

    用最新版本,2.x以上,除了开放8848nocos-server这个端口以外,还需要开放9848,9849这2个端口,否则启动会报以上错误

    1. com.alibaba.nacos.api.exception.NacosException: Client not connected, current status:STARTING
    2. at com.alibaba.nacos.common.remote.client.RpcClient.request(RpcClient.java:655) ~[nacos-client-2.1.1.jar:na]
    3. at com.alibaba.nacos.common.remote.client.RpcClient.request(RpcClient.java:635) ~[nacos-client-2.1.1.jar:na]
    4. at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.requestToServer(NamingGrpcClientProxy.java:304) ~[nacos-client-2.1.1.jar:na]
    5. at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.doSubscribe(NamingGrpcClientProxy.java:255) ~[nacos-client-2.1.1.jar:na]
    6. at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.subscribe(NamingGrpcClientProxy.java:240) ~[nacos-client-2.1.1.jar:na]
    7. at com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate.subscribe(NamingClientProxyDelegate.java:160) ~[nacos-client-2.1.1.jar:na]
    8. at com.alibaba.nacos.client.naming.NacosNamingService.subscribe(NacosNamingService.java:404) ~[nacos-client-2.1.1.jar:na]
    9. at com.alibaba.cloud.nacos.discovery.NacosWatch.start(NacosWatch.java:134) ~[spring-cloud-starter-alibaba-nacos-discovery-2.2.7.RELEASE.jar:2.2.7.RELEASE]
    10. at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:182) [spring-context-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    11. at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:53) [spring-context-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    12. at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:360) [spring-context-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    13. at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:158) [spring-context-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    14. at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:122) [spring-context-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    15. at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:895) [spring-context-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    16. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554) [spring-context-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    17. at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) [spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    18. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:755) [spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    19. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    20. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:402) [spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    21. at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) [spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    22. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1247) [spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    23. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1236) [spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    24. at com.tw.tsm.auth.AuthApplication.main(AuthApplication.java:11) [classes/:na]

    如果是云服务,要在安全组添加端口开放,并指定访问范围

    如果是物理机,开放端口:

    Centos7 查看防火墙状态,开放特定端口 - 明知山有小脑斧 - 博客园

    参考资料

    官网地址:Nacos Spring Cloud 快速开始

    nacos连接异常 · Issue #I3TQ9B · 若依/RuoYi-Cloud - Gitee.com

  • 相关阅读:
    代码随想录算法训练营Day1 : 704.二分查找、27.移除元素
    总结使用React做过的一些优化
    Mysql Liunx下安装指南
    Transformer:开源机器学习项目,上千种预训练模型 | 开源日报 No.66
    Altium Designer20 交叉选择模式
    猿创征文 | 组件的定义及复用性,局部组件和全局组件(2)
    供电企业的福音,远程抄表系统无线解决方案,让工程师告别奔波
    解决报错 java.lang.IllegalArgumentException: Cannot format given Object as a Date
    工业4.0数字孪生下的应用案例
    如何设计一个“好的“测试用例?
  • 原文地址:https://blog.csdn.net/weigeshikebi/article/details/126818292