Dubbo(26)Dubbo的配置方式有哪些?

Dubbo支持多种配置方式,包括XML配置、注解配置以及基于Spring Boot的配置方式。每种配置方式都有其独特的优势和使用场景。下面详细介绍这三种配置方式,并结合具体代码示例进行说明。

1. XML 配置

XML配置是最传统的配置方式,通过Spring的配置文件来描述Dubbo的各类配置。

示例代码

pom.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>
</dependencies>

dubbo-provider.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- Dubbo application config -->
    <dubbo:application name="dubbo-demo-provider"/>

    <!-- Registry config -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <!-- Protocol config -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- Service config -->
    <dubbo:service interface="com.example.DemoService" ref="demoServiceImpl"/>
    
    <bean id="demoServiceImpl" class="com.example.DemoServiceImpl"/>
</beans>

dubbo-consumer.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- Dubbo application config -->
    <dubbo:application name="dubbo-demo-consumer"/>

    <!-- Registry config -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <!-- Reference config -->
    <dubbo:reference id="demoService" interface="com.example.DemoService"/>
</beans>

服务接口和实现

package com.example;

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

package com.example;

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

消费者

package com.example;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Consumer {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-consumer.xml");
        context.start();
        DemoService demoService = (DemoService) context.getBean("demoService");
        String hello = demoService.sayHello("World");
        System.out.println(hello);
    }
}

2. 注解配置

注解配置方式简化了XML配置,通过注解直接在代码中进行配置。

示例代码

pom.xml

与XML配置相同。

application.yml

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

服务接口和实现

package com.example;

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

package com.example;

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

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

消费者

package com.example;

import org.apache.dubbo.config.annotation.DubboReference;
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);
    }

    @DubboReference
    private DemoService demoService;

    @Bean
    public CommandLineRunner demo() {
        return args -> {
            String result = demoService.sayHello("World");
            System.out.println(result);
        };
    }
}

3. 基于Spring Boot的配置

基于Spring Boot的配置方式是目前最为流行的配置方式,结合了Spring Boot的自动配置特性,简化了配置过程。

示例代码

pom.xml

与XML配置相同。

application.yml

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

服务接口和实现

package com.example;

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

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

消费者

package com.example;

import org.apache.dubbo.config.annotation.DubboReference;
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);
    }

    @DubboReference
    private DemoService demoService;

    @Bean
    public CommandLineRunner demo() {
        return args -> {
            String result = demoService.sayHello("World");
            System.out.println(result);
        };
    }
}

总结

通过上述示例,我们可以看到Dubbo支持以下三种主要配置方式:

  1. XML 配置:传统的配置方式,通过Spring的配置文件来描述Dubbo的各类配置。
  2. 注解配置:通过注解直接在代码中进行配置,简化了XML配置。
  3. 基于Spring Boot的配置:结合Spring Boot的自动配置特性,进一步简化了配置过程。

每种配置方式都有其独特的优势和适用场景,可以根据项目需求选择合适的配置方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

辞暮尔尔-烟火年年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值