From dcc05fcb8c8544fa47d7f5a91a57076a6cba8d5d Mon Sep 17 00:00:00 2001 From: njkfei <297206021@qq.com> Date: Mon, 14 Mar 2016 22:11:22 +0800 Subject: [PATCH] Create README.md --- README.md | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..92ae1fc --- /dev/null +++ b/README.md @@ -0,0 +1,153 @@ +# dubbo-spring-java-config-comsumer + +## 说明 +有几个坑 +### spring 依赖问题 + 在dubbo依赖于spring2,如果集成spring3会有依赖冲突问题,需要添加依赖解决. +``` + + com.alibaba + dubbo + 2.5.3 + + + spring + org.springframework + + + service(){ + ServiceConfig service = new ServiceConfig(); // 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏 + service.setApplication(application()); + service.setRegistry(registry()); // 多个注册中心可以用setRegistries() + service.setProtocol(protocol()); // 多个协议可以用setProtocols() + service.setInterface(DemoService.class); + service.setVersion("1.0.0"); // 服务版本 + service.setRef(demoService()); + service.export(); + + return service; + } +``` + +``` + // 服务引用 配置,需要与服务提供者版本保持一致 + @Bean + public ReferenceConfig reference(){ + ReferenceConfig reference = new ReferenceConfig(); + reference.setApplication(application()); + reference.setRegistry(registry()); + reference.setProtocol("dubbo"); + reference.setInterface(DemoService.class); + reference.setId("demoService"); + reference.setVersion("1.0.0"); // 这行必须要有 + + return reference; + + } +``` + +如果不一致,会报下面的错误 +``` +Exception in thread "main" java.lang.IllegalStateException: Failed to check the status of the service com.niejinkun.dubbo.spring4start.service.DemoService. No provider available for the service com.niejinkun.dubbo.spring4start.service.DemoService from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=demoService-comsumer&dubbo=2.5.3&interface=com.niejinkun.dubbo.spring4start.service.DemoService&methods=sayHello&pid=2432&protocol=dubbo&side=consumer×tamp=1457964636762 to the consumer 192.168.1.236 use dubbo version 2.5.3 + at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:420) + at com.alibaba.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:300) + at com.alibaba.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:138) + at com.niejinkun.dubbo.spring4start.App.main(App.java:24) + +``` + +## 完整配置代码 +``` +package com.niejinkun.dubbo.spring4start.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.alibaba.dubbo.config.ApplicationConfig; +import com.alibaba.dubbo.config.ProtocolConfig; +import com.alibaba.dubbo.config.ReferenceConfig; +import com.alibaba.dubbo.config.RegistryConfig; +import com.alibaba.dubbo.config.ServiceConfig; +import com.alibaba.dubbo.config.annotation.Reference; +import com.niejinkun.dubbo.spring4start.service.DemoService; + +@Configuration +public class DubboConfig { + + // 当前应用配置 + @Bean + public ApplicationConfig application() { + ApplicationConfig application = new ApplicationConfig(); + application.setName("demoService-comsumer"); + + return application; + } + + // 连接注册中心 + @Bean + public RegistryConfig registry() { + RegistryConfig registry = new RegistryConfig(); + registry.setProtocol("zookeeper"); + registry.setAddress("zookeeper://127.0.0.1:2181"); +/* registry.setUsername("aaa"); + registry.setPassword("bbb");*/ + return registry; + } + + // 服务提供者协议配置 + @Bean + public ProtocolConfig protocol(){ + ProtocolConfig protocol = new ProtocolConfig(); + protocol.setName("dubbo"); + protocol.setPort(20880); + protocol.setThreads(200); + + return protocol; + } + // 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口 + +/* // 服务提供者暴露服务配置 + @Bean + public ServiceConfig service(){ + ServiceConfig service = new ServiceConfig(); // 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏 + service.setApplication(application()); + service.setRegistry(registry()); // 多个注册中心可以用setRegistries() + service.setProtocol(protocol()); // 多个协议可以用setProtocols() + service.setInterface(DemoService.class); + service.setVersion("1.0.0"); + + return service; + }*/ + + // 服务引用 配置,需要与服务提供者版本保持一致 + @Bean + public ReferenceConfig reference(){ + ReferenceConfig reference = new ReferenceConfig(); + reference.setApplication(application()); + reference.setRegistry(registry()); + reference.setProtocol("dubbo"); + reference.setInterface(DemoService.class); + reference.setId("demoService"); + //reference.setVersion("1.0.0"); // 这行必须要有 + + return reference; + + } + +/* @Bean + public DemoService demoService(){ + return reference().get(); + } + */ + +} + +```