Java应用研发,其形式随着需求一种在变,其架构演变可简单理解为:
1. 单一应用架构
2. 应用和数据库单独部署
3. 应用和数据库集群部署
4. 数据库压力变大,读写分离
5. 缓存中间件加快速度
6. 数据库分库分表
7. 应用按不同类型拆分
Netty 是一个基于NIO的客户端、服务端的编程框架,使用Netty 可以确保你快速和简单的开发出一个网络应用,例如实现了某种协议的客户端、服务端应用。
RPC(Remote Procedure Call)远程过程调用,是一个计算机通信协议,简单的理解是一个节点请求另一个节点提供的服务。RPC 是一种技术思想而非一种规范或协议。很多RPC框架都是基于netty来实现。
Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring框架无缝集成。简单理解为Dubbo基于netty网络通信框架,采用RPC模式。
Spring Cloud 是微服务框架,直接使用HTTP协议,支持跨语言实现。
HTTP是在应用层中完成,整个通信的代价较高;RPC是直接基于TCP进行远程调用,数据传输在传输层TCP层完成,更适合对效率要求比较高的场景;RPC主要依赖于客户端和服务端之间建立Socket链接进行,底层实现比REST更复杂。
RPC 主要用于公司内部的服务调用,性能消耗低,传输效率高,实现复杂。典型实现Dubbo。
HTTP 主要用于对外的异构环境,浏览器接口调用,App 接口调用,第三方接口调用等。典型实现SpringCloud。
springboot+dubbo实现示例,共包含3个工程,interface、provider、consumer
interface工程为普通的springboot单体应用,只包含interface接口,代码如下:
public interface TestService {
public void ins();
public void del();
public void upd();
public void sel();
}
provider工程部分代码如下:
依赖
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>com.ldc</groupId>
<artifactId>springboot_dubbo_interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
properties配置
server.port=8087
#spring项目名
spring.application.name=dubbo_provider
#Dubbo provider configuration
dubbo.application.name=dubbo_provider
dubbo.registry.protocol=zookeeper
dubbo.registry.address=zookeeper://192.168.1.2:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
#扫描注解包通过该设置将服务注册到zookeeper
dubbo.scan.base-packages=com.*.service
代码
@DubboService(version = "1.0.0",interfaceClass = TestService.class)
public class TestServiceImpl implements TestService {
@Override
public void ins() {
System.out.println("insert");
}
@Override
public void del() {
System.out.println("delete");
}
@Override
public void upd() {
System.out.println("update");
}
@Override
public void sel() {
System.out.println("select");
}
}
consumer工程部分代码如下:
依赖
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>com.ldc</groupId>
<artifactId>springboot_dubbo_interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
properties配置
server.port=8088
#spring项目名
spring.application.name=dubbo_consumer
#dubbo configuration
dubbo.application.name=dubbo_consumer
dubbo.registry.protocol=zookeeper
dubbo.registry.address=zookeeper://192.168.1.2:2181
代码
@RestController
public class TestController {
@DubboReference(version = "1.0.0")
private TestService testServiceImpl;
@RequestMapping("/ins")
public String ins(){
testServiceImpl.ins();
return "ins";
}
@RequestMapping("/del")
public String del(){
testServiceImpl.del();
return "del";
}
@RequestMapping("/upd")
public String upd(){
testServiceImpl.upd();
return "upd";
}
@RequestMapping("/sel")
public String sel(){
testServiceImpl.sel();
return "sel";
}
}
测试结果正常返回。
或
springboot+dubbo示例,共包含3个工程,分别是springboo_dubbo_api开放的interface、springboot_dubbo_sms短信服务提供者、springboot_dubbo_order订单服务消费者。
springboo_dubbo_api部分核心代码:
public interface OrderService {
void create(String orderContent);
}
public interface SmsService {
Object send(String phone,String content);
}
springboot_dubbo_sms部分核心代码:
依赖
<dependency>
<groupId>com.ldc</groupId>
<artifactId>springboot_dubbo_api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 注意版本号,否则会版本冲突 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
properties
spring.application.name=sms
server.port=9001
dubbo.application.name=sms
dubbo.registry.address=redis://127.0.0.1:6379
dubbo.consumer.timeout=1000
dubbo.application.qos-enable=false
dubbo.protocol.name=dubbo
dubbo.protocol.port=20883
dubbo.scan.base-packages=com.*.sms
@DubboService
public class SmsServiceImpl implements SmsService {
public Object send(String phone,String content) {
System.out.println("发送短信:" + phone + ":" + content);
return "短信发送成功";
}
}
springboot_dubbo_order部分核心代码:
依赖
<dependency>
<groupId>com.ldc</groupId>
<artifactId>springboot_dubbo_api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 注意版本号,否则会版本冲突 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
properties
spring.application.name=order
server.port=9002
dubbo.application.name=order
dubbo.registry.address=redis://127.0.0.1:6379
dubbo.consumer.timeout=1000
dubbo.application.qos-enable=false
dubbo.scan.base-packages=com.*.order
@DubboService
public class OrderServiceImpl implements OrderService {
@DubboReference
private SmsService smsService;
public void create(String orderContent) {
System.out.println("订单创建成功:" + orderContent);
Object smsResult = smsService.send("10086","你的短信来了");
System.out.println("smsService调用结果:" + smsResult);
}
}
@RestController
public class OrderController {
@Autowired
private OrderService orderService;
@RequestMapping("/create")
public Object create() {
orderService.create("买了一部手机");
return "买了一部手机";
}
}
访问地址http://localhost:9002/create ,正确返回 买了一部手机
sms服务控制台打印,发送短信:10086:你的短信来了
order服务控制台打印,
订单创建成功:买了一部手机
smsService调用结果:短信发送成功
经验证,rpc服务调用ok。