Springboot 整合dubbo多协议配置(dubbo、hessian协议)

1、yml 文件配置

首先在yml文件中种配置dubbo协议

dubbo:
  config-center:
    timeout: 6000
  application:
    name: info-service
  registry:
    address: zookeeper://127.0.0.1:2181
    timeout: 6000
  scan:
    base-packages: com.vale.info.server.service
  protocol:
    name: dubbo
    port: 20081

2、添加第二种协议相关依赖

此处配置的是hessian所以导入hessian相关包

<!--dubbo hessian协议-->
        <dependency>
            <groupId>com.caucho</groupId>
            <artifactId>hessian</artifactId>
            <version>4.0.7</version>
        </dependency>

        <!--dubbo多协议配置jetty包-->
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-util</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
        </dependency>

3、配置协议

@Configuration
public class DubboHessianConfig {

    // 配置rest协议
    @Bean("hessian")
    public ProtocolConfig restProtocolConfig() {
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName("hessian");
        protocolConfig.setId("hessian");
        protocolConfig.setServer("jetty");
        protocolConfig.setPort(9091);
        protocolConfig.setAccepts(500);
        protocolConfig.setThreads(100);
        // 可继续增加其它配置
        return protocolConfig;
    }
}

4、使用

//在使用的类上配置的buddo协议
@DubboService(interfaceClass = TaskService.class, version = "1.0.0",protocol = {"dubbo"})

//在使用的类上配置的hessian协议
@DubboService(interfaceClass = UploadService.class, version = "1.0.0",protocol = {"hessian"})

需要注意的是,在消费者端掉用配置多协议的dubbo服务时 也需要添加相关的依赖 即在第二步中的包 也需要在消费者端添加进去,否则会报找不到 服务提供者错误,切记切记

你可能感兴趣的:(spring,java,spring,boot)