标签:
Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案
Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点:
Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。
其核心部分包含:
Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。
主要核心部件
Remoting: 网络通信框架,实现了sync-over-async 和 request-response 消息机制.
RPC: 一个远程过程调用的抽象,支持负载均衡、容灾和集群功能
Registry: 服务目录框架用于服务的注册和服务事件发布和订阅。
Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mor.maven</groupId> <artifactId>dubboserver</artifactId> <version>0.0.1</version> <packaging>jar</packaging> <name>dubboserver</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>3.1.4.RELEASE</spring.version> <slf4j.version>1.6.6</slf4j.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-asm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <!-- spring end --> <!-- log --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <!-- zkclient --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!-- zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.3.6</version> </dependency> </dependencies> <build> <finalName>dubbo-demo</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.1</version> <configuration> <source>1.5</source> <target>1.5</target> <encoding>UTF-8</encoding> <failOnError>false</failOnError> </configuration> </plugin> </plugins> </build> </project>
因为要增加zookeeper的注册管理,所以如果有可用的zookeeper就用可用的zookeeper,没有可以按照如下的安装去本地安装一个。
applicationProvider.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <dubbo:application name="hello-world-app" /> <!-- 本机 伪集群 测试 --> <dubbo:registry protocol="zookeeper" address="192.9.145.19:2181,192.9.145.19:2182,192.9.145.19:2183" /> <dubbo:protocol name="dubbo" port="20880" /> <dubbo:service interface="com.mor.server.dubbo.service.DemoServer" ref="demoService" /> <!-- 和本地bean一样实现服务 --> <bean id="demoService" class="com.mor.server.dubbo.service.DemoServerImpl" /> </beans>
客户端:
applicationConsumer.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --> <dubbo:application name="consumer-of-helloworld-app" />
<span style="white-space:pre"> </span><!-- 使用本地伪集群注册中心暴露发现服务地址 -->
<dubbo:registry protocol="zookeeper" address="192.9.145.19:2181,192.9.145.19:2182,192.9.145.19:2183" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.mor.server.dubbo.service.DemoServer" />
</beans>
在客户端要引入服务端生成的jar包
demodubboserver\src\main\java\com\mor\server\dubbo\service\DemoServer.java
package com.mor.server.dubbo.service; public interface DemoServer { String sayHello(String str); }
demodubboserver\src\main\java\com\mor\server\dubbo\service\DemoServerImpl.java
<span style="font-family:Courier New;">package com.mor.server.dubbo.service; import java.util.Date; public class DemoServerImpl implements DemoServer { public String sayHello(String str) { str = "Hello " + str + "2:" + new Date(); System.out.println("server:" + str); return str; } }</span><strong style="font-family: Arial;"> </strong>
demodubboserver\src\main\java\com\mor\main\Main.java
package com.mor.main; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationProvider.xml" }); context.start(); System.out.println("按任意键退出"); System.in.read(); } }
log4j.properties
log4j.appender.Stdout=org.apache.log4j.ConsoleAppender log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\n log4j.rootLogger=INFO,Stdout log4j.logger.org.apache.wicket=INFO log4j.logger.org.apache.wicket.protocol.http.HttpSessionStore=INFO log4j.logger.org.apache.wicket.version=INFO log4j.logger.org.apache.wicket.RequestCycle=INFO
客户端代码:
package com.mor.client.dubbo.action; import java.util.Date; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.mor.server.dubbo.service.DemoServer; public class ChatAction { public void SayHello(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" }); context.start(); DemoServer demoServer = (DemoServer) context.getBean("demoService"); System.out.println("client:"+demoServer.sayHello("Morning"+"1:"+new Date())+"3:"+new Date()); } }
dubboclient\src\main\java\com\mor\client\dubbo\main\Main.java
package com.mor.client.dubbo.main; import com.mor.client.dubbo.action.ChatAction; public class Main { public static void main(String[] args){ ChatAction act = new ChatAction(); act.SayHello(); } }
============启动顺序================
1.先启动 zookeeper 集群
标签:
原文地址:http://www.cnblogs.com/xmzzp/p/4178064.html