1.在mac安装mysql
下载mysql
1 https://dev.mysql.com/downloads/mysql/
开启mysql服务
查看/usr/local/mysql/bin目录下是否有mysql
编辑~/.bash_profile
1 2 3 4 5 6 vi ~/.bash_profile export MYSQL_HOME=/usr/local /mysqlexport PATH=$MYSQL_HOME /bin:$PATH source ~/.bash_profile
设置密码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 sudo /usr/local/mysql/support-files/mysql.server stop sudo mysqld_safe mysql -uroot -p mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> create user 'matrix' @'localhost' identified by '123456' ; Query OK, 0 rows affected (0.00 sec) mysql> grant all privileges on *.* to matrix@'localhost'; Query OK, 0 rows affected (0.00 sec) mysql> UPDATE user SET authentication_string = PASSWORD('123456'), password_expired = 'N' WHERE User = 'matrix'; Query OK, 0 rows affected, 1 warning (0.00 sec) Rows matched: 2 Changed: 0 Warnings: 1 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> create database eshop; Query OK, 1 row affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
2.新建maven工程
项目结构
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <groupId > com.matrix</groupId > <artifactId > eshop-product-ha</artifactId > <version > 0.0.1-SNAPSHOT</version > <packaging > jar</packaging > <name > eshop-product-ha</name > <url > http://maven.apache.org</url > <parent > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-parent</artifactId > <version > 1.2.5.RELEASE</version > </parent > <properties > <project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > <java.version > 1.8</java.version > </properties > <dependencies > <dependency > <groupId > org.apache.httpcomponents</groupId > <artifactId > httpclient</artifactId > <version > 4.4</version > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-thymeleaf</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-jdbc</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-actuator</artifactId > </dependency > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis-spring</artifactId > <version > 1.2.2</version > </dependency > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis</artifactId > <version > 3.2.8</version > </dependency > <dependency > <groupId > org.apache.tomcat</groupId > <artifactId > tomcat-jdbc</artifactId > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > </dependency > <dependency > <groupId > com.alibaba</groupId > <artifactId > fastjson</artifactId > <version > 1.1.43</version > </dependency > </dependencies > <build > <plugins > <plugin > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-maven-plugin</artifactId > </plugin > </plugins > </build > <repositories > <repository > <id > spring-milestone</id > <url > https://repo.spring.io/libs-release</url > </repository > </repositories > <pluginRepositories > <pluginRepository > <id > spring-milestone</id > <url > https://repo.spring.io/libs-release</url > </pluginRepository > </pluginRepositories > </project >
HelloController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.matrix.eshop_product_ha.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controller public class HelloController { @RequestMapping ("/hello" ) @ResponseBody public String hello (String name) { return "hello, " + name; } }
ProductController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.matrix.eshop_product_ha.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controller public class ProductController { @RequestMapping ("/getProductInfo" ) @ResponseBody public String getProductInfo (Long productId) { return "{\"id\": " + productId + ", \"name\": \"iphone7手机\", \"price\": 5599, \"pictureList\":\"a.jpg,b.jpg\", \"specification\": \"iphone7的规格\", \"service\": \"iphone7的售后服务\", \"color\": \"红色,白色,黑色\", \"size\": \"5.5\", \"shopId\": 1, \"modifiedTime\": \"2017-01-01 12:00:00\"}" ; } }
UserMapper.java
1 2 3 4 5 package com.matrix.eshop_product_ha.mapper;public interface UserMapper {}
Application.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 package com.matrix.eshop_product_ha;import javax.sql.DataSource;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.transaction.PlatformTransactionManager;@EnableAutoConfiguration @SpringBootApplication @ComponentScan @MapperScan ("com.matrix.eshop_product_ha.mapper" )public class Application { @Bean @ConfigurationProperties (prefix="spring.datasource" ) public DataSource dataSource () { return new org.apache.tomcat.jdbc.pool.DataSource(); } @Bean public SqlSessionFactory sqlSessionFactoryBean () throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml" )); return sqlSessionFactoryBean.getObject(); } @Bean public PlatformTransactionManager transactionManager () { return new DataSourceTransactionManager(dataSource()); } public static void main (String[] args) { SpringApplication.run(Application.class, args); } }
Application.properties
1 2 3 4 5 server.port=8082 spring.datasource.url=jdbc:mysql://localhost:3306/eshop spring.datasource.username=eshop spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
UserMapper.xml
1 2 3 4 5 6 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace ="com.matrix.eshop_product_ha.mapper.UserMapper" > </mapper >
hello.html
在浏览器中输入http://localhost:8082/hello?name=matrix
3.新建名为eshop-cache-ha的Maven工程
Application.properties
1 2 3 4 5 server.port=8081 spring.datasource.url=jdbc:mysql://localhost:3306/eshop spring.datasource.username=eshop spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
CacheController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 package com.matrix.eshop_product_ha.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.matrix.eshop_product_ha.http.HttpClientUtils;@Controller public class CacheController { @RequestMapping ("/change/product" ) @ResponseBody public String changeProduct (Long productId) { String url = "http://127.0.0.1:8082/getProductInfo?productId=" + productId; String response = HttpClientUtils.sendGetRequest(url); System.out.println(response); return "success" ; } }
HttpClientUtils.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 package com.matrix.eshop_product_ha.http;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Map.Entry;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;@SuppressWarnings ("deprecation" )public class HttpClientUtils { @SuppressWarnings ("resource" ) public static String sendGetRequest (String url) { String httpResponse = null ; HttpClient httpclient = null ; InputStream is = null ; BufferedReader br = null ; try { httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null ) { is = entity.getContent(); br = new BufferedReader(new InputStreamReader(is)); StringBuffer buffer = new StringBuffer("" ); String line = null ; while ((line = br.readLine()) != null ) { buffer.append(line + "\n" ); } httpResponse = buffer.toString(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (br != null ) { br.close(); } if (is != null ) { is.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return httpResponse; } @SuppressWarnings ({ "rawtypes" , "unchecked" , "resource" }) public static String sendPostRequest (String url, Map<String,String> map) { HttpClient httpClient = null ; HttpPost httpPost = null ; String result = null ; try { httpClient = new DefaultHttpClient(); httpPost = new HttpPost(url); List<NameValuePair> list = new ArrayList<NameValuePair>(); Iterator iterator = map.entrySet().iterator(); while (iterator.hasNext()){ Entry<String,String> elem = (Entry<String, String>) iterator.next(); list.add(new BasicNameValuePair(elem.getKey(), elem.getValue())); } if (list.size() > 0 ){ UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "utf-8" ); httpPost.setEntity(entity); } HttpResponse response = httpClient.execute(httpPost); if (response != null ){ HttpEntity resEntity = response.getEntity(); if (resEntity != null ){ result = EntityUtils.toString(resEntity, "utf-8" ); } } } catch (Exception ex){ ex.printStackTrace(); } finally { } return result; } }
启动两个工程的Applicatin.java类
1 完成缓存服务接收数据变更消息以及调用商品服务接口
在浏览器上输入:http://127.0.0.1:8082/getProductInfo?productId=215646165