springboot 整合 dubbo 首先得引入 dubbo 依赖
在 pom 中加如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
</dependency>
1、配置使用 dubbo 配置文件方法:
向外提供接口:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/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
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="spring-boot-quick"></dubbo:application>
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
<dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>
<dubbo:service interface="cn.edu.pzhu.zc.service.PersonService" ref="personServiceImpl"></dubbo:service>
<!--<bean class="cn.edu.pzhu.zc.service.impl.PersonServiceImpl" id="personServiceImpl"></bean>-->
<dubbo:monitor protocol="registry"></dubbo:monitor>
</beans>
然后在启动时,加载 dubbo 配置文件即可:
@ImportResource(locations = "classpath:dubbo.xml")
@SpringBootApplication
public class SpringBootQuickApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootQuickApplication.class, args);
}
}
这样就能将 dubbo 接口提供出去。
2、使用注解 API 方式整合 dubbo:
对于配置文件中所有标签都能创建相应的组件,放到 IOC 容器中。
创建一个 dubbo 配置类,加载 dubbo 组件。
@Configuration
public class MyDubboConfig {
//<dubbo:application name="spring-boot-quick"></dubbo:application>
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("My-Dubbo-Test");
return applicationConfig;
}
//<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setProtocol("zookeeper");
registryConfig.setAddress("127.0.0.1:2181");
return registryConfig;
}
//<dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>
@Bean
public ProtocolConfig protocolConfig() {
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName("dubbo");
protocolConfig.setPort(2882);
return protocolConfig;
}
//配置接口
@Bean
public ServiceConfig<PersonService> serviceConfig(PersonService personService) {
ServiceConfig<PersonService> serviceConfig = new ServiceConfig<>();
serviceConfig.setInterface(PersonService.class);
serviceConfig.setRef(personService);
return serviceConfig;
}
//<dubbo:provider timeout="1000"></dubbo:provider>
public ProviderConfig providerConfig() {
ProviderConfig providerConfig = new ProviderConfig();
providerConfig.setTimeout(1000);
return providerConfig;
}
}
此外,我们在需要提供的接口上使用 dubbo 的 @Service 注解,暴露接口
@com.alibaba.dubbo.config.annotation.Service //暴露接口
@Component
public class PersonServiceImpl implements PersonService {
@Override
public Person getPersonById(Integer id) {
return null;
}
}
在启动时,使用 @EnableDubbo,并扫描需要向外提供接口的包
@EnableDubbo(scanBasePackages = "cn.edu.pzhu.zc.service")
@SpringBootApplication
public class SpringBootQuickApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootQuickApplication.class, args);
}
}