若依前后端分离版集成nacos

        根据公司要求,需要将项目集成到nacos中,当前项目是基于若依前后端分离版开发的,若依的版本为3.8.3,若依框架中整合的springBoot版本为2.5.14。Nacos核心提供两个功能:服务注册与发现,动态配置管理。


一、服务注册与发现

1、引入pom依赖

     </dependency>-->
        <!-- 在SpringBoot 2.4.x的版本之后,对配置文件加载方式进行了重构,需要导入如下的依赖;
        详情见官网:https://docs.spring.io/spring-cloud-config/docs/current/reference/html/#config-first-bootstrap -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.0.1</version>
        </dependency>

        <!-- 服务注册与发现依赖 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2021.1</version>
        </dependency>

2、bootstrap.yml配置

spring:
  # nacos配置
  cloud:
    nacos:
      discovery:
        server-addr: 10.2.XX.XX:8848
    compatibility-verifier:
      enabled: false

3、添加注解

在项目启动类上添加 @EnableDiscoveryClient 注解:

@EnableDiscoveryClient
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class RiskExamineApplication
{
    public static void main(String[] args)
    {
        // System.setProperty("spring.devtools.restart.enabled", "false");
        SpringApplication.run(RiskExamineApplication.class, args);
        System.out.println("(♥◠‿◠)ノ゙  若依启动成功   ლ(´ڡ`ლ)゙  \n" +
                " .-------.       ____     __        \n" +
                " |  _ _   \\      \\   \\   /  /    \n" +
                " | ( ' )  |       \\  _. /  '       \n" +
                " |(_ o _) /        _( )_ .'         \n" +
                " | (_,_).' __  ___(_ o _)'          \n" +
                " |  |\\ \\  |  ||   |(_,_)'         \n" +
                " |  | \\ `'   /|   `-'  /           \n" +
                " |  |  \\    /  \\      /           \n" +
                " ''-'   `'-'    `-..-'              ");
    }
}

4、验证

        项目启动后,登录nacos,在服务列表中,点击查询即可看到注册到nacos的服务,如下图:

服务名默认为yml配置里spring.application.name的名称,也可通过spring.cloud.nacos.discovery.service配置指定。

遇到问题:Your project setup is incompatible with our requirements due to following reasons:
- Spring Boot [2.5.14] is not compatible with this Spring Cloud release train

Change Spring Boot version to one of the following versions [2.3.x, 2.4.x] 


***************************
APPLICATION FAILED TO START
***************************

Description:

Your project setup is incompatible with our requirements due to following reasons:

- Spring Boot [2.5.14] is not compatible with this Spring Cloud release train


Action:

Consider applying the following actions:

- Change Spring Boot version to one of the following versions [2.3.x, 2.4.x] .
You can find the latest Spring Boot versions here [https://spring.io/projects/spring-boot#learn]. 
If you want to learn more about the Spring Cloud Release train compatibility, you can visit this page [https://spring.io/projects/spring-cloud#overview] and check the [Release Trains] section.
If you want to disable this check, just set the property [spring.cloud.compatibility-verifier.enabled=false]

解决办法:出现这个问题是由于springBoot和springCloud版本不一致导致的,如果是自己手动搭建的框架,可以根据提示将springBoot版本降到[2.3.x, 2.4.x];但是由于我用的是若依的框架,担心将springBoot版本降低后会引起其它问题,所以根据提示设置spring.cloud.compatibility-verifier.enabled=false这个属性,但是引入后发现配置不生效,最后根据研究一番之后,根据网上的提示需将配置文件名称application.yml改为bootstrap.yml,然后再引入依赖spring-cloud-starter-bootstrap 即可解决。

注意:如果只需要nacos的服务注册与发现功能,不用服务配置功能的话,一定要将服务配置的pom依赖(spring-cloud-starter-alibaba-nacos-config)去掉,否则启动时会报错:currentServerAddr:http://localhost:8848, err : connect timed out 。

二、服务动态配置与服务注册发现

1、引入pom依赖

  <!-- nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2021.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2021.1</version>
        </dependency>

2、bootstrap.yml配置

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 124.70.XX.XX:32389
        namespace: zs-smart
        group: dev
      config:
        server-addr: 124.70.XX.XX:32389
        namespace: zs-smart
        group: dev
        name: zs-smart
        file-extension: yaml
    compatibility-verifier:
      enabled: false

3、nacos配置

1)新建命名空间

 2) 新建配置

打开配置列表,选择刚刚创建的zs-smart,点击右上角的“+”号,如下图:

3)填写配置

然后启动项目,如果项目启动成功,即说明配置没问题! 

参考文章:Springboot集成Nacos2「服务注册与发现」 - 知乎

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
若依框架支持前后端分离的二次开发。根据引用中的描述,可以将自己系统的业务处理放在新建模块中,同时保持若依源码不变。具体做法是将controller层放在wms-admin模块进行管理,将domain、mapper、service和xml文件放在新建模块中进行管理。这样可以方便地进行业务扩展和定制化开发。 在主模块的pom文件中,可以引用新建模块的依赖,以实现主模块对新建模块的功能扩展。引用中的代码示例展示了如何在pom文件中引用新建模块。通过添加dependency标签,并指定新建模块的groupId、artifactId和version,就可以将新建模块的功能引入到主模块中。 在进行若依框架的二次开发之前,需要满足一些前置条件。引用中列举了一些主要的要求,包括JDK本、Mysql本、Redis本、Maven本、Node本和nacos本。确保满足这些要求可以保证二次开发的顺利进行。 总结起来,若依框架支持前后端分离的二次开发。可以通过在新建模块中管理自己系统的业务处理,同时保持若依源码不变。可以通过在主模块的pom文件中引用新建模块的依赖来扩展功能。在开始二次开发之前,需要满足一些前置条件,如特定的JDK、数据库、Redis、Maven、Node和nacos本要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [若依框架前后端分离使用2](https://blog.csdn.net/Hello_World_Me/article/details/125198350)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [若依框架前后端分离使用1](https://blog.csdn.net/Hello_World_Me/article/details/125142393)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码道功成

过程不易,恳请支持一下!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值