dubbo(开源分布式服务框架)-demo
程序员文章站
2022-05-07 14:23:43
...
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"/>
多配置几个就可以了
推荐阅读
-
Spring Boot Dubbo 构建分布式服务的方法
-
分享一个C++与Python开发的中小型通用游戏服务端框架(跨平台,开源,适合MMORPG游戏)
-
分布式服务框架之远程通讯技术及原理分析
-
【分享】Vue 资源典藏(UI组件、开发框架、服务端、辅助工具、应用实例、Demo示例)
-
kratos微服务框架学习笔记一(kratos-demo)
-
美团分布式服务通信框架及服务治理系统OCTO
-
springcloud 微服务分布式 框架源码 activiti工作流 前后分离
-
阿里分布式服务框架Dubbo的架构总结
-
(八)整合 Dubbo框架 ,实现RPC服务远程调用
-
Go开源游戏服务器框架——Pitaya