There is a problem of finding the best instance of a service in distributed systems with dynamic configuration. Nowadays, there are many products for the configuration storage and service discovery. It should be mentioned at least Netflix Eureka, Consul, etc or good old Zookeeper. These products can keep and give configuration, manage service instances lifecycle and some of them even can be as dynamic DNS service. But main question is not about what instance may be called at the certain time. It is about what instance is better for call? This means that smart load balancing top on service discovery is required. Spring Cloud project allows to integrate these products to your project and provides powerful solutions for typical problems, that make cloud native services developing easier. This talk will review the internal structure of SpringCloud implementation of Client-Side Service Discovery and Client Load Balancing patterns. It also will include specific details of concrete implementations with examples from official libraries and the authors own library.
Talk about Spring Boot Internals
* Spring Ripper retrospective
* how to build spring boot project. Maven/Gradle plugins
** DependencyManagement in gradle and maven
** Executable artifacts (war or jar)
** War and Jar anatomy
** Embeded tomcat and standalone tomcat integration SPI. WebApplicationInitializer and ServletContainerInitializer
** Tomcat in Tomcat like a Crank
** Executable jar anatomy
** Main Class and Start-Class. Java MANIFEST.MF anatomy
** Jar like a War, but Jar JarCraft
** Runtime ClassPath in spring boot applications
* SpringApplication.run ...
** Arguments
** Sources types
** Two general context type in spring boot app
** Starters and autoconfiguration
** spring.factories and SpringFactoriesLoader
** Merge app sources
** Environment and EnvironmentPostProcessor
** ConfigFileApplicationListener mistakes
** Spring Events vs Spring Boot Events
** Application Initializers
** Context prepare
** BeanDefinitionLoader
* @SpringBootApplication anatomy
** @Import Three types of arguments
*** ImportSelector
*** @Configuration
*** ImportBeanDefinitionRegistrar
** Who is your boss starters? @EnableAutoConfiguration anatomy
** Ugly internal spring boot starter
** ConfigurationClassParser
*** Configuration and Lite Configuration
** Conditional and shouldSkip in different context initialisation steps
12. Java 8
./TypeInferrenceTest.java:7: error: cannot infer
type arguments for HashMap<K,V>
new HashMap<>()
^
reason: cannot use '<>' with anonymous inner classes
where K,V are type-variables:
K extends Object declared in class HashMap
V extends Object declared in class HashMap
1 error
Diamond 舒仆仂仆亳仄仆仄亳 从仍舒舒仄亳
16
class TypeInferrenceTest {
Map<String, String> map =
new HashMap<> ()
{
{
map.put("key", "value");
}
};
}
13. 舒仗亠 仆舒 亳仗仂仍亰仂于舒仆亳亠 _ 于 亳仄亠仆舒
17
// -source 8:
// warning: as of release 9, '_' is a keyword,
// and may not be used as an identifier
// -source 9:
// error: as of release 9, '_' is a keyword,
// and may not be used as an identifier
class UnderscoreAsIdentifierWarning {
String _ = null;
}
14. 亞亳亠 仂弍仆仂于仍亠仆亳 磶从舒 (?)
舒亟仂亠仍仂 仗亳舒 亳仄 亳仗舒 于 亟亠从仍舒舒亳亳 仗亠亠仄亠仆仆仂亶?
18
var list = new ArrayList<String>();
JEP-286 Local-Variable Type Inference. 仂从舒 仂 仂仍从仂 从舒仆亟亳亟舒
var result = processNewCustomerAdded();
15. 个舒弍亳仆亠 仄亠仂亟 亟仍 从仂仍仍亠从亳亶
弌仂亰亟舒仆亳亠 仆亠亳亰仄亠仆仆 从仂仆亠亶仆亠仂于
19
Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set = Collections.unmodi鍖ableSet(set);
Set<String> set = Collections.unmodi鍖ableSet(
new HashSet<>(Arrays.asList("a", "b")));
Set<String> set = Collections.unmodi鍖ableSet(
new HashSet<String>() {{
add("a");
add("b");
}});
Set<String> set = Collections.unmodi鍖ableSet(
Stream.of("a", b").collect(toSet())
);
Set<String> set = Set.of("a", "b");
17. 仂于仂亠 于 Stream API (1)
22
Stream::takeWhile
while (predicate_on_stream_element) {
<keep processing this ordered stream>
};
Stream.of(3, 2, 1, 0, -1, 0, 1, 2, 3)
.takeWhile(s -> s > 0)
.forEach(System.out::println);
3
2
1
18. 仂于仂亠 于 Stream API (2)
23
Stream::dropWhile
while (predicate_on_stream_element) {
<skip element of this ordered stream>
};
Stream.of(3, 2, 1, 0, -1, 0, 1, 2, 3)
.dropWhile(s -> s > 0)
.forEach(System.out::println);
0
-1
0
1
2
3
19. 仂于仂亠 于 Stream API (3)
24
Stream::Iterate 仗亠亟亳从舒仂仄
Stream.iterate(3, i -> i > 0, i -> i-1)
.forEach(System.out::println);
3
2
1
于亠亳 亟仍 Java 8:
Stream.iterate(3, i -> i-1)
.limit(3)
.forEach(System.out::println);
20. Stream API changes (4)
25
Stream::ofNullable
个舒从仂亳 亟仍 仂亟仆仂亞仂 仆亠仆仍亠于仂亞仂 仍亠仄亠仆舒
long a = Stream.ofNullable(1).count();
long b = Stream.ofNullable(null).count();
System.out.println(a);
System.out.println(b);
1
0
21. Spin Loop Hint (JEP-285)
丶亠仍: latency (亳 仗仂亳亰于仂亟亳亠仍仆仂)
亠 API:
亠仂亟 j.l.Thread.onSpinWait()
舒 x86 亳仗仂仍亰亠 亳仆从亳 pause
仗仂仍亰亠 于 JSR 166 亟仍 JDK9
java/util/concurrent/locks/StampedLock.java
java/util/concurrent/Phaser.java
java/util/concurrent/SynchronousQueue.java
class EventHandler {
volatile boolean eventNoti鍖cationNotReceived;
void waitForEventAndHandleIt() {
while ( eventNoti鍖cationNotReceived ) {
java.lang.Thread.onSpinWait();
}
readAndProcessEvent();
}
void readAndProcessEvent() {
// Read event from some source and process it
. . .
}
}
26
33. 弍仆仂于仍亠仆亳亠 Process API
JEP 102: Process API Updates
仂于仂亠:
仂仍亳 pid 于仂亠亶 JVM
仂仍亳 仗亳仂从 仗仂亠仂于
亳亠仄
舒弍仂舒 亟亠亠于礆亳 仗仂亠仂于
supportsNormalTermination() ?
Source: http://blog.takipi.com/java-9-the-ultimate-feature-list/ 38
34. 亳仄亠 Process API
Source: http://blog.takipi.com/java-9-the-ultimate-feature-list/ 39
Process proc = Runtime.getRuntime()
.exec(new String[]{"/bin/sh",
"-c", "echo $PPID"});
if (proc.waitFor()==0) {
InputStream in = proc.getInputStream();
int available = in.available();
byte[] outputBytes = new byte[available];
in.read(outputBytes);
String pid = new String(outputBytes);
System.out.println("Your pid is " + pid)
}
System.out.println("Your pid is "
+ ProcessHandle.current().getPid());