SpringBoot整合SpringCloud+nacos+feign

500 阅读1分钟

 背景        

        最近因为项目需求,要将一个SpringBoot项目A整合进一个分布式项目B中,项目B的技术栈为SpringCloud Alibaba+nacos+openfeign。

操作步骤      

        首先,确定项目B中Cloud的依赖版本,我这里的cloud版本是 2020.0.4,alibaba cloud的版本是2021.1

 ​编辑

​编辑

1.将上面的cloud依赖加到项目A当中。

 代码如下:

   <!--https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies/Hoxton.SR8-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-alibaba-dependencies/2.2.1.RELEASE-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${alibaba.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

坑!

如果原先springboot的依赖版本和cloud版本不兼容在启动时会报这个异常:

2021-12-22 15:26:16.795 ERROR [ system,,] 84046 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

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

- Spring Boot [2.6.1] 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.4.x, 2.5.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]



Process finished with exit code 1

我这里将springboot版本改为了 2.5.5

​编辑

2.整合nacos

        <!--添加nacos客户端-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--坑:spring-cloud-dependencies 2020.0.0 默认不在加载bootstrap配置文件-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>

注意: 要读取bootstarp配置项,必须加此依赖

<!--坑:spring-cloud-dependencies 2020.0.0 默认不在加载bootstrap配置文件-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>

3.整合feign

        <!--Feign远程调用-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

4.至此,所需依赖整合完毕。我们可以刷新maven,尝试编译项目啦

最后,如果对你有帮助的话请收藏+点赞!