dubbo(開源分散式服務框架)-demo
阿新 • • 發佈:2018-11-22
dubbo-demo-api 提供服務者和消費者都需要介面名和方法名才能呼叫
dubbo-demo-consumer 服務消費者(消費者)
dubbo-demo-provider 服務提供者(生產者)
dubbo-demo-parent maven父版本提供者
dubbo-demo-api專案
package com.java1234.service; /** * 服務提供者介面 * @author Administrator * */ public interface DemoProviderService { public String sayHello(String name); } pom.xml <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.java1234</groupId> <artifactId>dubbo-demo-api</artifactId> <version>0.0.1-SNAPSHOT</version> </project>
dubbo-demo-consumer(消費者)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 消費方應用名稱,用於計算依賴關係,不是匹配條件,不要與提供方名稱一樣 --> <dubbo:application name="demo-consumer"/> <!-- 使用zookeeper註冊中心暴露服務地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 生成遠端服務代理,可以與本地bean一樣使用 check屬性,啟動時候是否檢查 一般設定成false 啟動時候不檢查 --> <dubbo:reference id="demoProviderService" check="false" interface="com.java1234.service.DemoProviderService"/> </beans>
import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.java1234.service.DemoProviderService; public class ConsumerTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-demo-consumer.xml"}); context.start(); DemoProviderService demoProviderService=(DemoProviderService) context.getBean("demoProviderService"); String result=demoProviderService.sayHello("你好"); System.out.println("遠端呼叫的結果:"+result); try { System.in.read(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } context.close(); } }
<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>
<parent>
<groupId>com.java1234</groupId>
<artifactId>dubbo-demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dubbo-demo-consumer</artifactId>
<dependencies>
<dependency>
<groupId>com.java1234</groupId>
<artifactId>dubbo-demo-api</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
</dependencies>
</project>
dubbo-demo-provider 服務提供者(生產者)
package com.java1234.service.impl;
import com.java1234.service.DemoProviderService;
/**
* 服務提供者介面實現類
* @author Administrator
*
*/
public class DemoProviderServiceImpl implements DemoProviderService{
public String sayHello(String name) {
return "服務001"+name;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方應用名稱,用於計算依賴關係 -->
<dubbo:application name="demo-provider" />
<!-- 使用zookeeper註冊中心暴露服務地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 使用dubbo協議,在20880埠暴露服務 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- service實現類作為本地的一個bean -->
<bean id="demoProviderService" class="com.java1234.service.impl.DemoProviderServiceImpl"/>
<!-- 宣告需要暴露的服務介面 -->
<dubbo:service interface="com.java1234.service.DemoProviderService" ref="demoProviderService"/>
</beans>
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ProviderTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-demo-provider.xml"});
context.start();
System.out.println("服務提供者向zookeeper註冊中心註冊服務成功(埠:20880)");
try {
System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // press any key to exit
context.close();
}
}
<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>
<parent>
<groupId>com.java1234</groupId>
<artifactId>dubbo-demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dubbo-demo-provider</artifactId>
<dependencies>
<dependency>
<groupId>com.java1234</groupId>
<artifactId>dubbo-demo-api</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
</dependencies>
</project>
dubbo-demo-parent
<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.java1234</groupId>
<artifactId>dubbo-demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- 依賴版本管理 -->
<properties>
<dubbo-demo-api.version>0.0.1-SNAPSHOT</dubbo-demo-api.version>
<dubbo.version>2.6.0</dubbo.version>
<zkclient.version>0.10</zkclient.version>
<curator-framework.version>4.0.1</curator-framework.version>
<fastjson.version>1.2.46</fastjson.version>
<log4j.version>1.2.17</log4j.version>
<slf4j-api.version>1.7.25</slf4j-api.version>
<commons-lang3.version>3.4</commons-lang3.version>
<netty-all.version>4.0.35.Final</netty-all.version>
</properties>
<!-- 依賴管理 僅僅定義 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.java1234</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>${dubbo-demo-api.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator-framework.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-api.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${netty-all.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
1.什麼時候用到叢集
併發量大
1.當單臺伺服器扛不住業務請求量的時候;
2.當業務需要7*24小時不間斷服務的時候
dubbo的監控可以檢視叢集 權重 而且可以修改
dubbo叢集只需要在
<dubbo:protocol name="dubbo" port="20881"/>
<dubbo:protocol name="dubbo" port="20880"/>
多配置幾個就可以了