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、引入依赖

        
            org.springframework.boot
            spring-boot-starter-cache
        
        
        
            net.sf.ehcache
            ehcache
        

b、开启基于注解的缓存 @EnableCaching

java web开发中常用的协议的使用和java-web 常见的缓存技术_第1张图片

c、创建Ehcache的配置文件

文件名:ehcache.xml
位置:src/main/resources/ehcache.xml


    xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
    
    
    
    <defaultCache 
            maxElementsInMemory="10000" 
            eternal="false"
            timeToIdleSeconds="120" 
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000" 
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        
    
    
    
     
            maxElementsInMemory="10000" 
            eternal="false"
            timeToIdleSeconds="120" 
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000" 
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        
    

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
 

 

java web开发中常用的协议的使用和java-web 常见的缓存技术_第2张图片

MySql在高版本需要指明是否进行SSL连接,这里因为自己的数据库安装的是mysql8.0所以需要加上这个东西,如果用的是mysql5点几版本的朋友可以自行忽略…

SSL协议,当前版本为3.1(SSL3.1就是TLS1.0)。它已被广泛地用于Web浏览器与服务器之间的身份认证和加密数据传输.它位于TCP/IP协议与各种应用层协议之间,为数据通讯提供安全支持
java web开发中常用的协议的使用和java-web 常见的缓存技术_第3张图片

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));
    }
}

测试结果

java web开发中常用的协议的使用和java-web 常见的缓存技术_第4张图片

第二次测试‘’
这里我们将之前在 UserServiceImpl里面的 @Cacheable(value = “users”)取消注释
java web开发中常用的协议的使用和java-web 常见的缓存技术_第5张图片
再次启动项目测试
java web开发中常用的协议的使用和java-web 常见的缓存技术_第6张图片

第三次测试
修改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());

测试结果

java web开发中常用的协议的使用和java-web 常见的缓存技术_第7张图片

 


第四次测试(清除缓存:@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());
}

测试结果一:

java web开发中常用的协议的使用和java-web 常见的缓存技术_第8张图片

 

由于缓存机制的存在,所以数据库的元素改变了,但是我们用的却还是之前缓存的内容,很显然这不合适。如何解决呢,我们修改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);
}

再次测试,结果如下:

 java web开发中常用的协议的使用和java-web 常见的缓存技术_第9张图片

 

2.Memcache       :

3.Redis                :

转载于:https://www.cnblogs.com/h-c-g/p/11083834.html

你可能感兴趣的:(java web开发中常用的协议的使用和java-web 常见的缓存技术)