DUBBO是一個分佈式服務框架,致力於提供高性能和透明化的RPC遠程服務調用方案,是阿里巴巴SOA服務化治理方案的核心框架,天天爲2,000+個服務提供3,000,000,000+次訪問量支持,並被普遍應用於阿里巴巴集團的各成員站點。java
http://www.oschina.net/p/dubbospring
1.導入依賴包(dubbo zookeeper zkclient) apache
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.7</version> </dependency>
2.寫提供者(服務端)的方法 tomcat
接口:springboot
public interface Hello { public String getString(String name); }
實現類: app
public class HelloImpl implements Hello { @Override public String getString(String name) { return name; } }
3.提供者的spring配置 框架
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- dubbo程序名字 -->--> <dubbo:application name="dubbo"/> <!-- 使用zookeeper註冊中心暴露服務地址 --> <dubbo:registry protocol="zookeeper" address="192.168.1.1:4181" username="用戶名" password="密碼"/> <!-- 用dubbo協議在20880端口暴露服務 --> <bean id="helloimpl" class="com.example.HelloImpl"/> <!--建立實現類的bean--> <dubbo:service interface="com.example.Hello" ref="helloimpl" id="hello"/> <!--將接口暴露給服務中心--> </beans>
4.寫消費者(客戶端)接口 分佈式
這裏能夠把服務端的接口複製過來就完事,若是你須要提供給其餘們人使用,最好不是複製接口,而是打成jar接口包。ide
5.導入和服務端同樣的依賴,配置spring配置 性能
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--名字能夠和服務端不一樣--> <dubbo:application name="dubbo"/> <!-- 使用zookeeper註冊中心暴露服務地址 --> <dubbo:registry protocol="zookeeper" address="192.168.1.1:4181" username="用戶名" password="密碼"/> <!-- 用dubbo協議在20880端口暴露服務 --> < dubbo : reference interface = "com.example.Hello" id = "hello" /> <!--複製過來的接口或者jar裏的接口 必須和服務端的接口一致 --> </beans>
6.客戶端test
public class DemoApplicationTests { @Autowired Hello h; @Test public void test() { String hh = h.getString("dubbo 遠程調用成功"); System.out.println(hh); } }
最後說一下不用tomcat啓動的方式
springboot集成dubbo
@SpringBootApplication @ImportResource("classpath:/dubbo-provider.xml")//啓動加在dubbo配置文件 public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); try { System.out.println("啓動成功,按任意鍵關閉"); System.in.read(); } catch (IOException e) { e.printStackTrace(); } } }
@SpringBootApplication public class DemoApplication { private static final Logger log = LoggerFactory.getLogger(DemoApplication.class); public static void main(String[] args) { try { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:/applicationContext*.xml"); context.start(); } catch (Exception e) { log.error("== DubboProvider context start error:", e); } synchronized (DemoApplication.class) { while (true) { try { DemoApplication.class.wait(); } catch (InterruptedException e) { log.error("== synchronized error:", e); } } } } }