系统架构演变
1. 单一应用框架(ORM)所有功能部署在一个应用上
2. 垂直应用框架(MVC) 将功能分到多个应用上,缺乏复用性
3. 分布式应用架构(RPC)功能作为服务抽离出来,形成服务中心
4. 流动计算架构(SOA)面向服务的体系结构
Dubbo简介
Dubbo:
一款基于RPC的分布式服务框架,架构包含
1. Provider:服务提供方
2. Consumer:服务使用方
3. Registry:服务注册中心
dubbo提供的:Multicast、ZooKeeper、Redis、Simple注册中心
4. Monitor:服务监控中心
Dubbo优点
1. 调用远程方法如同调用本地方法
2. 负载均衡及容错机制
3.服务注册中心自动注册
4.服务接口监控与治理
Dubbo使用Demo
服务端:
1. 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.rambo</groupId>
<artifactId>dubbotest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--引入springboot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入dubbot-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.7</version>
</dependency>
<!--引入zookeeper-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<!--引入zookeeper客户端-->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. DubboServiceProvider
package com.rambo.dubbotest.service;
public interface DubboServiceProvider {
//声明服务方法
public String sayHello(String name);
}
3. DubboServiceProvider实现
package com.rambo.dubbotest.service.impl;
import com.rambo.dubbotest.service.DubboServiceProvider;
@Component("dubboServiceProvider")
public class DubboServiceProviderImpl implements DubboServiceProvider {
public String sayHello(String name) {
// TODO Auto-generated method stub
return "Hello, " + name;
}
}
4. 启动类
package com.rambo.dubbotest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource("classpath:/dubbo.xml")
public class ProviderApplication {
public static void main(String[] args){
SpringApplication.run(ProviderApplication.class,args);
}
}
5. Dubbo.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="dubboproviderhello" />
<!--配置服务注册中心,dubbo不仅仅支持zookeeper-->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!--声明对外暴露的服务-->
<dubbo:service interface="com.rambo.service.DubboServiceProvider" ref="dubboServiceProvider" />
</beans>
客户端
1.pom.xml 除开aircraftID不一样
2.Service 包路径相同的情况下相同的服务接口
3.使用类
package client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.rambo.dubbotest.service.DubboServiceProvider;
@SpringBootApplication
@ImportResource("classpath:/dubbo.xml")
@RestController
public class ConsumerApplication {
//注入声明的服务
@Autowired
private DubboServiceProvider dubboServiceProvider;
public static void main(String[] args){
SpringApplication.run(ConsumerApplication.class,args);
}
//测试服务调用
@ResponseBody
@RequestMapping("/sayhello/{name}")
public String hello(@PathVariable String name){
return dubboServiceProvider.sayHello(name);
}
}
4.dubbo.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="dubboproviderhello" />
<!--配置服务注册中心,dubbo不仅仅支持zookeeper-->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!--声明服务引用,与服务声明接口类型一致-->
<dubbo:reference interface="com.rambo.dubbotest.service.DubboServiceProvider" id="dubboServiceProvider" />
</beans>
5. application.properties 修改客户端启动的端口
server.port=8081
演示:
1.启动zookeeper
2.以SpringBoot方式启动服务端主类
3.以SpringBoot方式启动客户端主类
5.http://localhost:8081/sayhello/Rambo
链接: