Dubbo(25)如何配置Dubbo的协议和端口?

配置Dubbo的协议和端口是设置分布式服务通信的基础步骤。Dubbo支持多种协议(如Dubbo、RMI、HTTP等),你可以根据需求选择合适的协议并配置相应的端口。下面以一个完整的Spring Boot项目为例,详细介绍如何配置Dubbo的协议和端口。

配置步骤

  1. 引入依赖:在项目中引入Dubbo和注册中心(如ZooKeeper)的相关依赖。
  2. 配置注册中心和Dubbo协议:在Dubbo的配置文件中配置注册中心和协议的相关属性。
  3. 定义服务接口和实现 :定义一个服务接口和其实现,并通过@DubboService注解将其暴露为Dubbo服务。
  4. 启动服务提供者和消费者:编写启动类,启动Spring Boot应用。

详细代码示例

1. 引入依赖

在Maven项目中,需要在pom.xml文件中引入相关依赖。

xml 复制代码
<dependencies>
    <!-- Dubbo dependencies -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.7.8</version>
    </dependency>

    <!-- ZooKeeper dependencies -->
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-recipes</artifactId>
        <version>4.2.0</version>
    </dependency>

    <!-- Spring Boot 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>
</dependencies>

2. 配置注册中心和Dubbo协议

在Spring Boot项目中,可以通过application.yml文件来配置Dubbo的注册中心和协议的相关属性。

服务提供者的配置文件(application.yml):

yaml 复制代码
server:
  port: 8081

dubbo:
  application:
    name: dubbo-demo-provider
  registry:
    address: zookeeper://127.0.0.1:2181
  protocol:
    name: dubbo
    port: 20880
  scan:
    base-packages: com.example

服务消费者的配置文件(application.yml):

yaml 复制代码
server:
  port: 8080

dubbo:
  application:
    name: dubbo-demo-consumer
  registry:
    address: zookeeper://127.0.0.1:2181
  protocol:
    name: dubbo
  scan:
    base-packages: com.example

3. 定义服务接口和实现

定义一个服务接口和其实现,并通过@DubboService注解将其暴露为Dubbo服务。

服务接口:

java 复制代码
package com.example;

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

服务实现:

java 复制代码
package com.example;

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

@DubboService
public class DemoServiceImpl implements DemoService {
    @Override
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

服务消费者逻辑:

java 复制代码
package com.example;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

@Component
public class DemoServiceConsumer {

    @DubboReference
    private DemoService demoService;

    public void execute() {
        String result = demoService.sayHello("World");
        System.out.println(result);
    }
}

4. 编写启动类

编写启动类,启动Spring Boot应用。

服务提供者启动类:

java 复制代码
package com.example;

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);
    }
}

服务消费者启动类:

java 复制代码
package com.example;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

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

    @Bean
    public CommandLineRunner demo(DemoServiceConsumer consumer) {
        return args -> consumer.execute();
    }
}

运行示例

  1. 启动ZooKeeper :确保ZooKeeper注册中心在本地或远程服务器上运行,并且地址为127.0.0.1:2181
  2. 启动服务提供者 :运行DubboProviderApplication类,启动Spring Boot应用,确保服务成功注册到ZooKeeper。
  3. 启动服务消费者 :运行DubboConsumerApplication类,启动Spring Boot应用。

在消费者的控制台中,你会看到服务调用的结果:

复制代码
Hello, World

配置其他协议(如HTTP)

如果你想使用其他协议,如HTTP,可以在application.yml文件中进行相应的配置。

服务提供者的配置文件(application.yml):

yaml 复制代码
server:
  port: 8081

dubbo:
  application:
    name: dubbo-demo-provider
  registry:
    address: zookeeper://127.0.0.1:2181
  protocol:
    name: http
    port: 8082
  scan:
    base-packages: com.example

服务消费者的配置文件(application.yml):

yaml 复制代码
server:
  port: 8080

dubbo:
  application:
    name: dubbo-demo-consumer
  registry:
    address: zookeeper://127.0.0.1:2181
  protocol:
    name: http
  scan:
    base-packages: com.example

总结

通过上述步骤,我们可以看到如何配置Dubbo的协议和端口:

  1. 引入依赖:在项目中引入Dubbo和注册中心(如ZooKeeper)的相关依赖。
  2. 配置注册中心和Dubbo协议 :在application.yml文件中配置注册中心的地址和协议的相关属性。
  3. 定义服务接口和实现 :通过@DubboService注解将服务实现类暴露为Dubbo服务。
  4. 启动服务提供者和消费者:编写启动类,启动Spring Boot应用。

通过这些配置,服务提供者和消费者能够使用指定的协议和端口进行通信,实现分布式服务架构。

相关推荐
65岁退休Coder1 小时前
LangGraph v1.2.9 节点容错策略 & 流式输出 & 持久化记忆管理
后端·python·langchain
元界metalite3 小时前
SpringBoot整合RocketMQ-毒丸消息还要无限重试吗
后端
SimonKing3 小时前
升级Spring Boot 4后,从 Jackson 2 到 3,到底有哪些变化
java·后端·程序员
YIAN3 小时前
Docker + Nginx 核心原理扫盲:从环境隔离到反向代理,运维面试必考点
后端·docker·面试
万物智能3 小时前
启动链路与分区—【万物智能之开源鸿蒙OpenHarmony系统实战开发系列教程】
后端·架构
寒蝉1283 小时前
一个简单操作是怎么在分布式环境下变复杂的
后端
苏三的开发日记3 小时前
Windows宿主机+VMware CentOS虚拟机 + 同一个Wi-Fi下的其他实体电脑,三者可以互相访问
后端
boooooooom3 小时前
手把手做一个图 RAG 烹饪问答系统:Neo4j + Milvus + LLM 的工程实践
前端·javascript·后端
newerp4 小时前
Golang 切片底层结构
后端·程序员·go
BingoGo4 小时前
免费可商用 PHP 管理后台 CatchAdmin V5.4.0 发布,新增短信服务能力
后端·php