快速搭建SpringCloudAlibaba + Nacos + Dubbo工程

·  阅读 1245

1.创建父工程SpringCloudAlibabaDubbo

引入SpringCloud Alibaba依赖

<!--  引入springcloud依赖  -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2.创建子模块springcloud-alibaba-dubbo-api

引入dubbo相关依赖

	<dependencies>
        <!--spring cloud alibaba dubbo 依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-apache-dubbo-adapter</artifactId>
        </dependency>
    </dependencies>

创建服务接口

/**
 * @project SpringCloudAlibabaDubbo
 * @remark
 * @Author Administrator
 * @date 2021/5/31
 */
public interface HelloService {
    String sayHello();
}

3.创建服务提供方模块

添加maven依赖

    <dependencies>
        <!--web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<!--        Nacos服务发现依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.pengheng</groupId>
            <artifactId>springcloud-alibaba-dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

实现dubbo-api接口

@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello() {
        return "Hello SpringCloud Alibaba Dubbo";
    }
}

创建提供方配置文件

spring:
  application:
    name: springcloud-alibaba-dubbo-provider
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850
  main:
    allow-bean-definition-overriding: true

server:
  port: 9090

dubbo:
  scan:
    # dubbo 服务扫描包
    base-packages: com.pengheng.service.impl
  protocol:
    # 设置协议为dubbo
    name: dubbo
    # dubbo 协议端口 (-1 表示自增端口,从20880开始)
    port: -1
  registry:
    # 以下是配置中已包含nacos配置的写法
    address: spring-cloud://localhost
  cloud:
    subscribed-services: springcloud-alibaba-dubbo-provider,springcloud-alibaba-dubbo-consumer

提供方主启动类

@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class);
    }
}

4.创建服务消费方模块

添加maven依赖

    <dependencies>
        <!--web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<!--        Nacos服务发现依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.pengheng</groupId>
            <artifactId>springcloud-alibaba-dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

消费方Controller调用层

@RestController
@RequestMapping(("/demo"))
public class HelloController {

    @Reference
    private HelloService helloService;

    @RequestMapping("/hello")
    public String sayHello() {
        return helloService.sayHello();
    }
}

创建消费方配置文件

server:
  port: 8080
spring:
  main:
    allow-bean-definition-overriding: true
  application:
    name: springcloud-alibaba-dubbo-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850
dubbo:
  registry:
    address: spring-cloud://localhost
  cloud:
    subscribed-services: springcloud-alibaba-dubbo-provider,springcloud-alibaba-dubbo-consumer

消费方主启动类

@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class);
    }
}

Demo下载地址

分类:
后端
标签:
分类:
后端
标签:
收藏成功!
已添加到「」, 点击更改