`<dubbo:service>` 是 Apache Dubbo 中用于服务提供者端配置和暴露远程服务的 XML 元素

<dubbo:service> 是 Apache Dubbo 中用于服务提供者端配置和暴露远程服务的 XML 元素。以下是对 <dubbo:service> 的详细解析:

<dubbo:service> 的作用

  1. 服务暴露

    • <dubbo:service> 的主要作用是将服务提供者的本地服务暴露为远程服务,使得服务消费者能够通过网络进行调用。
  2. 配置服务元数据

    • 通过该元素,服务提供者可以配置服务的接口、实现类、版本、分组等元数据,以便服务消费者能够准确地找到并调用所需的服务。

<dubbo:service> 的配置参数

以下是一些常见的配置参数及其作用:

  • interface

    • 描述:服务的接口类名(全路径),指定要暴露的服务接口。
    • 示例<dubbo:service interface="com.example.DemoService" ... />
    • 重要性:这是必需的参数,因为没有接口,消费者无法知道如何调用服务。
  • ref

    • 描述:服务接口的实现类引用,指定服务接口的具体实现类。
    • 示例<dubbo:service ref="demoServiceImpl" ... />
    • 重要性:这是必需的参数,因为服务消费者需要知道服务接口的具体实现。
  • version

    • 描述:服务的版本号,用于区分不同版本的服务。
    • 示例<dubbo:service version="1.0.0" ... />
    • 作用:支持服务的向后兼容,消费者可以指定调用特定版本的服务。
  • group

    • 描述:服务的分组,用于将服务分组,便于管理和调用。
    • 示例<dubbo:service group="group1" ... />
    • 作用:当同一个接口有多个实现时,可以用分组来区分不同的实现。
  • protocol

    • 描述:服务使用的协议,如 Dubbo 协议、HTTP 协议等。
    • 示例<dubbo:service protocol="dubbo" ... />
    • 作用:指定服务使用的通信协议,消费者需要支持相应的协议才能调用服务。
  • port

    • 描述:服务监听的端口号。
    • 示例<dubbo:service port="20880" ... />
    • 作用:指定服务监听的端口,消费者需要通过该端口调用服务。
  • timeout

    • 描述:远程服务调用的超时时间(毫秒)。
    • 示例<dubbo:service timeout="3000" ... />
    • 作用:设置服务调用的超时时间,避免长时间等待。
  • retries

    • 描述:远程服务调用失败后的重试次数。
    • 示例<dubbo:service retries="2" ... />
    • 作用:设置服务调用失败后的重试次数,提高服务调用的可靠性。

XML 配置示例

<dubbo:service interface="com.example.DemoService" 
                 ref="demoServiceImpl" 
                 version="1.0.0" 
                 group="group1" 
                 protocol="dubbo" 
                 port="20880" 
                 timeout="3000" 
                 retries="2" />

注意事项

  1. 接口和实现类<dubbo:service> 中的 interfaceref 属性是必需的,它们分别指定了服务的接口和实现类。

  2. 版本和分组versiongroup 属性用于区分不同的服务版本和分组,有助于服务的管理和调用。

  3. 协议和端口protocolport 属性指定了服务使用的通信协议和监听的端口,消费者需要支持相应的协议和访问端口才能调用服务。

  4. 超时和重试:合理设置 timeoutretries 属性可以提高服务调用的可靠性和用户体验。

总结

