昆明做网站建设公司,手机app页面设计,成都做小程序的公司排名,网站的想法一Dubbo的简易介绍
1.Dubbo是什么#xff1f;
Dubbo是一个分布式服务框架#xff0c;致力于提供高性能和透明化的RPC远程服务调用方案#xff0c;以及SOA服务治理方案。
简单的说#xff0c;dubbo就是个服务框架#xff0c;如果没有分布式的需求#xff0c;其实是不需…一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)
统计服务的调用次数与时间等。
二.springbootdubbozookeeper整合使用
1.在Linux中安装zookeeper实现服务注册 进入到镜像中
[rootlocalhost ~]# docker exec -it 46c8d188683b bash查看zookeeper中的服务情况 查看已经注册存在的服务 2.服务层中service使用dubbo中的Service注解 3.Providor服务提供者 3.0.application.yml
dubbo:provider:application: dubbo-providerregistry:address: zookeeper://192.168.58.128:2181scan:base-packages:- com.tjetc.serviceprotocol:name: dubbo #使用dubbo协议port: 20880 #协议端口为20880
server:port: 8081
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://192.168.58.128:3306/springboot?serverTimezoneGMT%2B8username: rootpassword: root
mybatis:type-aliases-package: com.tjetc.domain
logging:level:com.tjetc.mapper: debug3.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
//开启dubbo服务注册
EnableDubbo
MapperScan(com.tjetc.mapper)
public class ProvidorApplication {public static void main(String[] args) {SpringApplication.run(ProvidorApplication.class, args);}}3.2.ProductServiceImplService使用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 {Resourceprivate ProductMapper productMapper;Overridepublic void add(Product product) {productMapper.add(product);}Overridepublic PageInfoProduct list(String name, Integer pageNum, Integer pageSize) {PageHelper.startPage(pageNum,pageSize);ListProduct listproductMapper.list(name);PageInfoProduct pageInfo new PageInfo(list);System.out.println(pageInfo.getList() pageInfo.getList());return pageInfo;}Overridepublic ListProduct listByName(String name) {return productMapper.listByName(name);}Overridepublic Product findById(Integer id) {return productMapper.findById(id);}Overridepublic void update(Product product) {productMapper.update(product);}Overridepublic 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;Referenceprivate 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 {//7. 显示上传的结果file_id 也就是文件的路径String path fastDfsClient.upload(photo.getBytes(), extName);System.out.println(path path);product.setPhotopath(fastdfspath);} 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);PageInfoProduct pageInfoproductService.list(name,pageNum,pageSize);ListProduct list pageInfo.getList();for (Product product : list) {System.out.println(product product);}/*ListProduct list productService.listByName(name);System.out.println(list list);*/model.addAttribute(page,pageInfo);model.addAttribute(list,list);model.addAttribute(name,name);return list;}RequestMapping(/findById)public String findById(Integer id,Model model){Product productproductService.findById(id);model.addAttribute(p,product);return update;}RequestMapping(/update)public String update(Product product,MultipartFile photo){if (photo!null photo.getSize()0){String fileNamephoto.getOriginalFilename();String extNamefileName.substring(fileName.lastIndexOf(.)1);FastDfsClient fastDfsClient new FastDfsClient(classpath:client.properties);try {String path fastDfsClient.upload(photo.getBytes(), extName);product.setPhotopath(fastdfspath);} catch (IOException e) {e.printStackTrace();}}productService.update(product);return redirect:/product/list;}RequestMapping(/del)public String del(Integer id){int iproductService.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;/*** FastDFS上传文件的工具类*/
public class FastDfsClient {//声明4个成员变量private TrackerClient trackerClientnull;private TrackerServer trackerServernull;// 4. 声明存储服务端storageprivate StorageServer storageServernull;private StorageClient1 storageClientnull;/** 根据配置文件初始化4个成员变量* */public FastDfsClient(String config) {Properties properties new Properties();if (config.contains(classpath:)) {try {configconfig.replaceAll(classpath:,);System.out.println(config config);// 使用ClassLoader加载properties配置文件生成对应的输入流InputStream in this.getClass().getClassLoader().getResourceAsStream(config);// 使用properties对象加载输入流properties.load(in);System.out.println(properties properties);// 1. 加载配置文件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();}}// 2. 创建tracker的客户端this.trackerClient new TrackerClient();try {// 3. 通过客户端得到服务端连接对象this.trackerServer trackerClient.getConnection();} catch (IOException e) {e.printStackTrace();}// 5. 获取存储服务器的storage客户端对象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);}/*** *根据文件的内容上传** paramcontent文件内容byte[]** paramextName文件扩展名* *parammetas参数*return*/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.printStackTrace();}return null;}public String upload(byte[] content,String extName){return this.upload(content, extName,null);}public static void main(String[] args) {// 外部普通类System.out.println(方法名 类名);System.out.println(getName FastDfsClient.class.getName());System.out.println(getCanonicalName FastDfsClient.class.getCanonicalName());System.out.println(getSimpleName FastDfsClient.class.getSimpleName());System.out.println();//用代码实现:得到类路径的真实路径String path FastDfsClient.class.getClassLoader().getResource().getPath();System.out.println(path path);}}
7.3.client.properties
fastdfs.tracker_servers192.168.58.128:22122
7.4.application.yml
fastdfs: http://192.168.58.128/
dubbo:application:name: dubbo-consumerregistry:address: zookeeper://192.168.58.128:2181protocol:name: dubbo #使用dubbo协议port: 20880 #协议端口为20880server:port: 8082
spring:servlet:multipart:# 设置 上传文件的大小max-file-size: 20MB# 设置 整个请求的大小max-request-size: 20MB7.5.list.html
!DOCTYPE html
html xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8titleTitle/titlestyle typetext/csstable{width: 700px;border-collapse: collapse;text-align: center;}th,td{border: 1px solid #CCCCCC;}/stylescript typetext/javascriptfunction fenye(pageNum) {alert(document.getElementById(pageSize).value)location.href/product/list?pageNumpageNumnamedocument.getElementById(name).value;}function findById(id) {location.href/product/findById?idid;}function del(id) {location.href/product/del?idid;}/script
/head
body
divinput typetext idname th:value${name} placeholder请输入商品名称button th:onclickfenye(1)查询/button
/div
tabletrth编号/thth名称/thth单价/thth时间/thth美照/thth操作/th/trtr th:eachp:${list}td th:text${p.id}/tdtd th:text${p.name}/tdtd th:text${p.price}/tdtd th:text${#dates.format(p.time)}/tdtdimg th:src${p.photopath} width100px/tdtdbutton th:onclick|findById(${p.id})|编辑/buttonbutton th:onclick|del(${p.id})|删除/button/td/trtrtd colspan10button th:onclick|fenye(1)|首页/buttonbutton th:onclick|fenye(${page.prePage})|上一页/button当前页span th:text${page.pageNum}/span/span th:text${page.pages}/spanbutton th:onclick|fenye(${page.nextPage})|下一页/buttonbutton th:onclick|fenye(${page.pages})|尾页/button每页select idpageSize th:onclickfenye(1)option value33/optionoption value66/optionoption value99/option/select条数据总记录数:span th:text${page.total}/span/td/tr
/table
/body
/html效果截图