SpringBoot之整合Dubbo

一、下载Dubbo-Admin并导入本地

dubbo-admin地址为:
GitHub - apache/dubbo-admin: The ops and reference implementation for Apache Dubbo

官方文档:
https://github.com/apache/dubbo-admin/blob/develop/README_ZH.md

注意需要安装Zookeeper(因为Dubbo使用Zookeeper作为注册中心)

二、建立服务提供者(这里是我博客的一个子项目名为blog-dubbo-provider)

1.导入Maven依赖

XML 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- dubbo的依赖 -->
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.6</version>
</dependency>


<!-- zk的依赖 -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-dependencies-zookeeper</artifactId>
    <version>2.7.6</version>
    <type>pom</type>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>

2.编写启动类和配置文件

启动类:

java 复制代码
package com.dubbo.blog;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DubboProviderApplication {

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

配置文件(application.properties):

java 复制代码
# Spring boot application
spring.application.name=blog-dubbo-provider
# Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
dubbo.scan.base-packages=com.dubbo.blog.provider.service.impl   
# Dubbo Application
## The default value of dubbo.application.name is ${spring.application.name}
dubbo.application.name=${spring.application.name}
# Dubbo Protocol
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
## Dubbo Registry
dubbo.registry.address=zookeeper://127.0.0.1:2181
server.port=7010

3.编写接口和对应的实现类

TestService:

java 复制代码
package com.dubbo.blog.provider.service;

public interface TestService {

    String showName();
}

TestServiceImpl:

java 复制代码
package com.dubbo.blog.provider.service.impl;

import com.dubbo.blog.provider.service.TestService;
import org.apache.dubbo.config.annotation.Service;

@Service(version = "1.0.0")
public class TestServiceImpl implements TestService {
    @Override
    public String showName() {
        return "HELLO   DUBBO";
    }
}

TestService2:

java 复制代码
package com.dubbo.blog.provider.service;

public interface TestService2 {
    String showName();
}

TestService2Impl:

java 复制代码
package com.dubbo.blog.provider.service.impl;

import com.dubbo.blog.provider.service.TestService2;
import org.apache.dubbo.config.annotation.Service;

@Service(version = "1.0.0")
public class TestService2Impl implements TestService2 {
    @Override
    public String showName() {
        return "HELLO   TestService2";
    }
}

4.启动主类并打开Dubbo-Admin

下图表示Ok

三、建立服务消费者(这里是我博客的一个子项目名为blog-dubbo-consumer)

1.导入Maven依赖

XML 复制代码
<dependency>
    <groupId>com.challenger.blog</groupId>
    <artifactId>blog-dubbo-provider</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>

<!-- dubbo的依赖 -->
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.6</version>
</dependency>


<!-- zk的依赖 -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-dependencies-zookeeper</artifactId>
    <version>2.7.6</version>
    <type>pom</type>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>

2.编写启动类和配置文件

启动类:

java 复制代码
package com.dubbo.blog;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DubboConsumerApplication {

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

}

配置文件(application.properties):

复制代码
spring.application.name=blog-dubbo-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
server.port=7000

3.编写Controller

java 复制代码
package com.dubbo.blog;


import com.dubbo.blog.provider.service.TestService;
import com.dubbo.blog.provider.service.TestService2;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestDubboController {
    @Reference(version = "1.0.0")
    private TestService2 service2;
    @Reference(version = "1.0.0")
    private TestService service;

    @GetMapping("test1")
    public String test1() {
        return service.showName();
    }

    @GetMapping("test2")
    public String test2() {
        return service2.showName();
    }
}

4.切换到dubbo-admin看服务关系

5.可以请求Controller对应的url测试一下

相关推荐
齐 飞8 分钟前
MongoDB笔记01-概念与安装
前端·数据库·笔记·后端·mongodb
云空8 分钟前
《Python 与 SQLite:强大的数据库组合》
数据库·python·sqlite
暮毅13 分钟前
10.Node.js连接MongoDb
数据库·mongodb·node.js
狂放不羁霸15 分钟前
idea | 搭建 SpringBoot 项目之配置 Maven
spring boot·maven·intellij-idea
wowocpp16 分钟前
ubuntu 22.04 server 格式化 磁盘 为 ext4 并 自动挂载 LTS
服务器·数据库·ubuntu
九圣残炎16 分钟前
【从零开始的LeetCode-算法】1456. 定长子串中元音的最大数目
java·算法·leetcode
wclass-zhengge19 分钟前
Netty篇(入门编程)
java·linux·服务器
LunarCod25 分钟前
WorkFlow源码剖析——Communicator之TCPServer(中)
后端·workflow·c/c++·网络框架·源码剖析·高性能高并发
成富38 分钟前
文本转SQL(Text-to-SQL),场景介绍与 Spring AI 实现
数据库·人工智能·sql·spring·oracle
songqq2739 分钟前
SQL题:使用hive查询各类型专利top 10申请人,以及对应的专利申请数
数据库·sql