Dubbo支持多种配置方式,包括XML配置、注解配置以及基于Spring Boot的配置方式。每种配置方式都有其独特的优势和使用场景。下面详细介绍这三种配置方式,并结合具体代码示例进行说明。
XML配置是最传统的配置方式,通过Spring的配置文件来描述Dubbo的各类配置。
pom.xml
<dependencies>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.8version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>4.2.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
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 name="dubbo-demo-provider"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:protocol name="dubbo" port="20880"/>
<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 name="dubbo-demo-consumer"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<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);
}
}
注解配置方式简化了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);
};
}
}
基于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支持以下三种主要配置方式:
每种配置方式都有其独特的优势和适用场景,可以根据项目需求选择合适的配置方式。