首页 文章 精选 留言 我的

精选列表

搜索[分布式锁],共10000篇文章
优秀的个人博客,低调大师

分布式系统学习共性总结:

1.归纳法: 对标与参考 适配与裁剪 2..术法道,深入源码体系 先分析该系统是数据存储还是计算系统。 如果是数据存储系统,从数据分布和副本策略开始入手 哈希:ElasticSearch 范围:HBase 数据量:HDFS 一致性:Cassan 副本读取 副本更新 副本切换 如果是数据处理问题,从数据投递策略入手。 离线 实时 微批 at most once at least once exactly once 2 .读对应系统架构图,对应着常用的架构模型,每个组件和已有的系统进行类比,想一下这个组件类似于hdfs的namenode等等,最后在脑海里梳理下数据流的整个流程。 3.在了解了系统的大概,着重看下文档中fault tolerence章节,看系统如何容错,或者自己可以预先问些问题,比如如果一个节点挂了、一个任务挂了系统是如何处理这些异常的,带着问题看文档。 4.文档详细读了一遍,就可以按照官方文档写些hello world的例子了,详细查看下系统配置项,随着工作的深入就可以看些系统的细节和关键源码了。 转自:https://blog.csdn.net/tg229dvt5i93mxaq5a6u/article/details/78266424

优秀的个人博客,低调大师

分布式--Spring Boot 微服务框架

