1.1提供者的application.properties
server.port=8080
# 提供者名称,唯一
dubbo.application.name=boot-provider
# 配置注册中心
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
# 配置协议以及端口
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
# 配置超时时间,默认为1000ms
dubbo.registry.timeout=5000
# 配置如果连接断开时,要重连次数,不包含第一次连接,0代表不重尝试连接
dubbo.provider.retries=3
# 配置监控中心要监控的地址,监控注册中心的地址
dubbo.monitor.address=registry
1.2暴露服务
1.3主程序
2.1消费者的application.properties
server.port=8081
# 消费者名称,唯一
dubbo.application.name=boot-consumer
# 配置注册中心
dubbo.registry.address=zookeeper://127.0.0.1:2181
# 配置超时时间,默认为1000ms
dubbo.registry.timeout=5000
# 配置如果连接断开时,要重连次数,不包含第一次连接,0代表不重尝试连接
dubbo.consumer.retries=3
# 配置监控中心要监控的地址
dubbo.monitor.address=registry
2.2调用远程服务
package com.yl.bootconsumer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.yl.bootinterface.model.Student;
import com.yl.bootinterface.service.StudentService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {
@Reference //引用远程服务
StudentService studentService;
@GetMapping("/getStudentById")
public Student getStudentById(Integer id) {
return studentService.getStudentById(id);
}
}
2.3主程序
1.1application.properties
1.2dubbo-provider.xml
<?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"
xmlns:dubbbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--声明提供者名称-->
<dubbo:application name="boot-provider"/>
<!--指定协议以及端口号-->
<dubbbo:protocol name="dubbo" port="20880"/>
<!--指定注册中心-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--暴露服务-->
<dubbo:service interface="com.yl.bootinterface.service.StudentService" ref="studentServiceImpl" version="0.0.1" timeout="5000">
<dubbo:method name="getStudentById" timeout="2000"/>
</dubbo:service>
<!--加载实现类-->
<bean id="studentServiceImpl" class="com.yl.bootprovider.service.impl.StudentServiceImpl"/>
</beans>
1.3接口实现类
1.4主程序
1.1application.properties
1.2dubbo-consumer.xml
<?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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--指定消费者名称-->
<dubbo:application name="boot-consumer"/>
<!--指定注册中心-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--引用远程服务接口-->
<dubbo:reference id="studentService" interface="com.yl.bootinterface.service.StudentService" version="0.0.1"/>
</beans>
1.3调用服务
1.4主程序
1.1提供一个配置类
package com.yl.bootprovider.config;
import com.alibaba.dubbo.config.*;
import com.yl.bootinterface.service.StudentService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class DubboConfig {
// 配置应用
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("boot-provider");
return applicationConfig;
}
//配置注册中心
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setProtocol("zookeeper");
registryConfig.setAddress("127.0.0.1:2181");
return registryConfig;
}
//配置协议
@Bean
public ProtocolConfig protocolConfig() {
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName("dubbo");
protocolConfig.setPort(20880);
return protocolConfig;
}
//暴露服务
@Bean
public ServiceConfig<StudentService> studentServiceServiceConfig(StudentService studentService) {
ServiceConfig<StudentService> serviceConfig = new ServiceConfig<>();
serviceConfig.setInterface(StudentService.class);
serviceConfig.setRef(studentService);
serviceConfig.setVersion("0.0.1");
//方法的具体配置
List<MethodConfig> list = new ArrayList<>();
MethodConfig methodConfig = new MethodConfig();
methodConfig.setName("getStudentById");
methodConfig.setTimeout(2000);
list.add(methodConfig);
serviceConfig.setMethods(list);
return serviceConfig;
}
}
2.要暴露的接口服务实现类
1.3 主程序
2.1提供一个配置类
package com.yl.bootconsumer.config;
import com.alibaba.dubbo.config.*;
import com.yl.bootinterface.service.StudentService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class DubboConfig {
// 配置应用
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("boot-consumer");
return applicationConfig;
}
//配置注册中心
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setProtocol("zookeeper");
registryConfig.setAddress("127.0.0.1:2181");
return registryConfig;
}
//配置协议
@Bean
public ProtocolConfig protocolConfig() {
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName("dubbo");
protocolConfig.setPort(20880);
return protocolConfig;
}
}
2.2.主程序
2.3调用远程服务
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_41359273/article/details/124642758
内容来源于网络,如有侵权,请联系作者删除!