专注Java教育14年 全国咨询/投诉热线:444-1124-454
赢咖4LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 Dubbo消费者配置的方法

Dubbo消费者配置的方法

更新时间:2022-12-06 10:11:29 来源:赢咖4 浏览1085次

相信大家对什么是Dubbo已经有所了解,那么,Dubbo消费者配置的方法有哪些?赢咖4小编来告诉大家。

消费者配置详解

Dubbo Consumer的配置有3种方式:XML配置、API调用方式配置、注解方式配置。

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://dubbo.apache.org/schema/dubbo"
    Xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema /dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <dubbo:application name="hello-world-app" />
    <dubbo:registry address="multicast://224.5.6.7:1234" />
    <dubbo:protocol name="dubbo" port="20880" />
    <dubbo:reference id="demoServiceRemote" interface="com.alibaba.dubbo.demo.DemoService" />
</beans>

注解

参考注解远程服务

Public class AnnotationConsumeService {
    @com.alibaba.dubbo.config.annotation.Reference
    Public AnnotateService annotateService;
    // ...
}

这种方式的配置和前面xml中的配置是一样的。

api直接触发

Import com.alibaba.dubbo.rpc.config.ApplicationConfig;
Import com.alibaba.dubbo.rpc.config.RegistryConfig;
Import com.alibaba.dubbo.rpc.config.ConsumerConfig;
Import com.alibaba.dubbo.rpc.config.ReferenceConfig;
Import com.xxx.XxxService;
// current application configuration
ApplicationConfig application = new ApplicationConfig();
application.setName("yyy");
// Connect to the registry configuration
RegistryConfig registry = new RegistryConfig();
registry.setAddress("10.20.130.230:9090");
registry.setUsername("aaa");
registry.setPassword("bbb");
// Note: ReferenceConfig is a heavy object that internally encapsulates the connection to the registry and the connection to the service provider.
// reference remote service
ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // This instance is heavy, encapsulates the connection to the registry and the connection to the provider, please cache it yourself, otherwise it may cause memory and connection leaks.
reference.setApplication(application);
reference.setRegistry(registry); // Multiple registries can use setRegistries()
reference.setInterface(XxxService.class);
reference.setVersion("1.0.0");
// Use xxxService like local beans
XxxService xxxService = reference.get();

方法特殊设置

// Method level configuration
List<MethodConfig> methods = new ArrayList<MethodConfig>();
MethodConfig method = new MethodConfig();
method.setName("createXxx");
method.setTimeout(10000);
method.setRetries(0);
Methods.add(method);
// reference remote service
ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // This instance is heavy, encapsulates the connection to the registry and the connection to the provider, please cache it yourself, otherwise it may cause memory and connection leaks.
...
reference.setMethods(methods); // Set method level configuration

消费者调用远程服务

需要通过 Dubbo 调用远程服务。具体步骤如下:

1.创建项目 如果已经有项目,可以忽略。创建一个可以在https://start.spring.io/创建的 spring boot 项目。服务的提供者已在提供者部分定义。

2.调用服务

@RestController
Public class UserTestController{
    @Autowired
    Private UserReadService userReadService;
    @RequestMapping("/user/getById")
    Public String getUserById(Long id){
        // just test
        Return userReadService.getUserById(id).toString();
    }
}

3.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://dubbo.apache.org/schema/dubbo"
    Xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema /dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <dubbo:application name="hello-world-app" />
    <dubbo:registry address="multicast://224.5.6.7:1234" />
    <dubbo:protocol name="dubbo" port="20880" />
    <dubbo:reference id="userReadService" interface="com.package.UserReadService"check="false" />
</beans>

以上就是关于“Dubbo消费者配置的方法”介绍,大家如果想了解更多可查看Dubbo教程,里面还有更丰富的知识等着大家去学习,希望对大家能够有所帮助。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>