1. Spring Boot 1). 简介 Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。 2). 好处 (1)它是Spring的升级版,Spring容器能做到的事情,它都能做到,而且更简便,从配置形式上来说,SpringBoot完全抛弃了繁琐的XML文件配置方式,而是替代性地用注解方式来实现,虽然本质来说,是差不多的(类似包扫描,注解扫描,类加载之类)。 (2)SpringBoot集成的插件更多,从而使用很多服务,都只是引入一个依赖,几个注解和Java类就可以用了,具体的参考相关手册。 (3)在Web应用开发这一块,之前的应用一般来说是打包成war包,再发布到相关服务器容器下(例如Tomcat),虽然SpringBoot也可以这么做,但在SpringBoot下更常见的形式是将SpringBoot应用打包成可执行jar包文件。之所以这么做,源于你可以直接将SpringBoot应用看成是一个Java Application,其Web应用可以没有webapp目录(更不用说web.xml了),它推荐使用html页面,并将其作为静态资源使用。 3). Spring Boot是什么 Spring Boot不是一个框架,它是一种用来轻松创建具有最小或零配置的独立应用程序的方式。这是方法用来开发基于Spring的应用,但只需非常少的配置。它提供了默认的代码和注释配置,快速启动新的Spring项目而不需要太多时间。它利用现有的Spring项目以及第三方项目来开发生产就绪(投入生产)的应用程序。它提供了一组pom或gradle构建文件,可以使用它们添加所需的依赖项,并且还便于自动配置。 Spring Boot根据其类路径上的库自动配置所需的类。假设应用程序想要与数据库交互,如果在类路径上有Spring数据库,那么它会自动建立与数据源类的连接。 图1.png 4). 主要目标 为所有Spring开发提供一个基本的,更快,更广泛的入门体验。 开箱即用,但随着需求开始偏离默认值,快速启动。 提供大型项目(例如嵌入式服务器,安全性,度量,运行状况检查,外部化配置)常见的一系列非功能特性。 绝对没有代码生成以及不需要XML配置,完全避免XML配置。 为了避免定义更多的注释配置(它将一些现有的 Spring Framework 注释组合成一个简单的单一注释) 避免编写大量import语句。 提供一些默认值,以便在短时间内快速启动新项目。 5). 新项目使用Spring Boot 简化基于Java的应用程序开发,单元测试和集成测试过程。 通过提供一些默认值来减少开发,单元测试和集成测试时间。 提高生产力。 当使用默认值时,Spring Boot有自己的看法。如果不指定详细信息,它将使用其自己的默认配置。如果想要持久化,但是没有在POM文件中指定任何东西,那么Spring Boot会- 将Hibernate带有HSQLDB数据库的配置作为JPA提供者。 为大型项目(例如嵌入式服务器,安全性,度量,健康检查,外部化配置)提供许多非常常见的非功能特性/解决方案。 6). 优缺点 Spring Boot的优点: 使用Java或Groovy开发基于Spring的应用程序非常容易。 它减少了大量的开发时间并提高了生产力。 它避免了编写大量的样板代码,注释和XML配置。 Spring Boot应用程序与其Spring生态系统(如Spring JDBC,Spring ORM,Spring Data,Spring Security等)集成非常容易。 它遵循“自用默认配置”方法,以减少开发工作量。 它提供嵌入式HTTP服务器,如Tomcat,Jetty等,以开发和测试Web应用程序非常容易。 它提供CLI(命令行界面)工具从命令提示符,非常容易和快速地开发和测试Spring Boot(Java或Groovy)应用程序。 它提供了许多插件来开发和测试Spring启动应用程序非常容易使用构建工具,如Maven和Gradle。 它提供了许多插件,以便与嵌入式和内存数据库工作非常容易。 Spring Boot的限制: 将现有或传统的Spring Framework项目转换为Spring Boot应用程序是一个非常困难和耗时的过程。它仅适用于全新Spring项目。 2. Spring Boot 1). 入门 Spring Boot项目归根只是一个常规的Spring项目,只是利用了Spring Boot启动程序和自动配置。要创建Spring Boot应用程序的方法,Spring团队(The Pivotal Team)提供了以下三种方法。 使用Spring Boot CLI工具 使用Spring STS IDE 使用Spring Initializr 可以使用Spring Boot开发两种基于Spring的应用程序: 基于Java的应用程序 基于Groovy的应用程序 图2.png 2). Spring Boot CLI I. 下载并解压 图3.png 在Windows系统中命令提示符中设置Spring Boot CLI环境变量: 右键“我的电脑”->”高级系统设置”->”高级”->”环境变量”->选择”PATH”并新建并加上面的路径 D:\distribution\spring-2.0.2.RELEASE\bin; 图4.png 验证: spring --version 图5.png II. spring 命令语法查看spring --help 图6.png III. 创建HelloWorld.groovy文件 @RestController class HelloWorld { @RequestMapping("/") String hello() { "Hello mazaiting." } } 图6.png IV. 运行 spring run HelloWorld.groovy 图7.png V. 测试 在浏览器输入http://localhost:8080/即可访问 图8.png 3). Maven 安装Spring Boot <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> 4). Eclipse 使用 Spring Boot I. 在pom.xml文件中添加SpringBoot的依赖 <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.mazaiting</groupId> <artifactId>SpringBootTest</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringBootTest</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- Spring-Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <!-- Spring-Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project> II. 创建HelloWorld类 @RestController @EnableAutoConfiguration public class HelloWorld { @RequestMapping("/") String home() { return "Hello mazaiting"; } public static void main(String[] args) { SpringApplication.run(HelloWorld.class, args); } } III. 执行结果 图9.png 在浏览器输入 http://localhost:8080/ 图10.png IV. 注解解释 @RestController: 构造型注释, 指定控制器,Spring会在处理传入的Web请求时考虑它. @RequestMapping:注解提供了“路由”信息。它告诉Spring任何带有路径“/”的HTTP请求应该映射到home方法。[@RestController](https://github.com/RestController "@RestController")注解告诉Spring将生成的字符串直接返回给调用者。 @EnableAutoConfiguration: 允许自动配置 V. 创建可执行的jar包 在pom.xml的project节点下添加 <build> <plugins> <!-- 用于创建可执行jar --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> 在项目的根目录下执行dos命令mvn package 图11.png 图12.png 执行完成后\SpringBootTest\target\目录下生成SpringBootTest-0.0.1-SNAPSHOT.jar文件 图13.png 执行java -jar target/SpringBootTest-0.0.1-SNAPSHOT.jar命令运行,并在浏览器输入http://localhost:8080/,同样可看到执行效果. 5). Spring Boot JSP 实例 I. 创建Maven Web项目 图14.png II. 在pom文件中添加依赖 <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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mazaiting</groupId> <artifactId>SpringBoot-JSP</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringBoot-JSP Maven Webapp</name> <url>http://maven.apache.org</url> <!-- Spring-Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <!-- 配置Java版本 --> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <!-- Spring-Boot: This is a web application --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Tomcat embedded container--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!-- Need this to compile JSP --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!-- JSTL for JSP --> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/taglibs/standard --> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!-- Optional, test for static content, bootstrap CSS--> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>3.3.7</version> </dependency> </dependencies> <build> <finalName>SpringBoot-JSP</finalName> <plugins> <!-- Package as an executable jar/war --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 实现依赖关系:mvn dependency:tree 图15.png III. SpringBootServletInitializer执行传统的WAR部署运行SpringApplication // 注解为SpringBoot应用 @SpringBootApplication public class SpringBootWebApplication extends SpringBootServletInitializer{ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(SpringBootWebApplication.class); } public static void main(String[] args) { SpringApplication.run(SpringBootWebApplication.class, args); } } IV. 创建控制器 @Controller public class WelcomeController { // 注入应用配置 application.properties @Value("${welcome.message}") private String message = "Hello World"; @RequestMapping("/") public String welcome(Map<String, Object> model) { model.put("message", this.message); return "welcome"; } } V. welcome.jsp页面 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <!-- 链接bootstrap --> <!-- href中的内容写为:webjars/bootstrap/3.3.7/css/bootstrap.css 加载地址为:http://localhost:8080/SpringBoot-JSP/webjars/bootstrap/3.3.7/css/bootstrap.css href中的内容写为:/webjars/bootstrap/3.3.7/css/bootstrap.css 加载地址为:http://localhost:8080/webjars/bootstrap/3.3.7/css/bootstrap.css 使用中建议写为webjars/bootstrap/3.3.7/css/bootstrap.css --> <link rel="stylesheet" type="text/css" href="webjars/bootstrap/3.3.7/css/bootstrap.css"/> <%-- <spring:url value="/css/main.css" var="springCss"/> <link href="${springCss}" rel="stylesheet"/> --%> <script type="text/javascript" src="webjars/jquery/1.11.1/jquery.min.js"></script> <c:url value="/css/main.css" var="jstlCss"></c:url> <link href="${jstlCss }" rel="stylesheet"/> <title>SpringBootTest</title> </head> <body> <nav class="navbar navbar-inverse"> <div class="container"> <div class="navbar-header"> <a class="navbar-brand" href="#">Spring Boot</a> </div> </div> <div id="navbar" class="collapse navbar-collapse"> <ul class="nav nav-bar-nav"> <li class="active"><a href="#">Home</a></li> <li><a href="#about">About</a></li> </ul> </div> </nav> <div class="container"> <div class="starter-template"> <h1>Spring Boot Web JSP Example</h1> <h2>Message: ${message }</h2> </div> </div> <script type="text/javascript" src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script> </body> </html> 注 href中的内容写为:webjars/bootstrap/3.3.7/css/bootstrap.css 加载地址为:http://localhost:8080/SpringBoot-JSP/webjars/bootstrap/3.3.7/css/bootstrap.css href中的内容写为:/webjars/bootstrap/3.3.7/css/bootstrap.css 加载地址为:http://localhost:8080/webjars/bootstrap/3.3.7/css/bootstrap.css 使用中建议写为webjars/bootstrap/3.3.7/css/bootstrap.css main.css h1 { color: #00F; } h2 { color: #F00; } VI. 在/src/main/resources/目录下创建application.properties及application-dev.properties、application-prod.properties、application-test.properties。 application-dev.properties:用于开发环境 application-test.properties:用于测试环境 application-prod.properties:用于生产环境 在application.properties文件中配置使用哪一类型的配置文件, prefix和suffix配置视图层的前后缀。 spring.mvc.view.prefix: /jsp/ spring.mvc.view.suffix: .jsp spring.profiles.active= dev application-dev.properties welcome.message: Hello mazaiting1 application-test.properties welcome.message: Hello mazaiting2 application-prod.properties welcome.message: Hello mazaiting3 VII. 代码结构 图16.png VIII. 运行测试一:在SpringBootWebApplication类上右键Run As -> Java Application, 在浏览器中输入http://localhost:8080/ 图17.png IX.. 运行测试二:在项目的根目录下,在命令提示符输入mvn spring-boot:run,然后在浏览器输入http://localhost:8080/也可呈现效果 图18.png X. 运行测试三:在工程右键-> Run As -> Run on Server部署在Tomcat容器中, 然后再浏览器中输入http://localhost:8080/SpringBoot-JSP/即可访问 图19.png XI. JSP限制 不能创建可执行jar来运行这个嵌入式Tomcat + JSP Web示例,因为Tomcat中有一个硬编码文件模式。 6). Spring Boot创建war包,并部署在Tomcat I. 使用命令提示符进入项目根目录 mvn clean package 图20.png II. 执行成功后,项目的\SpringBoot-JSP\target\目录下出现SpringBoot-JSP.war 图21.png 7). Spring Boot + Thymeleaf 模板引擎 -- 可执行JAR文件 I. 引入依赖 <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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mazaiting</groupId> <artifactId>SpringBoot-Thymeleaf</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringBoot-Thymeleaf Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent> <!-- Java版本 --> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- hot swapping, disable cache for template, enable live reload --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <!-- Optional, for bootstrap --> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>3.3.7</version> </dependency> </dependencies> <build> <finalName>SpringBoot-Thymeleaf</finalName> <plugins> <!-- Package as an executable jar/war --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> spring-boot-devtools有助于禁用缓存并启用热插拔,以便开发人员总是看到最后的更改。尝试修改Thymeleaf模板或属性文件,刷新浏览器以查看更改立即生效。 II. 创建SpringBootWebApplication @SpringBootApplication public class SpringBootWebApplication { public static void main(String[] args) { SpringApplication.run(SpringBootWebApplication.class, args); } } III. 创建WelcomeController @Controller public class WelcomeController { @Value("${welcome.message}") private String message= "Hello World"; @RequestMapping("/") public String welcome(Map<String, Object> model){ model.put("message", this.message); return "welcome"; } } IV. Thymeleaf +资源+静态文件 welcome.html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>Spring Boot Thymeleaf Hello World 示例</title> <link rel="stylesheet" type="text/css" href="webjars/bootstrap/3.3.7/css/bootstrap.css"/> <link rel="stylesheet" th:href="@{/css/main.css}" href="../../css/main.css"/> <script type="text/javascript" src="webjars/jquery/1.11.1/jquery.min.js"></script> </head> <body> <nav class="navbar navbar-inverse"> <div class="container"> <div class="navbar-header"> <a class="navbar-brand" href="#">Spring Boot</a> </div> <div id="navbar" class="collapse navbar-collapse"> <ul class="nav navbar-nav"> <li class="avtive"><a href="#">首页</a></li> <li><a href="#about">关于</a></li> </ul> </div> </div> </nav> <div class="container"> <div class="starter-template"> <h1>Spring Boot Web Thymeleaf 示例</h1> <h2> <span th:text="'Message: ' + ${message}"></span> </h2> </div> </div> <script type="text/javascript" src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script> </body> </html> main.css h1 { font-size: 20pt; } h2 { font-size: 16pt; } application.properties welcome.message: Hello, mazaiting 目录结构 图22.png V. 运行方式一:运行SpringBootWebApplication,浏览器输入http://localhost:8080/ 图23.png VI. 运行方式二:在命令提示符的项目根目录下执行mvn spring-boot:run,浏览器输入http://localhost:8080/即可显示效果 VII. 运行方式三:在命令提示符的项目根目录下执行mvn clean package,创建可执行jar包,运行jar包,浏览器输入http://localhost:8080/即可显示效果。想要生成jar包,在pom.xml文件中,一定有此行<packaging>war</packaging> 8). Spring Boot 非Web应用--命令行应用 I. 创建Maven的普通Java工程,并在pom.xml文件中添加依赖 <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.mazaiting</groupId> <artifactId>SpringBoot-Console</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringBoot-Console</name> <url>http://maven.apache.org</url> <!-- Spring-Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> <build> <plugins> <!-- Package as an executable jar/war --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> II. 创建HelloService服务 @Service public class HelloMessageService { @Value("${name}") private String name; public String getMessage() { return getMessage(name); } public String getMessage(String name) { return "Hello " + name; } } III. application.properties内容 name=mazaiting IV. SpringBootConsoleApplication应用 @SpringBootApplication public class SpringBootConsoleApplication implements CommandLineRunner{ @Autowired private HelloMessageService helloService; public static void main(String[] args) { SpringApplication application = new SpringApplication(SpringBootConsoleApplication.class); application.setBannerMode(Banner.Mode.OFF); application.run(args); } @Override public void run(String... args) throws Exception { if (args.length > 0) { System.out.println(helloService.getMessage(args[0].toString())); } else { System.out.println(helloService.getMessage()); } } } V. 目录结构 图24.png VI. 运行 生成jar包 mvn package 运行1 java -jar target/SpringBoot-Console-0.0.1-SNAPSHOT.jar 结果:Hello mazaiting 运行2 java -jar target/SpringBoot-Console-0.0.1-SNAPSHOT.jar spring-boot 结果:Hello spring-boot 9). Spring Boot 属性配置 I. pom.xml文件 <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.mazaiting</groupId> <artifactId>SpringBoot-Config</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringBoot-Config</name> <url>http://maven.apache.org</url> <!-- Spring-Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- Spring-Boot: This is a web application --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <!-- Package as an executable jar/war --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> II. @Value使用 @Component @PropertySource("classpath:global.properties") public class GlobalProperties { @Value("${thread-pool}") private int threadPool; @Value("${email}") private String email; //getters and setters } III. ConfigurationProperties @Component @PropertySource("classpath:global.properties") @ConfigurationProperties public class GlobalProperties { private int threadPool; private String email; //getters and setters } IV. 配置文件 global.properties文件 email=zaitingma@foxmail.com thread-pool=12 application.properties文件 #Logging logging.level.org.springframework.web=ERROR logging.level.com.mazaiting=DEBUG #Global email=1425941077@qq.com thread-pool=10 #App app.menus[0].title=Home app.menus[0].name=Home app.menus[0].path=/ app.menus[1].title=Login app.menus[1].name=Login app.menus[1].path=/login app.compiler.timeout=5 app.compiler.output-folder=/temp/ app.error=/error/ 注: 如果在application.properties文件中有与global.properties中有相同的属性,取值时则取application.properties中的配置 V. 对应的java文件 AppProperties.java @Component //prefix app, find app.* values @ConfigurationProperties("app") public class AppProperties { private String error; private List<Menu> menus = new ArrayList<>(); private Compiler compiler = new Compiler(); public String getError() { return error; } public void setError(String error) { this.error = error; } public List<Menu> getMenus() { return menus; } public void setMenus(List<Menu> menus) { this.menus = menus; } public Compiler getCompiler() { return compiler; } public void setCompiler(Compiler compiler) { this.compiler = compiler; } public static class Menu { private String name; private String path; private String title; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } @Override public String toString() { return "Menu [name=" + name + ", path=" + path + ", title=" + title + "]"; } } public static class Compiler { private String timeout; private String outputFolder; public String getTimeout() { return timeout; } public void setTimeout(String timeout) { this.timeout = timeout; } public String getOutputFolder() { return outputFolder; } public void setOutputFolder(String outputFolder) { this.outputFolder = outputFolder; } @Override public String toString() { return "Compiler [timeout=" + timeout + ", outputFolder=" + outputFolder + "]"; } } @Override public String toString() { return "AppProperties [error=" + error + ", menus=" + menus + ", compiler=" + compiler + "]"; } } GlobalProperties.java @Component @PropertySource("classpath:global.properties") public class GlobalProperties { @Value("${thread-pool}") private int threadPool; @Value("${email}") private String email; public int getThreadPool() { return threadPool; } public void setThreadPool(int threadPool) { this.threadPool = threadPool; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "GlobalProperties [threadPool=" + threadPool + ", email=" + email + "]"; } } VI. 应用入口SpringBootWebApplication @SpringBootApplication public class SpringBootWebApplication { public static void main(String[] args) { SpringApplication.run(SpringBootWebApplication.class, args); } } VII. 控制器 @Controller public class WelcomeController { private static final Logger LOGGER = LoggerFactory.getLogger(WelcomeController.class); private GlobalProperties global; private AppProperties app; @Autowired public void setApp(AppProperties app) { this.app = app; } @Autowired public void setGlobalProperties(GlobalProperties global) { this.global = global; } @RequestMapping("/") public String welcome(Map<String, Object> model) { String globalProperties = global.toString(); String appProperties = app.toString(); LOGGER.debug("Welcome {}, {}", app, global); model.put("message", appProperties + globalProperties); return "welcome"; } } VIII. 运行SpringBootWebApplication,浏览器输入http://localhost:8080/,查看控制台 2018-05-14 13:07:22.056 DEBUG 4228 --- [nio-8080-exec-3] com.mazaiting.config.WelcomeController : Welcome AppProperties [error=/error/, menus=[Menu [name=Home, path=/, title=Home], Menu [name=Login, path=/login, title=Login]], compiler=Compiler [timeout=5, outputFolder=/temp/]], GlobalProperties [threadPool=10, email=1425941077@qq.com] IX. 验证 @Component @ConfigurationProperties public class GlobalProperties { @Max(5) @Min(0) private int threadPool; @NotEmpty private String email; //getters and setters } 10). 配置文件中配置logging #Logging # log level 日志等级 logging.level.org.springframework.web=ERROR logging.level.com.mazaiting=DEBUG # log file 日志文件存放路径 #logging.file=${java.io.tmpdir}/application.log # output to a file 存放路径 logging.file=C:/Users/Administrator/Desktop/mylog.log # pattern 格式化文件内容 logging.pattern.file="%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n" 代码下载

