java web开发中常用的协议的使用和java-web 常见的缓冲技术
一、DNS协议
作用将域名解析为IP 类似于我们只需要知道中央一台,中央二台,而不需要知道它的频率,方便记忆。
java dns 域名解析协议实现
1 域名解析,将域名可转换为ip地址
InetAddress也可以通过使用getAddress()来获得IP地址,但是它的返回值是一个4个字节的数组。
因此尽管getAddress()在获得IP方面是有用的,但却不适于用来输出。
package dns;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
public class NsLookup { public static void main(String[] args){
InetAddress address=null;
try {
address = InetAddress.getByName(args[0]);
} catch (UnknownHostException e) {
e.printStackTrace();
}
System.out.println(args[0]+": "+address.getHostAddress()); //args[0]是执行程序时写的参数,
InetAddress localhost=null;
try {
localhost = InetAddress.getLocalHost(); //本地地址
} catch (UnknownHostException e) {
e.printStackTrace();
}
System.out.println("localhost:ip address "+localhost.getHostAddress());
System.out.println("localhost:主机名: "+localhost.getHostName());
/* * 在开始使用RS232端口通讯之前,我们想知道系统有哪些端口是可用的,以下代码列出系统中所有可用的RS232端口
*/
CommPortIdentifier portId;
Enumeration en = CommPortIdentifier.getPortIdentifiers();
while (en.hasMoreElements()) {
portId = (CommPortIdentifier) en.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(portId.getName());
}
}
}
}
执行时命令行参数如果输入:www.sina.com
执行结果如下:
www.sina.com: 121.194.0.209
localhost:ip address 59.64.158.214
localhost:主机名: bupt
COM1
COM2
还有一个域名可能对应不止一个ip地址,一下程序时列举出sina域名下的所有ip
package dns;
import java.net.InetAddress;
import java.net.UnknownHostException;
//有时一个域名会包含不止一个IP地址,比如微软的Web服务器,这是为了保持负载平衡。
//InetAddress提供了一种可以得到一个域名的所有IP地址的方法
public class NsLookup2 {
static public void main(String[] args) {
try {
String name = args[0];
InetAddress[] addresses = InetAddress.getAllByName(name);
for (int i = 0; i < addresses.length; i++) {
System.out.println(name + "[" + i + "]: "+ addresses[i].getHostAddress());
}
} catch (UnknownHostException uhe) {
System.err.println("Unable to find: " + args[0]);
} } }
执行结果: www.sina.com[0]: 121.194.0.208
www.sina.com[1]: 121.194.0.209
www.sina.com[2]: 121.194.0.210
www.sina.com[3]: 121.194.0.203
www.sina.com[4]: 121.194.0.205
www.sina.com[5]: 121.194.0.206
二、TCP/IP协议
IP 是用来查找地址,对应网际互连层,
IP 负责计算机之间的通信。
IP 负责在因特网上发送和接收数据包。
TCP用来规范传输规则的对应传输层。tcp/ip只是一套规则,并不能具体工作,就像程序中的接口,socket是这套协议的具体实现。
TCP 用于从应用程序到网络的数据传输控制。
TCP 负责在数据传送之前将它们分割为 IP 包,然后在它们到达的时候将它们重组。
三、HTTP协议
http是应用层的协议,tcp/ip接受到数据之后通过htp协议来解析。
HTTP 负责 web 服务器与 web 浏览器之间的通信。
HTTP 用于从 web 客户端(浏览器)向 web 服务器发送请求,并从 web 服务器向 web 客户端返回内容(网页)。
http协议中的报文结构很重要。
1:request message 首行,头部和主体 首行包括请求类型(get,head,post,put,delete),urL 和http版本
2:response 首行包括状态行,状态码(1XX, 2xx成功状态吗,3xx重定向状态吗,4xx:客户端状态吗,为请求到资源,5xx服务器端错误码,500内部错误),简短原因,http版本
四、servlet
是j2ee标准的一部分,是java web 开发的标准,
servlet 是对接收到的数据进行处理并生成结果。
五、SMTP - 简易邮件传输协议(Simple Mail Transfer Protocol)
SMTP 用于电子邮件的传输。
六、IMAP - 因特网消息访问协议(Internet Message Access Protocol)
IMAP 用于存储和取回电子邮件。
七、FTP - 文件传输协议(File Transfer Protocol)
FTP 负责计算机之间的文件传输。
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
开发中常使用缓存中间件:redis、memcahed、ehcache;
* 缓存:其实就是内存中的一块空间.可以使用缓存将数据源中的数据拿到,存入到内存中.后期获得数据的话 从缓存中进行获得.
* 常见欢送有以下几种
1.EHCache :是Hibernate常使用的二级缓存的插件.
参考https://elim.iteye.com/blog/2123030
a、引入依赖
<!-- Spring Boot 缓存支持启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Ehcache 坐标 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
b、开启基于注解的缓存 @EnableCaching
c、创建Ehcache的配置文件
文件名:ehcache.xml
位置:src/main/resources/ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <diskStore path="java.io.tmpdir" />
<!--defaultCache:echcache 的默认缓存策略 -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap" />
</defaultCache> <!-- 自定义缓存策略 -->
<cache name="users"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap" />
</cache>
</ehcache>
d、创建application.properties文件(mysql与springDateJpa相关配置)
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo01?useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.cache.ehcache.config=ehcache.xml
MySql在高版本需要指明是否进行SSL连接,这里因为自己的数据库安装的是mysql8.0所以需要加上这个东西,如果用的是mysql5点几版本的朋友可以自行忽略…
SSL协议,当前版本为3.1(SSL3.1就是TLS1.0)。它已被广泛地用于Web浏览器与服务器之间的身份认证和加密数据传输.它位于TCP/IP协议与各种应用层协议之间,为数据通讯提供安全支持
package com.test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.App;
import com.entity.Users;
import com.service.UsersService; /**
* UsersService测试
*
* @author Administrator
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = App.class)
public class UsersServiceTest {
@Autowired
private UsersService usersService; @Test
public void addUser() {
Users users=new Users();
users.setId(1);
users.setAddress("苏站路");
users.setAge(18);
users.setName("张三");
usersService.saveUsers(users);
System.out.println("执行完毕");
}
@Test
public void testFindUserById() {
//第一次查询
System.out.println(this.usersService.findUserById(1));
//第二次查询
System.out.println(this.usersService.findUserById(1));
}
}
测试结果
第二次测试‘’
这里我们将之前在 UserServiceImpl里面的 @Cacheable(value = “users”)取消注释
再次启动项目测试
第三次测试
修改UsersServiceImpl里面findUserByPage的方法指定为pageable.pageSize,默认的是pageable
修改测试类
新增方法testFindUserByPage
@Test
public void testFindUserByPage() {
Pageable pageable = new PageRequest(0, 2);
// 第一次查询
System.out.println(this.usersService.findUserByPage(pageable).getTotalElements()); // 第二次查询
System.out.println(this.usersService.findUserByPage(pageable).getTotalElements());
测试结果
第四次测试(清除缓存:@CacheEvict)
首先在UsersServiceTest中添加测试方法testFindAll,代码如下
@Test
public void testFindAll() {
//第一次查询
System.out.println(this.usersService.findUserAll().size());
//添加用户
Users users = new Users();
users.setId(4);
users.setAddress("苏站路");
users.setAge(18);
users.setName("张三4");
usersService.saveUsers(users);
//第二次查询
System.out.println(this.usersService.findUserAll().size());
}
测试结果一:
由于缓存机制的存在,所以数据库的元素改变了,但是我们用的却还是之前缓存的内容,很显然这不合适。如何解决呢,我们修改UsersServiceImpl,在saveUsers上面填上注解@CacheEvict(value=“users”,allEntries=true)
@Override
//@CacheEvict(value="users",allEntries=true) 清除缓存中以users缓存策略缓存的对象
@CacheEvict(value="users",allEntries=true)
public void saveUsers(Users users) {
this.usersRepository.save(users);
}
再次测试,结果如下:
2.Memcache :
3.Redis :
java web开发中常用的协议的使用和java-web 常见的缓冲技术的更多相关文章
- 依赖注入及AOP简述(十)——Web开发中常用Scope简介 .
1.2. Web开发中常用Scope简介 这里主要介绍基于Servlet的Web开发中常用的Scope. l 第一个比较常用的就是Application级Scope,通常我们会将一 ...
- Python Web开发中的WSGI协议简介
在Python Web开发中,我们一般使用Flask.Django等web框架来开发应用程序,生产环境中将应用部署到Apache.Nginx等web服务器时,还需要uWSGI或者Gunicorn.一个 ...
- WEB开发中常用的正则表达式
在计算机科学中,正则表达式用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串.在WEB开发中,正则表达式通常用来检测.查找替换某些符合规则的字符串,如检测用户输入E-mai格式是否正确,采集符 ...
- WEB开发中常用的正则表达式集合
在计算机科学中,正则表达式用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串.在WEB开发中,正则表达式通常用来检测.查找替换某些符合规则的字符串,如检测用户输入E-mai格式是否正确,采集符 ...
- Python Web开发中,WSGI协议的作用和实现原理详解
首先理解下面三个概念: WSGI:全称是Web Server Gateway Interface,WSGI不是服务器,python模块,框架,API或者任何软件,只是一种规范,描述web server ...
- web开发中常用的技术体系
HTML html(HyperText Markup Language)超文本标记语言"超文本"就是指页面内可以包含图片.链接.程序等非文字元素. 超文本标记语言的结构包括&q ...
- 【java】开发中常用字符串方法
java字符串的功能可以说非常强大, 它的每一种方法也都很有用. java字符串中常用的有两种字符串类, 分别是String类和StringBuffer类. Sting类 String类的对象是不可变 ...
- Web开发中常用的状态码
在HtttpServletResponse类中有关于状态码的描述. static int SC_ACCEPTED Status code (202) indicating that a request ...
- Web开发中常用的定位布局position
定位布局就是为开发提供了更好的布局方式,可以根据需求给相应的模块设定相应位置,从而使界面更佳丰富,代码更佳完美. position是CSS中非常重要的一个属性,通过position属性,我们可以让元素 ...
随机推荐
- Android Studio 1.5.1
Android Studio 1.5.1 December 3rd, 2015: For information on what's new in 1.5.1, see the release ann ...
- saltstack自动化运维快速入门
saltstack自动化运维快速入门 关于saltstack 这个软件是干啥的 我这里就不介绍了 只是简单的说下是干啥的 网上的说法是 它是func的强化版本+ puppet的精简版 关于puppet ...
- 【旧文章搬运】Windows句柄分配算法(一)
原文发表于百度空间,2009-04-04========================================================================== 分析了Wi ...
- Unity3D4.* NGUI制作动态字库
新建一个工程,这个工程必须没有中文路径,否则会不识别字体!!! 首先导入NGUI插件,这里我用的是NGUI 3.0.2版本的. 在Assets 下创建一个文件夹,用来存放接下来的工作文件 . 这里随便 ...
- 036--MySQL扩展
一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. SELECT * FROM ( S ...
- ORACLE知识点整理之一
1. 安装客户端 去官方网站下载 此处略 2. 客户端登陆身份 Oracle有三种身份登录方式:Normal.sysdba.sysoper. normal身份:普通用户身份,默认选项(默认可以不写), ...
- POJ2365【几何】
因为给出的点已经是顺时针了, 整个长度=相邻点距离+一个圆周长: C++ac代码-G++wa-因为标准不一样.G++用f //#include <bits/stdc++.h> #inclu ...
- NOIp 2015 Day1T3斗地主【搜索】
题目传送门 昨天真题测试赛题目== 没想到一道纯到都不用剪枝的搜索会是noipT3难度. 不过因为我搜索弱啊所以打不出来== LA:这不就是一道简单模拟题么 码完此题能增加对搜索的理解== (闲话结束 ...
- scikit-learning教程(四)选择合适的估计量
选择正确的估计 解决机器学习问题的最困难的部分通常是找到合适的工作量. 不同的估计器更适合于不同类型的数据和不同的问题. 下面的流程图旨在给用户一些关于如何处理关于哪些估计器尝试您的数据的问题的粗略指 ...
- B.华华教月月做数学
链接:https://ac.nowcoder.com/acm/contest/392/B 题意: 找到了心仪的小姐姐月月后,华华很高兴的和她聊着天.然而月月的作业很多,不能继续陪华华聊天了.华华为了尽 ...