标签:health load zookeeper conf -- turn mem 依赖 很多
dubbo结合spring boot,直接采用注解的方式,没有采用xml配置1:创建空的maven项目,引入依赖:
<!--dubbo-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
2:使用properties配置
app.service.version=1.0.0
\# Base packages to scan Dubbo Component: @com.alibaba.dubbo.config.annotation.Service
dubbo.scan.basePackages=com.seasfood.producer.
\# Dubbo Config properties
\## ApplicationConfig Bean
dubbo.application.id=seasfood-dubbo-id
dubbo.application.name=seasfood-dubbo-name
dubbo.application.qos.port=22222
dubbo.application.qos.enable=true
\## ProtocolConfig Bean
dubbo.protocol.id=dubbo-id
\#只能是dubbo,这个是协议名
dubbo.protocol.name=dubbo
dubbo.protocol.port=9090
dubbo.protocol.status=server
\## RegistryConfig Bean
dubbo.registry.id=zookeeper
dubbo.registry.address=zookeeper://zk-cs:2181
\# Enables Dubbo All Endpoints
management.endpoint.dubbo.enabled=true
management.endpoint.dubbo-shutdown.enabled=true
management.endpoint.dubbo-configs.enabled=true
management.endpoint.dubbo-services.enabled=true
management.endpoint.dubbo-references.enabled=true
management.endpoint.dubbo-properties.enabled=true
\# Dubbo Health
\## StatusChecker Name defaults (default : "memory", "load" )
management.health.dubbo.status.defaults=memory
\## StatusChecker Name extras (default : empty )
management.health.dubbo.status.extras=load,threadpool
3:声明一个服务接口,声明一个实现类实现接口,然后在实现类上注解
public interface MyInterface{}
@Service(
version = "${app.service.version}",
application = "${dubbo.application.id}",
protocol = "${dubbo.protocol.id}",
registry = "${dubbo.registry.id}"
)
public class MyService implements MyIneterface {}
4:如果接口中需要传输DTO类型的,需要给类型实现序列化接口Serializable,否则在rpc调用会报错
5:消费者只需要注入使用即可
@Reference(version = "${app.service.version}",
lazy = true,
application = "${dubbo.application.id}",
registry = "${dubbo.registry.id}"
)
private MyInterface myInterface;
6:配置:有很多配置,目前我这用到的,一个是均衡负载配置,一个是服务降级配置,一个是集群容错(重试)br/>负载均衡配置在服务端和客户端都可以配置,配置loadbalance属性即可,缺省为random,可以配置roundrobin
集群容错配置retries属性,
服务降级配置:在@Reference里配置mock属性
mock=fail:return+null:当调用失败的时候返回null值
标签:health load zookeeper conf -- turn mem 依赖 很多
原文地址:https://blog.51cto.com/13860853/2362599