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支持以下三种主要配置方式:
- XML 配置:传统的配置方式,通过Spring的配置文件来描述Dubbo的各类配置。
- 注解配置:通过注解直接在代码中进行配置,简化了XML配置。
- 基于Spring Boot的配置:结合Spring Boot的自动配置特性,进一步简化了配置过程。
每种配置方式都有其独特的优势和适用场景,可以根据项目需求选择合适的配置方式。