优秀的个人博客,低调大师

分布式--Redis 内存数据库

1. Redis 内存数据库 1). Redis Redis是一个开源的,高性能,C语言开发的 ,键值对(key-value)存储数据的NOSQL数据库。 2). 基本概念 NOSQL :Not Only Sql 泛指非关系型数据库 Redis MongoDB Hbase 关系型数据库: mysql oracle sybase sqlServer 3). 数据库应用的发展历史 1:在互联网时代来临之前 ,企业的一些内部信息管理系统,一个单个的数据库的实例就可以应付系统的功能需求单数据库实例 2:随着系统访问用户的增多 ,数据量的增大,单个数据库的实例已经满足不了系统频繁读取数据(权限的验证,系统的默认配置)的需求 。缓存 (ehCache/MemCached)+ 数据库实例 3: 缓存虽然可以缓解数据库的读取压力 ,但是数据量的写入压力持续增大 ,这个时候我们需要采取数据库主从机制进行读写分离缓存 (ehCache/MemCached)+ 主从数据库 + 读写分离 4: 数据量再次增大,读写分离以后,主数据库的写库的压力出现瓶颈缓存 (ehCache/MemCached)+ 主从数据库 + 读写分离 +分库分表 5:互联网和大数据的时代的来林,关系型数据库已经不能很好的存储一些并发性大,实时性高,而且格式不固定的数据Nosql数据库+主从数据库 + 读写分离 +分库分表 4). 系统架构中的位置 图1.png 2. 安装 3. Redis的五大数据类型以及应用 String/Hash/List/Set/sorted set 字符串类型 String 列表类型 list 集合类型 set 有序集合类型 sorted set 散列类型 hash 1). K-V格式中 V是一个字符串对象 set k1 v1 get k1 del k1 incr num (自增1 如果没有设置值默认为1) decr num (自减1) incrby num 5 (自增5) decrby num 5 (自减 5) Append num 4 (追加4)返回字符串长度位数,不是数学+ 是拼接 应用场景: 秒杀商品/商品点赞 2). Hash K-V格式中 V是一个Hash类型 其实java里面的Map<String,object> 赋值命令 hset student id 1 hset student name mazaiting hset student sex boy hmset myhash username admin age 20 取值命令 hget myhash username hget myhash username age hgetall myhash 删除命令 hdel myhash username age del myhash (删除myhash) hincrby myhash age 5 (增加5) 其他命名 hexists myhash username 判断myhash中是否包含username包含返回1否则返回0 hlen myhash 获取myhash中的字段数 hkeys myhash 获取所有的 key hvals myhash 获取所有的值 应用场景: 电商网站的商品详细信息 3). list K-V格式中 V是一个list类型 其实java里面的LinkedList 把他看成一个竹子,两边是打通了 从左插入记录 lpush book 1 2 3 从右插入记录 rpush book 4 5 6 从左弹出记录 lpop book 1 从右弹出记录 lpush book 1 列表里面的记录顺序(竹子)-- 3 2 1 4 5 6 应用场景: 商品的评论 lpush mylist 1 2 3 从左插入记录 rpush mylist a b c 从右插入记录 lrange mylist 开始索引位置 结束索引位置(-1最后一个) lpop mylist 左侧弹出(相当于删除) rpop mylist 右侧弹出(相当于删除) lpush mylist 左侧插入 rpush mylist 右侧插入 lrem mylist 删除几个元素 删除哪个元素 lset mylist 索引位置 插入的值 linsert mylist before 哪个值前面 要插入的值 linsert mylist after 哪个值后面 要插入的值 rpoplpush 集合1 集合2 把集合1中取出的值放在集合2中 4). Set K-V格式中 V是一个set类型 set是无序的/set里面的元素具有唯一性、 sadd summer_friends sky big mo mike sadd maybe_friends sky big tdd world oky 应用场景: 独立IP进行网站投票 /微信朋友圈的共同好友 sadd myset a b c 添加 a b c 不能添加重复的值 srem myset a c 移除a c smembers myset 查看所有值 sismember myset a 查看集合里面是否包含a包含返回1 否则返回0 sadd myset2 a b c sadd myset3 a c 1 sdiff myset2 myset3 myset2 和myset3相差的是b 差集运算 sinter myset2 myset3 myset2 和myset3相交的是a c 交集运算 sunion myset2 myset3 并集运算 合并集合 相同的会去掉(值不能重复) scard myset 获取集合数量 srandmember myset 随机获取集合中的一个值 sdiffstore myset4 myset2 myset3 将myset2 和myset3中相差的值放到myset4里面 sinterstore myset4 myset2 myset3 将myset2 和myset3中相交的值放到myset4里面 sunionstore myset4 myset2 myset3 将myset2 和myset3中相并的值放到myset4里面 5). sorted-set 游戏排名 热点话题中应用 K-V格式中 V是一个Zset类型 zset是有序的? 怎么做到有序 应用场景: 商品的销售排行榜 zadd sales 100 huawei 99 iphone 98 vivo 97 oppo Zrange sales 0 -1 withscores 从小到大 Zrevrange sales 0 -1 withscores 从大到小 4. Java 使用Redis 1). 工具下载 Jedis下载 此处使用windows下的redis工具包,因为虚拟机连接会出现很多问题。 1). Redis-win下载 下载后解压,如下图 图3.png 2). 在上方地址栏输入cmd然后回车,这时会打开命令提示符窗口,然后输入redis-server.exe redis.windows.conf 开启redis服务器. 图4.png 3). 在上方地址栏继续输入cmd回车,输入命令``打开redis客户端 图5.png 2). 连接 public class RedisJava { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1", 6379); System.out.println("服务正在运行: " + jedis.ping()); jedis.close(); } } 3). 测试 public class RedisJava { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1", 6379); System.out.println("服务正在运行: " + jedis.ping()); jedis.set("name", "linghaoyu"); System.out.println(jedis.get("name"));; jedis.close(); } } 打印结果: 图6.png 5. 其他 1). Redis HyperLogLog命令 Redis HyperLogLog 是用来做基数统计的算法,HyperLogLog 的优点是,在输入元素的数量或者体积非常非常大时,计算基数所需的空间总是固定 的、并且是很小的。基数:数据集 {1, 3, 5, 7, 5, 7, 8}, 那么这个数据集的基数集为 {1, 3, 5 ,7, 8}, 基数(不重复元素)为5。 基数估计就是在误差可接受的范围内,快速计算基数。 图7.png 2). Redis 订阅 I. Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息。Redis 客户端可以订阅任意数量的频道。 下图展示了频道 channel1 , 以及订阅这个频道的三个客户端 —— client2 、 client5 和 client1 之间的关系: 图8.png 当有新消息通过 PUBLISH 命令发送给频道 channel1 时, 这个消息就会被发送给订阅它的三个客户端: 图9.png II. 实例 订阅 subscribe redisChat 图10.png 发布消息 publish redisChat "Redis is a great caching technique" 图11.png 3). Redis 事务 Redis 事务可以一次执行多个命令, 并且带有以下两个重要的保证: 事务是一个单独的隔离操作:事务中的所有命令都会序列化、按顺序地执行。事务在执行的过程中,不会被其他客户端发送来的命令请求所打断。 事务是一个原子操作:事务中的命令要么全部被执行,要么全部都不执行。 一个事务从开始到执行会经历以下三个阶段: 开始事务。 命令入队。 执行事务。 示例: 127.0.0.1:6379> multi OK 127.0.0.1:6379> set book-name "Mastering C++ in 21 days" QUEUED 127.0.0.1:6379> get book-name QUEUED 127.0.0.1:6379> sadd tag "C++" "Programming" "Matering Series" QUEUED 127.0.0.1:6379> smembers tag QUEUED 127.0.0.1:6379> exec 1) OK 2) "Mastering C++ in 21 days" 3) (integer) 3 4) 1) "Matering Series" 2) "C++" 3) "Programming" 图12.png 序号 命令及描述 1 DISCARD 取消事务,放弃执行事务块内的所有命令。 2 EXEC 执行所有事务块内的命令。 3 MULTI 标记一个事务块的开始。 4). Redis 桌面管理 代码下载

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册