springboot整合dubbo nacos

avatar 2021年09月26日11:54:10 6 3879 views
博主分享免费Java教学视频,B站账号:Java刘哥 ,长期提供技术问题解决、项目定制:本站商品点此

因为最近要在组内做一个dubbo技术分享,为了方便演示和测试,这里写一个demo。

几年前写了一个 dubbo zookeeper的,因为公司项目目前用nacos比较多,这里也写一个 springboot 整合 dubbo 和 nacos 的 demo 。

代码地址:https://github.com/saysky/springboot-dubbo-nacos-demo

代码结构:

注册中心nacos用的1.1.4版本

一、服务提供者

0、pom.xml

<?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>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>dubbo-demo-provider</artifactId>
    <groupId>com.liuyanzhao</groupId>
    <version>1.0-SNAPSHOT</version>

    <packaging>pom</packaging>
    <modules>
        <module>dubbo-demo-provider-api</module>
        <module>dubbo-demo-provider-impl</module>
    </modules>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <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>
            <version>2.2.2.RELEASE</version>
        </dependency>

        <!-- dubbo依赖 -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
            <version>2.7.8</version>
        </dependency>
    </dependencies>
</project>

 

1、API层

1)pom.xml

<?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>dubbo-demo-provider</artifactId>
        <groupId>com.liuyanzhao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <packaging>jar</packaging>

    <artifactId>dubbo-demo-provider-api</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

 

2)service接口或实体类

package com.liuyanzhao.provider.api.service;

public interface HelloService
{
    String sayHello(String name);
}

 

2、服务提供者实现

1)pom.xml

引入dubbo相关依赖,api的依赖

<?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>dubbo-demo-provider</artifactId>
        <groupId>com.liuyanzhao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-demo-provider-impl</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.liuyanzhao</groupId>
            <artifactId>dubbo-demo-provider-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>


    </dependencies>
</project>

 

2)application.properties

server.port=8080
spring.application.name=dubbo-demo-provider

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=nacos://127.0.0.1:8848
dubbo.application.name=dubbo-demo-provider
dubbo.consumer.check=false

 

3)启动类

package com.liuyanzhao;


import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.liuyanzhao.provider")
@EnableDubbo(scanBasePackages = "com.liuyanzhao.provider.service.impl")
public class ProviderApplication extends SpringBootServletInitializer
{

    public static void main(String[] args)
    {
        SpringApplication.run(ProviderApplication.class, args);
    }

}

 

4)service实现

package com.liuyanzhao.provider.service.impl;

import com.liuyanzhao.provider.api.service.HelloService;
import org.apache.dubbo.config.annotation.DubboService;

/**
 * 描述
 *
 * @author: liuyanzhao
 * @date: 2021/9/26 9:42
 */
@DubboService(group = "dubbo-demo-provider", version = "1.0.0", timeout = 30000)
public class HelloServiceImpl implements HelloService
{

    @Override
    public String sayHello(String name)
    {
        return "Hello:" + name;
    }
}

 

二、服务消费者

1、pom.xml

<?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>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>dubbo-demo-consumer</artifactId>
    <groupId>com.liuyanzhao</groupId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>


    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.liuyanzhao</groupId>
            <artifactId>dubbo-demo-provider-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <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>
            <version>2.2.2.RELEASE</version>
        </dependency>

        <!-- dubbo依赖 -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
            <version>2.7.8</version>
        </dependency>
    </dependencies>
</project>

 

2、application.properties

server.port=9090
spring.application.name=dubbo-demo-consumer

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=nacos://127.0.0.1:8848
dubbo.application.name=dubbo-demo-consumer
dubbo.consumer.check=false

 

3、启动类

package com.liuyanzhao;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.liuyanzhao.consumer")
public class ConsumerApplication extends SpringBootServletInitializer
{

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

}

 

4、调用服务提供者示例类

package com.liuyanzhao.consumer.controller;

import com.liuyanzhao.provider.api.service.HelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 描述
 *
 * @author: liuyanzhao
 * @date: 2021/9/26 10:22
 */
@RestController
public class MainController
{

    @DubboReference(group = "dubbo-demo-provider", version = "1.0.0", check = false)
    private HelloService helloService;

    @GetMapping("/")
    public String index()
    {
        return helloService.sayHello("consumer");
    }
}

 

 

历史上的今天
九月
26日
  • 微信
  • 交流学习,资料分享
  • weinxin
  • 个人淘宝
  • 店铺名:言曌博客咨询部

  • (部分商品未及时上架淘宝)
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0