SpringBoot:1.5.10.RELEASE
定制SpringApplication
以关闭 Banner 为例:
1 |
|
或者 application.yml 中
1 | spring: |
流式API
1 |
|
SpringBoot:1.5.10.RELEASE
定制SpringApplication
以关闭 Banner 为例:
1 | import org.springframework.boot.Banner; |
或者 application.yml 中
1 | spring: |
流式API
1 | import org.springframework.boot.Banner; |
SpringBoot:1.5.10.RELEASE
SpringBoot 推荐基于 Java 维护元数据信息,但是也支持基于 XML 维护元数据信息。
配置类通过 @Configuration 注解可以进行自动扫描
SpringBoot 支持多个配置类单独配置,可通过 @Import 注解进行集中导入或者通过 @ComponentScan 自动扫描
如果想集成基于 XML 维护元数据,可通过 @ImportResource 注解进行集成
自动配置需要使用 @EnableAutoConfiguration 注解
@SpringBootApplication 注解组合了上述注解
1 | package com.example.myproject; |
@SpringBootApplication 组合了 @Configuration @EnableAutoCofiguration @ComponentScan 三个注解
可通过如下方式去除自动配置:
1 | import org.springframework.boot.autoconfigure.*; |
@EnableAutoConfiguration 可以通过 exclude 进行排除
SpringBoot:1.5.10.RELEASE
不要使用默认包
default package 不能使用,SpringBoot 特性是自动扫描,自动装配,对代码结构有一定的约定,default package 结构可能影响自动扫描,自动装配
启动类的位置
官方推荐启动类位于根目录,因为 @EnableAutoConfiguration 注解是基于当前类所在包,扫描当前类所在包及子包
典型布局:1
2
3
4
5
6
7
8
9
10
11
12
13
14com
+- example
+- myproject
+- Application.java
|
+- domain
| +- Customer.java
| +- CustomerRepository.java
|
+- service
| +- CustomerService.java
|
+- web
+- CustomerController.java
1 | package com.example.myproject; |
SpringBoot:1.5.10.RELEASE
SpringBoot 引入有两种方式:
继承
1 | <parent> |
依赖管理
1 | <dependencyManagement> |
推荐使用依赖管理的方式,耦合性更低
Java 8 除 Lambda 表达式这一重大变化外,另一个就是 Stream API。
Stream API 是 Java 8 处理集合的关键抽象,希望对集合可以进行非常复杂的查找、过滤和映射数据等操作。
Spring Boot: 1.5.10.RELEASE
Spring Cloud: Dalston.SR5
从之前可知路由配置规则:
properties 风格:
1 | zuul.routes.<routh>.path = xxxx |
zuul:
routes:
path: xxxx
serviceId: xxxxx1
2
如:
zuul:
routes:
user-service:
path: /user-service/**
serviceId: user-service`
路径匹配主要是设置 path 参数,采用 Ant 风格定义, Ant 是根据通配符来进行匹配:
通配符 | 说明 |
---|---|
? | 匹配任意单个字符 |
* | 匹配任意数量的字符 |
** | 匹配任何数量的字符,支持多级 |
示例:
URL | 说明 |
---|---|
/user-service/? | 可以匹配 /user-service/ 之后拼接一个任意字符的路径,如 /user-service/a,/user-service/b,/user-service/c |
/user-service/* | 可以匹配 /user-service/ 之后拼接的任意字符的路径,如 /user-service/a,/user-service/aa,/user-service/aaaaaa,但是不支持多级 |
/user-service/** | 可以匹配 /user-service/ 之后拼接的任意字符的路径,且支持多级如 /user-service/a,/user-service/aa,/user-service/aaaaaa,/user-service/aa/bb,/user-service/aa/bb/cc 等等 |
路径匹配的顺序问题
默认路径匹配的顺序是根据配置文件中的配置顺序,而且因为 properties 文件无法保证有序,推荐使用 yml 格式
一个推荐的路径配置顺序是,层级越多的顺序优先
Spring Boot: 1.5.10.RELEASE
Spring Cloud: Dalston.SR5
Zuul 会暴露一个路由管理端点 /routes,借助这个端点,可以方便、直观地查看以及管理 Zuul 的路由。
使用 GET 请求访问该路由管理端点即可获取 Zuul 当前映射的路由列表。
使用 POST 请求访问该路由管理端点即可强制刷新 Zuul 当前映射的路由列表。
默认 SpringCloud 因为安全机制是不允许访问,需设置 management.security.enabled=false
在上一篇 SpringCloud Zuul 请求路由中有演示配置路由规则,那么如果不配置路径规则会怎么样?会通过服务提供者的请求路径直接访问吗?
还是使用上一篇的例子,只是修改配置文件 appliation.yml 如下:
1 | spring: |
POST 请求路由管理端点:
1 | http://localhost:8888/routes |
结果:
1 | { |
而实际路径如下:
hello-world-service:9001 的 /hello URL
hello-world-service-provide:9004 的 /hello URL
hello-world-service-provide:9005 的 /hello URL
可以发现 Zuul 默认会帮我们配置一个路由规则:默认以服务名称作为path的请求前缀
调用如下URL:
http://localhost:8888/hello-world-service/hello
http://localhost:8888/hello-world-service-provide/hello
因为 Zuul 的默认路由规则存在,那么等于默认是开放了所有的服务路径。故若想有选择的开放路由,则需要利用zuul.ignored-service= /* 进行路由屏蔽然后再通过开放指定路由的方式。
修改配置文件 appliation.yml 如下:
1 | spring: |
POST 请求路由管理端点:
1 | http://localhost:8888/routes |
结果:
1 | {} |
修改配置文件 appliation.yml 如下:
1 | spring: |
POST 请求路由管理端点:
1 | http://localhost:8888/routes |
结果:
1 | { |
调用如下URL:
http://localhost:8888/hello-world-service/hello 可以正常访问
http://localhost:8888/hello-world-service-provide/hello 不可以正常访问
Lambda 可以使用现成已经实现的方法。
要求被调用的方法的参数类型和返回值和 Lambda 表达式中函数接口中抽象方法的参数类型和返回值一致
对象::实例方法名
1 | Consumer<String> consumer = x -> System.out.println(x); |
1 | Consumer<String> consumer = x -> System.out.println(x); |
类::静态方法名
1 | Comparator<Integer> comparator = (x, y) -> Integer.compare(x, y); |
类::实例方法名
要求 第一个参数是实例方法的调用者,第二个参数是实例方法的参数
1 | BiPredicate<String, String> biPredicate = (x, y) -> x.equals(y); |
构造函数引用
可以自动匹配对应的构造函数
1 | Supplier<String> supplier = () -> new String(); |
1 | Function<String, String> function = (x) -> new String(x); |
数组引用
1 | Function<Integer, String[]> function = (x) -> new String[x]; |
Consumer 接口
1 | public interface Consumer<T> { |
1 | public void happy(double money, Consumer<Double> consumer) { |
1 | happy(1000, money -> System.out.println("吃喝玩乐")); |
1 | happy(10000, money -> System.out.println("旅游")); |
Supplier 接口
1 | public interface Supplier<T> { |
1 | public List<Integer> getNumList(int num, Supplier<Integer> supplier) { |
1 | getNumList(10, () -> (int)(Math.random() * 100)) |
Function 接口
1 | public interface Function<T, R> { |
1 | public String strHandler(String str, Function<String, String> function) { |
1 | strHandler(" Lambda ", str -> str.trim().toUpperCase()) |
Predicate 接口
1 | public interface Predicate<T> { |
1 | public List<String> filterStr(List<String> list, Predicate<String> predicate) { |
1 | List<String> strings = Arrays.asList("Hello", "Lambda", "Function", "Java"); |
tag:
缺失模块。
1、请确保node版本大于6.2
2、在博客根目录(注意不是yilia根目录)执行以下命令:
npm i hexo-generator-json-content --save
3、在根目录_config.yml里添加配置:
jsonContent: meta: false pages: false posts: title: true date: true path: true text: false raw: false content: false slug: false updated: false comments: false link: false permalink: false excerpt: false categories: false tags: true