1.添加依赖
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.11</version>
</dependency>
2.创建服务提供者
1。创建DubboProviderConfig文件
package com.csesteel.config;
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.csesteel.service.AccountService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class DubboConfig {
/*
* <dubbo:application name="account-provider"> <!-- 为了解决端口冲突
* https://blog.csdn.net/u012988901/article/details/84503672 -->
* <dubbo:parameter key="qos.enable" value="true"/> <dubbo:parameter
* key="qos.accept.foreign.ip" value="false"/> <dubbo:parameter key="qos.port"
* value="33333"/> </dubbo:application>
*/
@Bean
public ApplicationConfig applicationConfig(){ ApplicationConfig
applicationConfig=new ApplicationConfig();
applicationConfig.setName("account-provider");
Map<String,String> map=new HashMap<String,String>();
map.put("qos.enable","true");
map.put("qos.accept.foreign.ip","false"); map.put("qos.port","33333");
applicationConfig.setParameters(map);
return applicationConfig;
}
// <dubbo:registry id="acountaddr" address="zookeeper://localhost:2181"/>
@Bean
public RegistryConfig registryConfig(){ RegistryConfig
registryConfig=new RegistryConfig();
registryConfig.setId("acountaddr");
registryConfig.setAddress("zookeeper://localhost:2181");
return registryConfig;
}
// <dubbo:protocol name="dubbo" port="28080"/>
@Bean
public ProtocolConfig protocolConfig(){ ProtocolConfig
protocolConfig=new ProtocolConfig(); protocolConfig.setName("dubbo");
protocolConfig.setPort(28080);
return protocolConfig;
} //<dubbo:service registry="acountaddr" timeout="3000"
//interface="com.csesteel.api.AccountApi" ref="accountService"></dubbo:service>
@Bean
public ServiceConfig serviceConfig(){
ServiceConfig serviceConfig=new ServiceConfig();
serviceConfig.setRegistry(this.registryConfig());
serviceConfig.setInterface("com.csesteel.api.AccountApi");
serviceConfig.setTimeout(3000);
serviceConfig.setRef(AccountService.class);
return serviceConfig;
}
}
2.启动类使用@EnableDubbo启动dubbo注解
3.使用dubbo的@service注解标识服务实现类
3.创建功能的接口模块
1.提供公共的接口
4.创建服务消费者
1. 创建DubboCustomerConfig文件
package com.csesteel.config;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DubboConfig {
// <dubbo:application name="online-consumer"/>
@Bean
public ApplicationConfig applicationConfig(){
ApplicationConfig applicationConfig=new ApplicationConfig();
applicationConfig.setName("online-consumer");
return applicationConfig;
}
// <dubbo:registry address="zookeeper://localhost:2181"/>
@Bean
public RegistryConfig registryConfig(){
RegistryConfig registryConfig=new RegistryConfig();
registryConfig.setAddress("zookeeper://localhost:2181");
return registryConfig;
}
// <dubbo:protocol port="28080"/>
public ProtocolConfig protocolConfig(){
ProtocolConfig protocolConfig=new ProtocolConfig();
protocolConfig.setPort(28080);
return protocolConfig;
}
// <dubbo:reference id="accountService" interface="com.csesteel.api.AccountApi"/>
public ReferenceConfig referenceConfig(){
ReferenceConfig referenceConfig=new ReferenceConfig();
referenceConfig.setId("accountService");
referenceConfig.setInterface("com.csesteel.api.AccountApi");
referenceConfig.setTimeout(3000);
return referenceConfig;
}
}
2.启动类使用@EnableDubbo启动dubbo注解
3.Controller使用dubbo的@Reference注解注入service接口