springcloud alibaba 整合nacos&dubbo

1.创建父模块

创建maven模块,不用勾选任何东西

依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.fenhao</groupId>
    <artifactId>demo-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <!--模块-->
    <modules>
        <module>demo-provider</module>
    </modules>

    <!--版本管理-->
    <properties>
        <spring.boot.version>2.3.12.RELEASE</spring.boot.version>
        <spring.cloud.version>Hoxton.SR12</spring.cloud.version>
        <spring.cloud.alibaba.version>2.2.7.RELEASE</spring.cloud.alibaba.version>
        <dubbo.version>2.7.13</dubbo.version>
        <nacos.version>2.0.3</nacos.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${spring.cloud.alibaba.version}</version>
        </dependency>
    </dependencies>
    <!--依赖管理-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring.cloud.alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

 

注意地方

1.子pom继承父pom问题

父dependencyManagement里面包含的依赖子项目需要各自引用才加载,不需要写版本号
父dependencies里面包含的依赖子项目直接继承

2.packaging必须是pom

3.版本问题

地址:https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E

 

 

 

2.创建子模块

创建module模块,不用勾选任何东西

依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>demo-parent</artifactId>
        <groupId>com.fenhao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo-provider</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.spring</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>1.0.11</version>
        </dependency>
    </dependencies>
</project>

 

代码片段

server.port=8081
spring.application.name=nacos-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.discovery.enabled=ture
management.endpoints.web.exposure.include=*

#dubbo
dubbo.protocol.port=20881
dubbo.protocol.name=dubbo
dubbo.scan.base-packages=
dubbo.application.name = dubbo-application
dubbo.registry.address = nacos://127.0.0.1:8848
package com.fenhao.service;

import org.apache.dubbo.config.annotation.DubboService;

@DubboService
public class SayHelloService implements HelloService{

    @Override
    public String hello(String name) {
        System.out.println("say hello");
        return null;
    }
}
package com.fenhao;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }

}

 

注意地方

1.注解问题

启动开始加上@EnableDubbo。dubbo2.7.8以后的版本为了区别原生态@service注解把dubbo注解修改为@DubboService,如果要注册到nacos的话,不需要加上原生态springcloud注解@EnableDiscoveryClient

2.依赖报错问题

<dependency>
            <groupId>com.alibaba.spring</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>1.0.11</version>
        </dependency>

默认使用的1.0.10报错
Action:

Correct the classpath of your application so that it contains a single, compatible version of org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor

github#issue:https://github.com/alibaba/spring-cloud-alibaba/issues/2310

3.总结

springcloud&springcloud alibaba&springboot区分

springcloud是微服务解决方案里面包含eureka、feign、hystrix等等

springcloud是基于springboot

springcloud alibaba是阿里的微服务解决方案里面包含nacos、dubbo等等,但是nacos、dubbo等也可以和springcloud、springboot集成

springcloud alibaba是springcloud分支

posted @ 2023-02-01 17:22  缘故为何  阅读(214)  评论(0编辑  收藏  举报