一Dubbo的简易介绍
1.Dubbo是什么?
Dubbo是一个分布式服务框架 ,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。
简单的说,dubbo就是个服务框架,如果没有分布式的需求 ,其实是不需要用的,只有在分布式的时候,才有dubbo这样的分布式服务框架的需求,并且本质上是个服务调用的东西,说白了就是个远程服务调用的分布式框架(告别Web Service模式中的WSdl,以服务者与消费者的方式在dubbo上注册)。
其核心部分包含:
1. 远程通讯:
提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
2. 集群容错:
提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
3. 自动发现
基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。
2.Dubbo能做什么?
1.透明化的远程方法调用
就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。
2.软负载均衡及容错机制
可在内网替代F5等硬件负载均衡器,降低成本,减少单点。 3. 服务自动注册与发现
不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。
Dubbo采用全spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。
3.Dubbo核心组件
1)注册中心(registry)
生产者在此注册并发布内容,消费者在此订阅并接收发布的内容。
2)消费者(consumer)
客户端,从注册中心获取到方法,可以调用生产者中的方法。
3)生产者(provider)
服务端,生产内容,生产前需要依赖容器(先启动容器)。
4)容器(container)
生产者在启动执行的时候,必须依赖容器才能正常启动(默认依赖的是spring容器),
5)监控(Monitor)
统计服务的调用次数与时间等。
二.springboot+dubbo+zookeeper整合使用
1.在Linux中安装zookeeper实现服务注册
进入到镜像中
[ root@localhost ~ ] # docker exec - it 46 c8d188683b bash
查看zookeeper中的,服务情况 查看已经注册存在的服务
2.服务层中service,使用dubbo中的@Service注解
3.Providor服务提供者
3.0.application.yml
dubbo:
provider:
application: dubbo- provider
registry:
address: zookeeper: / / 192.168 .58 .128 : 2181
scan:
base- packages:
- com. tjetc. service
protocol:
name: dubbo #使用dubbo协议
port: 20880 #协议端口为20880
server:
port: 8081
spring:
datasource:
driver- class - name: com. mysql. cj. jdbc. Driver
url: jdbc: mysql: / / 192.168 .58 .128 : 3306 / springboot? serverTimezone= GMT% 2 B8
username: root
password: root
mybatis:
type- aliases- package: com. tjetc. domain
logging:
level:
com. tjetc. mapper: debug
3.1.ProvidorApplication
import com. alibaba. dubbo. config. spring. context. annotation. EnableDubbo;
import org. mybatis. spring. annotation. MapperScan;
import org. springframework. boot. SpringApplication;
import org. springframework. boot. autoconfigure. SpringBootApplication;
@SpringBootApplication
@EnableDubbo
@MapperScan ( "com.tjetc.mapper" )
public class ProvidorApplication {
public static void main ( String [ ] args) {
SpringApplication . run ( ProvidorApplication . class , args) ;
}
}
3.2.ProductServiceImpl,@Service使用dubbo中的注解
import com. alibaba. dubbo. config. annotation. Service;
import com. github. pagehelper. PageHelper;
import com. github. pagehelper. PageInfo;
import com. tjetc. domain. Product;
import com. tjetc. mapper. ProductMapper;
import com. tjetc. service. ProductService;
import org. springframework. stereotype. Component;
import javax. annotation. Resource;
import java. util. List;
@Service
public class ProductServiceImpl implements ProductService {
@Resource
private ProductMapper productMapper;
@Override
public void add ( Product product) {
productMapper. add ( product) ;
}
@Override
public PageInfo < Product > list ( String name, Integer pageNum, Integer pageSize) {
PageHelper . startPage ( pageNum, pageSize) ;
List < Product > list= productMapper. list ( name) ;
PageInfo < Product > pageInfo = new PageInfo < > ( list) ;
System . out. println ( "pageInfo.getList() = " + pageInfo. getList ( ) ) ;
return pageInfo;
}
@Override
public List < Product > listByName ( String name) {
return productMapper. listByName ( name) ;
}
@Override
public Product findById ( Integer id) {
return productMapper. findById ( id) ;
}
@Override
public void update ( Product product) {
productMapper. update ( product) ;
}
@Override
public int del ( Integer id) {
return productMapper. del ( id) ;
}
}
4.mapper模块,代码省略
5.实体类对象模块domain,代码省略
6.Service服务接口,代码省略
7.consumer,服务消费者
7.0.ComsumerApplication
7.1.ProductController
import com. alibaba. dubbo. config. annotation. Reference;
import com. github. pagehelper. PageInfo;
import com. tjetc. domain. Product;
import com. tjetc. service. ProductService;
import com. tjetc. utils. FastDfsClient;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. beans. factory. annotation. Value;
import org. springframework. stereotype. Controller;
import org. springframework. ui. Model;
import org. springframework. web. bind. annotation. GetMapping;
import org. springframework. web. bind. annotation. PostMapping;
import org. springframework. web. bind. annotation. RequestMapping;
import org. springframework. web. bind. annotation. RequestParam;
import org. springframework. web. multipart. MultipartFile;
import java. io. IOException;
import java. util. List;
@Controller
@RequestMapping ( "/product" )
public class ProductController {
@Value ( " ${ fastdfs } " )
private String fastdfs;
@Reference
private ProductService productService;
@GetMapping ( "/add" )
public String add ( ) {
return "add" ;
}
@PostMapping ( "/add" )
public String add ( Product product, MultipartFile photo) {
System . out. println ( "photo = " + photo) ;
System . out. println ( "product = " + product) ;
if ( photo!= null && photo. getSize ( ) > 0 ) {
String filename = photo. getOriginalFilename ( ) ;
String extName = filename. substring ( filename. lastIndexOf ( "." ) + 1 ) ;
FastDfsClient fastDfsClient = new FastDfsClient ( "classpath:client.properties" ) ;
try {
String path = fastDfsClient. upload ( photo. getBytes ( ) , extName) ;
System . out. println ( "path = " + path) ;
product. setPhotopath ( fastdfs+ path) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
System . out. println ( "product = " + product) ;
productService. add ( product) ;
return "redirect:/product/list" ;
}
@RequestMapping ( "/list" )
public String list ( @RequestParam ( defaultValue = "" ) String name,
@RequestParam ( defaultValue = "1" ) Integer pageNum,
@RequestParam ( defaultValue = "3" ) Integer pageSize, Model model) {
System . out. println ( productService) ;
PageInfo < Product > pageInfo= productService. list ( name, pageNum, pageSize) ;
List < Product > list = pageInfo. getList ( ) ;
for ( Product product : list) {
System . out. println ( "product = " + product) ;
}
model. addAttribute ( "page" , pageInfo) ;
model. addAttribute ( "list" , list) ;
model. addAttribute ( "name" , name) ;
return "list" ;
}
@RequestMapping ( "/findById" )
public String findById ( Integer id, Model model) {
Product product= productService. findById ( id) ;
model. addAttribute ( "p" , product) ;
return "update" ;
}
@RequestMapping ( "/update" )
public String update ( Product product, MultipartFile photo) {
if ( photo!= null && photo. getSize ( ) > 0 ) {
String fileName= photo. getOriginalFilename ( ) ;
String extName= fileName. substring ( fileName. lastIndexOf ( "." ) + 1 ) ;
FastDfsClient fastDfsClient = new FastDfsClient ( "classpath:client.properties" ) ;
try {
String path = fastDfsClient. upload ( photo. getBytes ( ) , extName) ;
product. setPhotopath ( fastdfs+ path) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
productService. update ( product) ;
return "redirect:/product/list" ;
}
@RequestMapping ( "/del" )
public String del ( Integer id) {
int i= productService. del ( id) ;
return "redirect:/product/list" ;
}
}
7.2.FastDfsClient集合文件系统工具类
import org. csource. common. MyException;
import org. csource. common. NameValuePair;
import org. csource. fastdfs. * ;
import java. io. IOException;
import java. io. InputStream;
import java. util. Properties;
public class FastDfsClient {
private TrackerClient trackerClient= null ;
private TrackerServer trackerServer= null ;
private StorageServer storageServer= null ;
private StorageClient1 storageClient= null ;
public FastDfsClient ( String config) {
Properties properties = new Properties ( ) ;
if ( config. contains ( "classpath:" ) ) {
try {
config= config. replaceAll ( "classpath:" , "" ) ;
System . out. println ( "config = " + config) ;
InputStream in = this . getClass ( ) . getClassLoader ( ) . getResourceAsStream ( config) ;
properties. load ( in ) ;
System . out. println ( "properties = " + properties) ;
ClientGlobal . initByProperties ( properties) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} catch ( MyException e) {
e. printStackTrace ( ) ;
}
} else {
try {
ClientGlobal . initByProperties ( config) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} catch ( MyException e) {
e. printStackTrace ( ) ;
}
}
this . trackerClient = new TrackerClient ( ) ;
try {
this . trackerServer = trackerClient. getConnection ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
this . storageClient = new StorageClient1 ( trackerServer, storageServer) ;
}
public FastDfsClient ( ) {
}
public String upload ( String filename, String extName, NameValuePair [ ] metas) {
try {
return storageClient. upload_file1 ( filename, extName, metas) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} catch ( MyException e) {
e. printStackTrace ( ) ;
}
return null ;
}
public String upload ( String filename, String exName) {
return this . upload ( filename, exName, null ) ;
}
public String upload ( byte[ ] content, String extNAme, NameValuePair [ ] metas) {
try {
return storageClient. upload_file1 ( content, extNAme, metas) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} catch ( MyException e) {
e