SpringBoot 整合Dubbo

文章目录

注:此项目使用了Zookeeper组件。

一、工程目录结构

在这里插入图片描述

二、创建工程项目

1、创建接口工程(cw-dubbo-api)

此工程就是一个maven工程。

在这里插入图片描述

(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>SpringBootDubboDemo</artifactId>
        <groupId>cw-dubbo-demo</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cw-dubbo-api</artifactId>

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

</project>

(2)创建接口类(LoginService)

package com.cw.dubbo.api;

public interface LoginService {

    String login(String name, String pwd);
}

2、创建服务提供者工程(cw-dubbo-provider)

该工程是一个SpringBoot工程。
在这里插入图片描述

(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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cw-dubbo-demo</groupId>
    <artifactId>cw-dubbo-provider</artifactId>
    <version>1.0.0</version>
    <name>cw-dubbo-provider</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>cw-dubbo-demo</groupId>
            <artifactId>cw-dubbo-api</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <!--引入springboot整合dubbo的jar包-->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

(2)application.properties

server.port=8078

# dubbo项目名
dubbo.application.name=cw-dubbo-providor

# 注册中心地址
dubbo.registry.address=127.0.0.1:2181
# 指定注册中心为zookeeper
dubbo.registry.protocol=zookeeper

# 指定通信协议
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
# 
dubbo.consumer.timeout=300000
dubbo.provider.timeout=300000

# 连接监控中心,去注册中心直接发现,不配地址
dubbo.monitor.protocol=registry

(3)主类(CwDubboProviderApplication)

package com.cw.dubbo.provider;

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

@EnableDubbo
@SpringBootApplication
public class CwDubboProviderApplication {

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

}

(4)LoginServiceImpl

package com.cw.dubbo.provider;

import com.alibaba.dubbo.config.annotation.Service;
import com.cw.dubbo.api.LoginService;

@Service
public class LoginServiceImpl implements LoginService {

    @Override
    public String login(String name, String pwd) {
        return "name = " + name + ", pwd = " + pwd;
    }
}

3、创建服务调用者工程(cw-dubbo-consumer)

该工程是一个SpringBoot工程。

在这里插入图片描述

(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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cw-dubbo-demo</groupId>
    <artifactId>cw-dubbo-consumer</artifactId>
    <version>1.0.0</version>
    <name>cw-dubbo-consumer</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>cw-dubbo-demo</groupId>
            <artifactId>cw-dubbo-api</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <!--引入springboot整合dubbo的jar包-->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

(2)application.properties

server.port=8079

#应用名称
dubbo.application.name=cw-dubbo-consumer

#注册中心地址(可以直接全地址)
dubbo.registry.address=zookeeper://127.0.0.1:2181

dubbo.consumer.timeout=300000
dubbo.provider.timeout=300000

#在注册中心找监控
dubbo.monitor.protocol=registry

(3)主类(CwDubboConsumerApplication)

package com.cw.dubbo.consumer;

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

@EnableDubbo
@SpringBootApplication
public class CwDubboConsumerApplication {

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

}

(4)LoginController

package com.cw.dubbo.consumer;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cw.dubbo.api.LoginService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

    @Reference
    private LoginService loginService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(@RequestParam(name = "name") String name,
                        @RequestParam(name = "pwd") String pwd){
        long start = System.currentTimeMillis();
        String str = loginService.login(name, pwd);
        long end = System.currentTimeMillis();
        return "duration = " + (end - start) + "ms, " + str;
    }
}

三、测试

先启动 cw-dubbo-provider 工程,然后在启动 cw-dubbo-consumer 工程。

启动成功后,在浏览器访问:http://localhost:8079/login?name=admin&pwd=123456

出现如下界面说明远程Dubbo调用成功。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值