memcached principle, deployment memcached, session sharing

Source: Internet
Author: User
Tags fpm memcached session id nginx server


############################################################################################
Memcache Server:

Issues with traditional Web architectures:
--Many Web applications save data to an RDBMS, where the application server reads the data and displays it in the browser
And as the amount of data increases, the concentration of access will lead to an increase in the burden of the RDBMS, deterioration of the database response, and Web site display delays and other important issues.

Data storage location Comparison:
Performance
-CPU cache > Memory > Disk > Database
Price
-CPU cache > Memory > Disk > Database

From the data storage location comparison, we can store the data in memory, considering the cost/performance ratio.

##################################################################################################### #3
Memcached High-performance distribution is the cache server:
--to centrally cache database query results and reduce database access times to improve dynamic web responsiveness.

memcached Working principle:
The client first accesses, obtains the data from the RDBMS to memcached, when the client accesses the second time, obtains the data display page in the direct village memcached.

Memory management mechanism:

Traditional memory allocation mechanism
Reclaim memory after using the allocated memory, which is prone to memory fragmentation and reduces the operating system's memory management efficiency

Slab allocation mechanism
It divides the allocated memory into a certain length of memory block (chunk) according to the predetermined size, and then divides the same size memory blocks into groups (chunk collections), which are not released and can be reused.
-f Specifies the growth factor to control the size of the chunk.

Memcached allocating space using the Least recently Used (LRU) mechanism
--delete "least recently used" records
--When Memcached's memory is low, it searches for records that have not been used recently and assigns them to new records.
-m parameter prohibit LRU function, memcached will return error when memory is exhausted, memcached-m-m 1024 is not recommended

#####################################################################################################

Install memcached:

#yum-y Install memcached
#rpm-qa memcached
#cat/etc/sysconfig/memcached//memcached Boot configuration file
#cat/usr/lib/systemd/system/memcached.service
#systemctl start memcached
#netstat-LNPT | grep:11211//memcache port number is 11211. Verify that the service is turned on

Test memcached: Use Telnet to access the MEMCACEHD server

#yum-y Install Telnet
#telnet 192.168.4.5 11211//192.168.4.5memcached server address

Add name 0 180 10//variable not present adds
Set name 0 180 10//Add or Replace variable
Replace name 0 180 10//replacement
Get name//Read variable
Append name 0 180 10//Append data to Variable
Delete name//Remove variable
Stats//View status
Flush_all//Clear All
Tip: 0 means no compression, 180 is the data cache time, and 10 is the number of bytes of data that needs to be stored.


##################################################################################################


Lnmp+memcached:

Install Nginx:
#yum-y install gcc gcc-c++ pcre-devel openssl-devel zlib-devel//GCC support C source code compilation, gcc-c++ support C + + source code compilation, Pcre-devel support regular expression, Openssl-devel supports encryption. Zlib-devel Library file
#useradd-S/sbin/nologin nginx
#tar Xzf nginx-1.8.0.tar.gz
#cd nginx-1.8.0
#./configure--user=nginx--group=nginx Prefix=/usr/local/nginx--with-http_ssl_module
#make && make Install
#ln-S/usr/local/nginx/sbin/nginx/usr/sbin/

Install MARIADB:
#yum-y install maraidb mariadb-server mariadb-devel

Install PHP:
#yum-y install php php-mysql php-pecl-memcached//php-pecl-memcached to add memcached extensions to PHP
#yum-y Localinstall php-fpm-5.4.16-36.el7_1.x86_64.rpm

To modify the Nginx configuration file:
#vim/usr/local/nginx/conf/nginx.conf
Location/{
root HTML;
Index index.php index.html index.htm;
}
Location ~ \.php$ {
root HTML;
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
# Fastcgi_param Script_filename $document _root$fastcgi_script_name;
Include fastcgi.conf;
}


Start the service:
#nginx-t//check for syntax errors
#nginx
#systemctl Start mariadb
#systemcrl Enable MARIADB
#systemctl Start php-fom
#systemctl Enable PHP-FPM
#netstat-LNPT | Grep:80
#netstat-LNPT | grep:3306
#netstat-lnmp | grep:9000

To create a PHP page Test memcached service:
# vim/usr/local/nginx/html/test.php
<?php
$memcache =new memcache; Create a Memcache object
$memcache->connect (' localhost ', 11211) or die (' could not connect!! ');
$memcache->set (' key ', ' test '); Defining variables
$get _values= $memcache->get (' key '); Get variable Value
echo $get _values;
?>

Client testing:

# Firefox http://192.168.4.5/test.php//show Test


###############################################################################################

Session Sharing:

Session
--Store on server side, save user name, password and other information

Cookies
--sent by the server to the client, saved in a file on the client, the contents of the save mainly include: SessionID, account name, expiration time, path and domain


###################################################################################################

Tomcat+session

Front-end nginx server: 192.168.4.1
TOMCAT1 Server: 192.168.4.2
TOMCAT2 Server: 192.168.4.3

Deploy the front-end Nginx Scheduler:

#vim/usr/local/nginx/conf/nginx.conf
Upstream Tomcatgrp {
Server 192.168.4.2:8080;
Server 192.168.4.3:8080;
}
server {
Listen 80;
Server_name=location;
Location/{
Proxy_pass http://tomcatgrp;
root HTML;
Index index.html index.htm;
}
}

#nginx

###################################################################################
Deploying Tomcat:
#rpm-qa | grep "Java"//query to install Java-1.8.0-openjdk;java-1.8.0-opjdk-headless
#tar Xzf apache-tomcat-8.0.30.tar.gz
#mv Apache-tomcat-8.0.30/usr/local/tomcat
#/usr/local/tomcat/bin/startup.sh
#netstat-LNPT | grep:8080

######################################################################################
Create a JSP test page that asks the page to display the session ID information:
#vim/usr/local/tomcat/webapps/root/test.jsp
<body bgcolor= "Red" >//Specify page background color
<center>
<%string s = Session.getid ();%>//Get SessionID
<%=s%>
</center>
</body>


Client Access:
#firefox http://192.168.4.1/test.jsp//Display the SessionID of the two pages has been changing.

######################################################################################
Tomcat implements session sharing:
Deploying MSM on Tomcat
1. Copy the MSM-related jar package
# CP lnmp--soft/session/*.jar/usr/local/tomcat/lib/
2. Modify Tomcat so that it can connect to the Memcache server
# Vim/usr/local/tomcat/conf/context.xml
<Context> Add the following instructions to the original Context
<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"
Memcachednodes= "mem1:192.168.4.1:11211"//memcacehd server address
Requesturiignorepattern= ". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "
transcoderfactoryclass= "De.javakaffee.web.msm.JavaSerializationTranscoderFactory"/>
</Context>
3. Restart Tomcat
#/usr/local/tomcat/bin/shutdown.sh
# NETSTAT-TLNP | grep:8080
#/usr/local/tomcat/bin/startup.sh
4. Reboot Tomcat failed
See if Port 8005 is open first
# NETSTAT-TLNP | grep:8005
If not open
#find/-name "*java.security*"//Query file location
# vim/usr/lib/jvm/jre/lib/security/java.security
Securerandom.source=file:/dev/urandom

Client:
#firefox http://192.168.4.1/test.jsp//Display the SessionID of the two pages unchanged.



memcached principle, deployment memcached, session sharing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.