• dubbo使用带有密码的redis注册中心完整配置及遇到问题解决、RestTemplate配置【持续更新】


    业务需要整合dubbo,考虑成本,使用现有的redis作为注册中心,项目使用的Spring-boot-starter版本2.7.6

    步骤:

    参考官方中文文档http://dubbo.apache.org/zh-cn/docs/user/quick-start.html

    1.在项目中导入依赖:(1.服务提供者provider、服务消费者consumer都需要导入以下jar)

    
            
                org.apache.dubbo
                dubbo-spring-boot-starter
                2.7.6
            
            
                redis.clients
                jedis
                2.9.0
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.创建一个module用于编写dubbo接口,即provider、consumer都需要依赖的,个人建议:不要做任何依赖,越干净越好

    3.毫无疑问provider、consumer都需要依赖该module

    4.分别对provider、consumer进行配置

    provider、consumer配置基本一样,唯一不一样的地方是dubbo.application.name=xxxx

    dubbo:
      scan:
        base-packages: com.xxxx.ccp.service
      registry:
        protocol: redis
        parameters:
          # 指定redis数据库,别问我为什么是这个参数,看源码发现的
          db.index: 1
        # 上面配置了redis故直接取值即可,也可以直接写成 address: root:123456@127.0.0.1:6379
        address: root:${spring.redis.password}@${spring.redis.host}:${spring.redis.port}
        cluster: failover
      protocol:
        # 名字任意
        id: xxx-protocol
        port: 20880
        name: dubbo
      application:
        # 名字任意
        name: xxxx-application
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    5.剩下就是provider实现上面module中的接口,记得在类上加上@service注解,注意:不是Spring的service注解,是org.apache.dubbo.config.annotation.Service注解

    6.服务consumer消费处,直接@Reference注入即可 @注解路径是org.apache.dubbo.config.annotation.Reference,示例

        @Reference
        private DubboProviderService dubboProviderService;
    
    • 1
    • 2

    启动顺序:

    redis服务-> provider -> consumer

    遇到的坑

    Invalid name="com.alibaba.dubbo.config.ProtocolConfig#0"
    
    • 1

    原因是

    dubbo采用Spring 工具类BeanDefinitionReaderUtils.generateBeanName(builder.getRawBeanDefinition(),register)得到的bean带有#,而dubbo进行验证pattern("[-._0-9a-zA-Z]+"不包含#故报错
    
    • 1

    解决办法:

    设置dubbo.protocol.id=xxx # 名称任意唯一即可

    初始配置为

    考虑到redis不需要用户名这样配置
    dubbo:
      registry:
        protocol: redis
        parameters:
          db.index: 1
        address: 127.0.0.1:6379
        password: 123456
    
    启动报错提示连接没有授权,即使配置了password也不行,修改为如下(加上username随便写一值反正没用)
    dubbo:
      registry:
        protocol: redis
        parameters:
          db.index: 1
        address: 127.0.0.1:6379
        username: root
        password: 123456
    启动仍然报错
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    修改为如下完美解决:

    dubbo:
      registry:
        protocol: redis
        parameters:
          db.index: 1
        address: root:123456@127.0.0.1:6379
        cluster: failover
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    为解决问题,顺便看了url解析逻辑,先根据“”split,然后在判断有没有"😕/“,再判断有没有”@",每次分隔进行参数封装

    2020.6.16尝试多提供者测试发现问题:

    背景:使用jedis版本2.9.0,启动报错:

    classNotFoundredis/clients/util/Pool

    修改方法: 将jedis版本降至2.8.1即可

    2)消费端启动报错,异常信息

    org.apache.dubbo.rpc.RpcException: Fail to start server(url: dubbo://x.x.x.x:20880/com.hanhai.ccp.web.service.event.ITaskEvent?anyhost=true&application=ccp-wep&bind.ip=10.10.10.165&bind.port=20880&channel.readonly.sent=true&codec=dubbo&default=true&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&heartbeat=60000&interface=com.hanhai.ccp.web.service.event.ITaskEvent&methods=getTaskEvent,sendTaskEvent,getTaskDetail&pid=12818&qos.enable=false&release=2.7.6&side=provider×tamp=1592904686840) Failed to bind NettyServer on /10.10.10.165:20880, cause: Address already in use
    	at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol.createServer(DubboProtocol.java:348) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol.openServer(DubboProtocol.java:320) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol.export(DubboProtocol.java:303) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.rpc.protocol.ProtocolListenerWrapper.export(ProtocolListenerWrapper.java:62) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper.export(ProtocolFilterWrapper.java:153) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.qos.protocol.QosProtocolWrapper.export(QosProtocolWrapper.java:66) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.rpc.Protocol$Adaptive.export(Protocol$Adaptive.java) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.registry.integration.RegistryProtocol.lambda$doLocalExport$2(RegistryProtocol.java:244) ~[dubbo-2.7.6.jar:2.7.6]
    	at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660) ~[na:1.8.0_202]
    	at org.apache.dubbo.registry.integration.RegistryProtocol.doLocalExport(RegistryProtocol.java:242) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.registry.integration.RegistryProtocol.export(RegistryProtocol.java:199) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.rpc.protocol.ProtocolListenerWrapper.export(ProtocolListenerWrapper.java:60) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper.export(ProtocolFilterWrapper.java:151) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.qos.protocol.QosProtocolWrapper.export(QosProtocolWrapper.java:64) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.rpc.Protocol$Adaptive.export(Protocol$Adaptive.java) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol(ServiceConfig.java:492) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.config.ServiceConfig.doExportUrls(ServiceConfig.java:325) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.config.ServiceConfig.doExport(ServiceConfig.java:300) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.config.ServiceConfig.export(ServiceConfig.java:206) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.config.bootstrap.DubboBootstrap.lambda$exportServices$15(DubboBootstrap.java:917) ~[dubbo-2.7.6.jar:2.7.6]
    	at java.util.HashMap$Values.forEach(HashMap.java:981) ~[na:1.8.0_202]
    	at org.apache.dubbo.config.bootstrap.DubboBootstrap.exportServices(DubboBootstrap.java:905) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.config.bootstrap.DubboBootstrap.start(DubboBootstrap.java:745) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.config.spring.context.DubboBootstrapApplicationListener.onContextRefreshedEvent(DubboBootstrapApplicationListener.java:59) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.config.spring.context.DubboBootstrapApplicationListener.onApplicationContextEvent(DubboBootstrapApplicationListener.java:52) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.config.spring.context.OneTimeExecutionApplicationContextEventListener.onApplicationEvent(OneTimeExecutionApplicationContextEventListener.java:40) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:404) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:361) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:898) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:162) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    	at com.hanhai.ccp.BootApplication.main(BootApplication.java:29) [classes/:na]
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_202]
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_202]
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_202]
    	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_202]
    	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    Caused by: org.apache.dubbo.remoting.RemotingException: Failed to bind NettyServer on /x.x.x.x:20880, cause: Address already in use
    	at org.apache.dubbo.remoting.transport.AbstractServer.(AbstractServer.java:77) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.remoting.transport.netty4.NettyServer.(NettyServer.java:77) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.remoting.transport.netty4.NettyTransporter.bind(NettyTransporter.java:35) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.remoting.Transporter$Adaptive.bind(Transporter$Adaptive.java) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.remoting.Transporters.bind(Transporters.java:56) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.remoting.exchange.support.header.HeaderExchanger.bind(HeaderExchanger.java:44) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.remoting.exchange.Exchangers.bind(Exchangers.java:70) ~[dubbo-2.7.6.jar:2.7.6]
    	at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol.createServer(DubboProtocol.java:346) ~[dubbo-2.7.6.jar:2.7.6]
    	... 45 common frames omitted
    Caused by: java.net.BindException: Address already in use
    	at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_202]
    	at sun.nio.ch.Net.bind(Net.java:433) ~[na:1.8.0_202]
    	at sun.nio.ch.Net.bind(Net.java:425) ~[na:1.8.0_202]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    原因1:配置的包扫描问题,(扫描了基础包路径,一般配置扫描com.xxx.xxx.service)

    修改为指定具体路径(确保唯一,很多项目都使用基础包路径,结果普通接口和dubbo提供的接口放在一起,这时会将其他接口也扫描进来,并当作服务端进行注册,会看到上述的错误)

    修改包路径即可解决

    关于包扫描配置说明2点:

    dubbo:
      scan:
        base-packages: com.xxxx.ccp.service
    
    • 1
    • 2
    • 3

    a)服务提供者配置包扫描时,配置的是注解dubbo的@service所在的包路径(请确保唯一,否则出现奇奇怪怪的问题不便于排查)

    b)消费端配置包扫描时,配置的是注解@Reference所在的包路径(请确保唯一,建议精确到impl层)

    c)即使不配置base-packages程序也会自动扫描带有@Service的注解进行注册

    原因2:服务实现类注解用错@service本应用Spring的,结果导成dubbo的注解

    解决:修改为正确的注解

    1. RestTemplate配置redis序列化、反序列化、spring 配置

      package cn.venny.datas.core.config;

      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.data.redis.connection.RedisConnectionFactory;
      import org.springframework.data.redis.core.RedisTemplate;
      import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;

      /**

      • @author Vick C

      • @version 1.0
        */
        @Configuration
        public class RedisConfig {

        @Bean
        public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
        }
        }

  • 相关阅读:
    linux多线程通信与同步(线程创建以及锁、条件变量)
    【设计模式】享元模式
    GRE隧道技术
    吉他 手小琴大解决方法
    echarts 仪表盘统计图
    react 高效高质量搭建后台系统 系列 —— 结尾
    认知世界和提示自己不可或缺的50本书
    AUTODL云服务器使用大致步骤(适合本人版)
    混合动力电动车优化调度与建模(发动机,电机,电池组等组件建模)(Matlab代码实现)
    docker-私有仓库Harbor-个人网盘
  • 原文地址:https://blog.csdn.net/m0_67391120/article/details/126327968