使用XML配置进行整合
如果使用xml做SpringBoot配置只需要将application.properties中的内容注释:
##The service name
#dubbo.application.name=boot-service-provider
#
##registration center
#dubbo.registry.address=127.0.0.1:2181
#dubbo.registry.protocol=zookeeper
#
##communication rule
#dubbo.protocol.name=dubbo
#dubbo.protocol.port=22288
#
##The monitoring center
#dubbo.monitor.protocol=registry
##dubbo.scan.base-packages=com.dubbo.basics_interface
然后将provider.xml复制到resources目录下:
内容如:
<?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://dubbo.apache.org/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.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">
<!-- 包扫描 -->
<context:component-scan base-package="com.dubbo.service.provider.service.impl"></context:component-scan>
<!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
<dubbo:application name="boot-service-provider"></dubbo:application>
<!-- 2、指定注册中心的位置 -->
<!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
<!-- 3、指定通信规则(通信协议?通信端口) -->
<dubbo:protocol name="dubbo" port="22288"></dubbo:protocol>
<!-- 4、暴露服务 ref:指向服务的真正的实现对象 -->
<dubbo:service interface="com.dubbo.basics_interface.service.UserService"
ref="userServiceImpl" ></dubbo:service>
<!-- 连接监控中心 -->
<dubbo:monitor protocol="registry"></dubbo:monitor>
</beans>
注释@Service注解:
package com.service.provider.service.impl;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.basics_interface.dto.User;
import com.dubbo.basics_interface.service.UserService;
/*
* @author zzf
* @date 2019年1月29日 下午3:40:16
*/
@Component
//@Service
public class UserServiceImpl implements UserService{
public List<User> getUserAddressList(String userId) {
// TODO Auto-generated method stub
User address1 = new User(1, "XXXXXXX1", "1", "小A", "88888888888", "Y");
User address2 = new User(2, "XXXXXXX2", "2", "小B", "77777777777", "N");
User address3 = new User(3, "XXXXXXX3", "3", "小C", "99999999999", "Y");
return Arrays.asList(address1,address2,address3);
}
}
启动类修改:(注释@EnableDubbo注解,使用@ImportResource注解引入配置文件)
package com.service.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
//@EnableDubbo
@ImportResource(locations="classpath:provider.xml")
@SpringBootApplication
public class BootServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BootServiceProviderApplication.class, args);
}
}
运行:
OK~配置成功……
使用API配置进行整合
首先创建一个config类:
package com.service.provider.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import com.dubbo.basics_interface.service.UserService;
/*
* @author zzf
* @date 2019年1月31日 上午11:33:20
*/
@Configuration
public class DubboConfig {
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("boot-service-provider");
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(22288);
return protocolConfig;
}
/**
*<dubbo:service interface="com.atguigu.gmall.service.UserService"
ref="userServiceImpl01" timeout="1000" version="1.0.0">
<dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>
</dubbo:service>
*/
@Bean
public ServiceConfig<UserService> userServiceConfig(UserService userService){
ServiceConfig<UserService> serviceConfig = new ServiceConfig<>();
serviceConfig.setInterface(UserService.class);
serviceConfig.setRef(userService);
// serviceConfig.setVersion("1.0.0");
//
// //配置每一个method的信息
// MethodConfig methodConfig = new MethodConfig();
// methodConfig.setName("getUserAddressList");
// methodConfig.setTimeout(1000);
//
// //将method的设置关联到service配置中
// List<MethodConfig> methods = new ArrayList<>();
// methods.add(methodConfig);
// serviceConfig.setMethods(methods);
return serviceConfig;
}
}
将@Service注解加在服务上:
package com.service.provider.service.impl;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.basics_interface.dto.User;
import com.dubbo.basics_interface.service.UserService;
/*
* @author zzf
* @date 2019年1月29日 下午3:40:16
*/
@Component
@Service
public class UserServiceImpl implements UserService{
public List<User> getUserAddressList(String userId) {
// TODO Auto-generated method stub
User address1 = new User(1, "XXXXXXX1", "1", "小A", "88888888888", "Y");
User address2 = new User(2, "XXXXXXX2", "2", "小B", "77777777777", "N");
User address3 = new User(3, "XXXXXXX3", "3", "小C", "99999999999", "Y");
return Arrays.asList(address1,address2,address3);
}
}
修改启动类:
package com.service.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
//@EnableDubbo
//@ImportResource(locations="classpath:provider.xml")
@EnableDubbo(scanBasePackages="com.service.provider.*")
@SpringBootApplication
public class BootServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BootServiceProviderApplication.class, args);
}
}
运行:
Ok~