<dubbo:service> 是 Dubbo 中用于服务提供者端配置和暴露远程服务的关键元素。通过该元素,服务提供者可以灵活地配置服务的元数据、通信协议、端口号等参数,以便服务消费者能够准确地找到并调用所需的服务。在实际应用中,应根据具体需求合理配置这些参数,以确保服务的稳定性和性能。
The configuration of the service provider. The corresponding class is org.apache.dubbo.config.ServiceConfig.
Attribute Corresponding URL parameter Type Required Default Value Function Description Compatibility
interface class True Service discovery The service interface name Above 1.0.0
ref object True Service discovery The reference to the service implementation Above 1.0.0
version version string False 0.0.0 Service discovery Service version. It’s recommended to use 2 digitals such as 1.0. It’s necessary to upgrade version only when the service is not compatible. Above 1.0.0
group group string False Service discovery The group of the service providers. It can distinguish services when it has multiple implements. Above 1.0.7
path string False default value is the interface name Service discovery In 1.0, service path is not supported, it’s always equals to the interface name. So it may not compitable when a service reference in 1.0 calls a service provider in 2.0 that specified path. Above 1.0.12
delay delay int False 0 Performance optimize The delay time(ms) for registering services. When set to -1, it indicates that the services will expose to registry after the Spring context is initialized Above 1.0.14
timeout timeout int False 1000 Performance optimize The RPC timeout(ms) Above 2.0.0
retries retries int False 2 Performance optimize The retry count for RPC, not including the first invoke. Please set it to 0 if don’t need to retry. Above 2.0.0
connections connections int False 100 Performance optimize The maximum connections of every provider. For short connection such as rmi, http and hessian, it’s connection limit, but for long connection such as dubbo, it’s connection count. Above 2.0.0
loadbalance loadbalance string False random Performance optimize Strategy of load balance, random, roundrobin and leastactive are available. Above 2.0.0
async async boolean False false Performance optimize Asynchronous execution, not reliable. It does not block the execution thread just only ignores the return value. Above 2.0.0
local local class/boolean False false Service governance true means use the default proxy class name, which is the interface name with Local as the suffix. But it has been marked as @Deprecated.Pleas see also stub. Above 2.0.0
stub stub class/boolean False false Service governance true means use the default proxy class name, which is the interface name with Stub as the suffix. It’s used to execute local logic such as caching. The proxy class must have a constructor with the remote proxy object as a parameter, such as public XxxServiceStub(XxxService xxxService) Above 2.0.0
mock mock class/boolean False false Service governance true means use the default mock class name, which is the interface name with Mock suffix. It’s called when the RPC is failed, such as timeout or IO exception. The mock class must carry a none parameter constructor. The difference between mock and local proxy is that local proxy is always invoked before RPC but mock is invoked only when exception after RPC. Above 2.0.0
token token string/boolean False false Service governance Enable token validation. Disable token if it’s null. It will generate token randomly when enable, or will use static token. The token is designed to prevent consumers from bypassing the registry direct access to provider. If you want peer to peer, token validation must disbable. Above 2.0.0
registry string False register to all registries by default Configuration association Register services to specified registry while having multiple registries. It is the id value of the dubbo:registry. If don’t want to register to any registry, set it as N/A Above 2.0.0
provider string False use the first configured provider Configuration association The reference to dubbo:provider Above 2.0.0
deprecated deprecated boolean False false Service governance Mark the service is deprecated. If true, there will log an error log on the client side. Above 2.0.5
dynamic dynamic boolean False true Service governance Whether the service is registered dynamically. If false, services will be showed as disable, you need to enable it manually. And you also need to disable it when provider shut down. Above 2.0.5
accesslog accesslog string/boolean False false Service governance true will write access log to logger. Specifying it to a log path, you can write access logs to special log file. Above 2.0.5
owner owner string False Service governance The owner of the service. It’s used for service governance. Above 2.0.5
document document string False Service governance Service document URL Above 2.0.5
weight weight int False Performance optimize The weight of the service Above 2.0.5
executes executes int False 0 Performance optimize The maximum parallel execution request count per method per service for the provider. Above 2.0.5
proxy proxy string False javassist Performance optimize The proxy implement, jdk/javassist are available. Above 2.0.5
cluster cluster string False failover Performance optimize failover/failfast/failsafe/failback/forking are available. Above 2.0.5
filter service.filter string False default Performance optimize The filter name of the RPC process of the provider, use , to separate multiple filter names. Above 2.0.5
listener exporter.listener string False default Performance optimize The listener name of the provider, use , to separate multiple listener names.
protocol string False Configuration association Specify the protocol for service provider. It references the id of dubbo:protocol tag. Use , to separate multiple protocols. Above 2.0.5
layer layer string False Service governance The biz layer of the service provider, such as biz, dao, intl:web and china:acton. Above 2.0.7
register register boolean False true Service governance Whether registering service providers to regis
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bol5261

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值