Compare commits
79 Commits
config-lab
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7dbc34ec1f | ||
|
|
4b0af6f233 | ||
|
|
4bf616ec0c | ||
|
|
fd635d96b3 | ||
|
|
660c0c821e | ||
|
|
f747d43fa5 | ||
|
|
aa277670c0 | ||
|
|
abc8dfdea7 | ||
|
|
a2d33c0d1e | ||
|
|
c610db6afc | ||
|
|
15123bc2bc | ||
|
|
c297adad3c | ||
|
|
6ac5953861 | ||
|
|
9043420c50 | ||
|
|
f356489f3b | ||
|
|
effa9b6693 | ||
|
|
8cda2a5187 | ||
|
|
51b5f430ec | ||
|
|
1380936fef | ||
|
|
d12659b80b | ||
|
|
6758d4d405 | ||
|
|
f9c3aa9e93 | ||
|
|
650c605af7 | ||
|
|
c7d829c01a | ||
|
|
ed2aa26c67 | ||
|
|
e3a75909b5 | ||
|
|
97ddd7c8ff | ||
|
|
8c733cc161 | ||
|
|
e02520281e | ||
|
|
311243bbc5 | ||
|
|
cfe10f8b8f | ||
|
|
8619381086 | ||
|
|
edcd6c27d1 | ||
|
|
b66548075d | ||
|
|
e5656ab36f | ||
|
|
073e829792 | ||
|
|
2e13917cb2 | ||
|
|
af23b60e30 | ||
|
|
cbc8af61a4 | ||
|
|
1c355c3ffe | ||
|
|
ae68a47506 | ||
|
|
af21e4f9f1 | ||
|
|
41fa06bf5c | ||
|
|
5d565f9e33 | ||
|
|
29a22a0ee1 | ||
|
|
4329c564d6 | ||
|
|
6f0f6f6e69 | ||
|
|
42f2b6210e | ||
|
|
a61256229b | ||
|
|
a9b169aeb1 | ||
|
|
6c6398adef | ||
|
|
3e1910c226 | ||
|
|
1069c79832 | ||
|
|
2431d464eb | ||
|
|
b242423b61 | ||
|
|
a8a20a76cb | ||
|
|
aaea217048 | ||
|
|
cd370b0aa2 | ||
|
|
53d0df5c65 | ||
|
|
badffc6817 | ||
|
|
4038f19d13 | ||
|
|
7d6712a878 | ||
|
|
54d13550dd | ||
|
|
4bacadcaca | ||
|
|
dfe067b1b7 | ||
|
|
f56e27fb87 | ||
|
|
ae71e94e3a | ||
|
|
703bc416cf | ||
|
|
a5a997eb97 | ||
|
|
2ccdde4fce | ||
|
|
e161019a18 | ||
|
|
90dcb48bbb | ||
|
|
510b2bc1fb | ||
|
|
aae309df5a | ||
|
|
b595d29c27 | ||
|
|
cf1cba9165 | ||
|
|
7f0bfb5afe | ||
|
|
e179d99643 | ||
|
|
e959483dd1 |
14
.gitignore
vendored
Normal file
14
.gitignore
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
.class
|
||||
.classpath
|
||||
.project
|
||||
build
|
||||
bin
|
||||
.bak
|
||||
.settings
|
||||
Thumbs.db
|
||||
.DS_Store
|
||||
._.DS_Store
|
||||
logs
|
||||
target
|
||||
*.iml
|
||||
.idea
|
||||
54
Chapter2-1-2/pom.xml
Normal file
54
Chapter2-1-2/pom.xml
Normal file
@@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.didispace</groupId>
|
||||
<artifactId>Chapter2-1-2</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Chapter2-1-2</name>
|
||||
<description>Spring Boot 2.0 features : Application Events and Listeners</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.0.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
31
Chapter2-1-2/src/main/java/com/didispace/Application.java
Executable file
31
Chapter2-1-2/src/main/java/com/didispace/Application.java
Executable file
@@ -0,0 +1,31 @@
|
||||
package com.didispace;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@Slf4j
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataLoader dataLoader() {
|
||||
return new DataLoader();
|
||||
}
|
||||
|
||||
@Slf4j
|
||||
static class DataLoader implements CommandLineRunner {
|
||||
|
||||
@Override
|
||||
public void run(String... strings) throws Exception {
|
||||
log.info("Loading data...");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.didispace;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
|
||||
@Slf4j
|
||||
public class ApplicationEnvironmentPreparedEventListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
|
||||
log.info("......ApplicationEnvironmentPreparedEvent......");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.didispace;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.context.event.ApplicationFailedEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
|
||||
@Slf4j
|
||||
public class ApplicationFailedEventListener implements ApplicationListener<ApplicationFailedEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationFailedEvent event) {
|
||||
log.info("......ApplicationFailedEvent......");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.didispace;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.context.event.ApplicationPreparedEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
|
||||
@Slf4j
|
||||
public class ApplicationPreparedEventListener implements ApplicationListener<ApplicationPreparedEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationPreparedEvent event) {
|
||||
log.info("......ApplicationPreparedEvent......");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.didispace;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
|
||||
@Slf4j
|
||||
public class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationReadyEvent event) {
|
||||
log.info("......ApplicationReadyEvent......");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.didispace;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
|
||||
@Slf4j
|
||||
public class ApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationStartedEvent event) {
|
||||
log.info("......ApplicationStartedEvent......");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.didispace;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.context.event.ApplicationStartingEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
|
||||
@Slf4j
|
||||
public class ApplicationStartingEventListener implements ApplicationListener<ApplicationStartingEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationStartingEvent event) {
|
||||
log.info("......ApplicationStartingEvent......");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
org.springframework.context.ApplicationListener=com.didispace.ApplicationEnvironmentPreparedEventListener,\
|
||||
com.didispace.ApplicationFailedEventListener,\
|
||||
com.didispace.ApplicationPreparedEventListener,\
|
||||
com.didispace.ApplicationReadyEventListener,\
|
||||
com.didispace.ApplicationStartedEventListener,\
|
||||
com.didispace.ApplicationStartingEventListener
|
||||
0
Chapter9-2-1/src/main/resources/application.properties → Chapter2-1-2/src/main/resources/application.properties
Normal file → Executable file
0
Chapter9-2-1/src/main/resources/application.properties → Chapter2-1-2/src/main/resources/application.properties
Normal file → Executable file
49
Chapter2-2-1/pom.xml
Normal file
49
Chapter2-2-1/pom.xml
Normal file
@@ -0,0 +1,49 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.didispace</groupId>
|
||||
<artifactId>Chapter2-2-1</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Chapter2-2-1</name>
|
||||
<description>Spring Boot 2 : Relaxed Binding</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.0.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.16.20</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
44
Chapter2-2-1/src/main/java/com/didispace/Application.java
Executable file
44
Chapter2-2-1/src/main/java/com/didispace/Application.java
Executable file
@@ -0,0 +1,44 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @blog http://blog.didispace.com
|
||||
*
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext context = SpringApplication.run(Application.class, args);
|
||||
|
||||
Binder binder = Binder.get(context.getEnvironment());
|
||||
|
||||
// 绑定简单配置
|
||||
FooProperties foo = binder.bind("com.didispace", Bindable.of(FooProperties.class)).get();
|
||||
System.out.println(foo.getFoo());
|
||||
|
||||
// 绑定List配置
|
||||
List<String> post = binder.bind("com.didispace.post", Bindable.listOf(String.class)).get();
|
||||
System.out.println(post);
|
||||
|
||||
List<PostInfo> posts = binder.bind("com.didispace.posts", Bindable.listOf(PostInfo.class)).get();
|
||||
System.out.println(posts);
|
||||
|
||||
// 读取配置
|
||||
System.out.println(context.getEnvironment().containsProperty("com.didispace.database-platform"));
|
||||
System.out.println(context.getEnvironment().containsProperty("com.didispace.databasePlatform"));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
14
Chapter2-2-1/src/main/java/com/didispace/FooProperties.java
Normal file
14
Chapter2-2-1/src/main/java/com/didispace/FooProperties.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package com.didispace;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "com.didispace")
|
||||
public class FooProperties {
|
||||
|
||||
private String foo;
|
||||
|
||||
private String databasePlatform;
|
||||
|
||||
}
|
||||
13
Chapter2-2-1/src/main/java/com/didispace/PostInfo.java
Normal file
13
Chapter2-2-1/src/main/java/com/didispace/PostInfo.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.didispace;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@Data
|
||||
@ConfigurationProperties
|
||||
public class PostInfo {
|
||||
|
||||
private String title;
|
||||
private String content;
|
||||
|
||||
}
|
||||
10
Chapter2-2-1/src/main/resources/application.properties
Executable file
10
Chapter2-2-1/src/main/resources/application.properties
Executable file
@@ -0,0 +1,10 @@
|
||||
com.didispace.foo=bar
|
||||
com.didispace.database-platform=sql
|
||||
|
||||
com.didispace.post[0]=Why Spring Boot
|
||||
com.didispace.post[1]=Why Spring Cloud
|
||||
|
||||
com.didispace.posts[0].title=Why Spring Boot
|
||||
com.didispace.posts[0].content=It is perfect!
|
||||
com.didispace.posts[1].title=Why Spring Cloud
|
||||
com.didispace.posts[1].content=It is perfect too!
|
||||
@@ -36,7 +36,7 @@ public class UserController {
|
||||
}
|
||||
|
||||
@ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
|
||||
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
|
||||
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
|
||||
@RequestMapping(value="/{id}", method=RequestMethod.GET)
|
||||
public User getUser(@PathVariable Long id) {
|
||||
return users.get(id);
|
||||
@@ -44,7 +44,7 @@ public class UserController {
|
||||
|
||||
@ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
|
||||
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path"),
|
||||
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
|
||||
})
|
||||
@RequestMapping(value="/{id}", method=RequestMethod.PUT)
|
||||
@@ -57,7 +57,7 @@ public class UserController {
|
||||
}
|
||||
|
||||
@ApiOperation(value="删除用户", notes="根据url的id来指定删除对象")
|
||||
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
|
||||
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
|
||||
@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
|
||||
public String deleteUser(@PathVariable Long id) {
|
||||
users.remove(id);
|
||||
|
||||
63
Chapter3-1-7/pom.xml
Normal file
63
Chapter3-1-7/pom.xml
Normal file
@@ -0,0 +1,63 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.didispace</groupId>
|
||||
<artifactId>Chapter3-1-7</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Chapter3-1-7</name>
|
||||
<description>Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.10.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.16.20</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<fork>true</fork>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
58
Chapter3-1-7/src/main/java/com/didispace/Application.java
Executable file
58
Chapter3-1-7/src/main/java/com/didispace/Application.java
Executable file
@@ -0,0 +1,58 @@
|
||||
package com.didispace;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@RestController
|
||||
class HelloController {
|
||||
|
||||
@PostMapping("/user")
|
||||
public UserDto user(@RequestBody UserDto userDto) throws Exception {
|
||||
return userDto;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
static class UserDto {
|
||||
|
||||
private String userName;
|
||||
private LocalDate birthday;
|
||||
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ObjectMapper serializingObjectMapper() {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
objectMapper.registerModule(new JavaTimeModule());
|
||||
return objectMapper;
|
||||
}
|
||||
|
||||
}
|
||||
0
Chapter3-1-7/src/main/resources/application.properties
Executable file
0
Chapter3-1-7/src/main/resources/application.properties
Executable file
66
Chapter3-2-10/pom.xml
Normal file
66
Chapter3-2-10/pom.xml
Normal file
@@ -0,0 +1,66 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.didispace</groupId>
|
||||
<artifactId>Chapter3-2-10</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Chapter3-2-10</name>
|
||||
<description>Spring Boot project</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.9.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-ldap</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.unboundid</groupId>
|
||||
<artifactId>unboundid-ldapsdk</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.16.16</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
13
Chapter3-2-10/src/main/java/com/didispace/Application.java
Normal file
13
Chapter3-2-10/src/main/java/com/didispace/Application.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
22
Chapter3-2-10/src/main/java/com/didispace/Person.java
Normal file
22
Chapter3-2-10/src/main/java/com/didispace/Person.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.didispace;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.ldap.odm.annotations.*;
|
||||
|
||||
import javax.naming.Name;
|
||||
|
||||
@Entry(base = "ou=people,dc=didispace,dc=com", objectClasses = "inetOrgPerson")
|
||||
@Data
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
private Name id;
|
||||
@DnAttribute(value = "uid", index = 3)
|
||||
private String uid;
|
||||
@Attribute(name = "cn")
|
||||
private String commonName;
|
||||
@Attribute(name = "sn")
|
||||
private String suerName;
|
||||
private String userPassword;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import javax.naming.Name;
|
||||
|
||||
public interface PersonRepository extends CrudRepository<Person, Name> {
|
||||
|
||||
|
||||
}
|
||||
4
Chapter3-2-10/src/main/resources/application.properties
Normal file
4
Chapter3-2-10/src/main/resources/application.properties
Normal file
@@ -0,0 +1,4 @@
|
||||
#spring.ldap.urls=ldap://localhost:1235
|
||||
#spring.ldap.base=dc=didispace,dc=com
|
||||
#spring.ldap.username=didispace
|
||||
#spring.ldap.password=123456
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class ApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private PersonRepository personRepository;
|
||||
|
||||
@Test
|
||||
public void findAll() throws Exception {
|
||||
|
||||
personRepository.findAll().forEach(p -> {
|
||||
System.out.println(p);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void save() throws Exception {
|
||||
Person person = new Person();
|
||||
person.setUid("uid:1");
|
||||
person.setSuerName("AAA");
|
||||
person.setCommonName("aaa");
|
||||
person.setUserPassword("123456");
|
||||
personRepository.save(person);
|
||||
|
||||
personRepository.findAll().forEach(p -> {
|
||||
System.out.println(p);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
3
Chapter3-2-10/src/test/resources/application.properties
Normal file
3
Chapter3-2-10/src/test/resources/application.properties
Normal file
@@ -0,0 +1,3 @@
|
||||
spring.ldap.embedded.ldif=ldap-server.ldif
|
||||
spring.ldap.embedded.base-dn=dc=didispace,dc=com
|
||||
|
||||
18
Chapter3-2-10/src/test/resources/ldap-server.ldif
Normal file
18
Chapter3-2-10/src/test/resources/ldap-server.ldif
Normal file
@@ -0,0 +1,18 @@
|
||||
dn: dc=didispace,dc=com
|
||||
objectClass: top
|
||||
objectClass: domain
|
||||
|
||||
dn: ou=people,dc=didispace,dc=com
|
||||
objectclass: top
|
||||
objectclass: organizationalUnit
|
||||
ou: people
|
||||
|
||||
dn: uid=ben,ou=people,dc=didispace,dc=com
|
||||
objectclass: top
|
||||
objectclass: person
|
||||
objectclass: organizationalPerson
|
||||
objectclass: inetOrgPerson
|
||||
cn: didi
|
||||
sn: zhaiyongchao
|
||||
uid: didi
|
||||
userPassword: {SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=
|
||||
68
Chapter3-2-11/pom.xml
Executable file
68
Chapter3-2-11/pom.xml
Executable file
@@ -0,0 +1,68 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.didispace</groupId>
|
||||
<artifactId>Chapter3-2-11</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Chapter3-2-11</name>
|
||||
<description>Spring Boot project</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.10.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.spring4all</groupId>
|
||||
<artifactId>mongodb-plus-spring-boot-starter</artifactId>
|
||||
<version>1.0.0.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.16.12</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
15
Chapter3-2-11/src/main/java/com/didispace/Application.java
Executable file
15
Chapter3-2-11/src/main/java/com/didispace/Application.java
Executable file
@@ -0,0 +1,15 @@
|
||||
package com.didispace;
|
||||
|
||||
import com.spring4all.mongodb.EnableMongoPlus;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@EnableMongoPlus
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
4
Chapter3-2-11/src/main/resources/application.properties
Normal file
4
Chapter3-2-11/src/main/resources/application.properties
Normal file
@@ -0,0 +1,4 @@
|
||||
spring.data.mongodb.uri=mongodb://localhost:27017/test
|
||||
|
||||
spring.data.mongodb.option.min-connection-per-host=20
|
||||
spring.data.mongodb.option.max-connection-per-host=200
|
||||
27
Chapter3-2-11/src/test/java/com/didispace/ApplicationTests.java
Executable file
27
Chapter3-2-11/src/test/java/com/didispace/ApplicationTests.java
Executable file
@@ -0,0 +1,27 @@
|
||||
package com.didispace;
|
||||
|
||||
import com.mongodb.MongoClient;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@Slf4j
|
||||
public class ApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private MongoClient mongoClient;
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
log.info("MinConnectionsPerHost = {}, MaxConnectionsPerHost = {}",
|
||||
mongoClient.getMongoClientOptions().getMinConnectionsPerHost(),
|
||||
mongoClient.getMongoClientOptions().getConnectionsPerHost());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,17 +3,18 @@ package com.didispace;
|
||||
import com.didispace.domain.User;
|
||||
import com.didispace.domain.UserMapper;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
@Transactional
|
||||
public class ApplicationTests {
|
||||
|
||||
@Autowired
|
||||
@@ -27,4 +28,4 @@ public class ApplicationTests {
|
||||
Assert.assertEquals(20, u.getAge().intValue());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -4,11 +4,11 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.didispace</groupId>
|
||||
<artifactId>Chapter9-2-1</artifactId>
|
||||
<artifactId>Chapter3-2-8</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Chapter9-2-1</name>
|
||||
<name>Chapter3-2-8</name>
|
||||
<description>Spring Boot project</description>
|
||||
|
||||
<parent>
|
||||
@@ -24,6 +24,7 @@
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
@@ -36,20 +37,15 @@
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>1.1.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>dubbo</artifactId>
|
||||
<version>2.4.9</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>spring</artifactId>
|
||||
<groupId>org.springframework</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.21</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
13
Chapter3-2-8/src/main/java/com/didispace/Application.java
Normal file
13
Chapter3-2-8/src/main/java/com/didispace/Application.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
47
Chapter3-2-8/src/main/java/com/didispace/domain/User.java
Normal file
47
Chapter3-2-8/src/main/java/com/didispace/domain/User.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package com.didispace.domain;
|
||||
|
||||
public class User {
|
||||
|
||||
private Long id;
|
||||
private String name;
|
||||
private Integer age;
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(Long id, String name, Integer age) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public User(String name, Integer age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.didispace.domain;
|
||||
|
||||
import org.apache.ibatis.annotations.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Mapper
|
||||
public interface UserMapper {
|
||||
|
||||
@Select("SELECT * FROM user WHERE name = #{name}")
|
||||
User findByName(@Param("name") String name);
|
||||
|
||||
@Results({
|
||||
@Result(property = "name", column = "name"),
|
||||
@Result(property = "age", column = "age")
|
||||
})
|
||||
@Select("SELECT name, age FROM user")
|
||||
List<User> findAll();
|
||||
|
||||
@Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})")
|
||||
int insert(@Param("name") String name, @Param("age") Integer age);
|
||||
|
||||
@Update("UPDATE user SET age=#{age} WHERE name=#{name}")
|
||||
void update(User user);
|
||||
|
||||
@Delete("DELETE FROM user WHERE id =#{id}")
|
||||
void delete(Long id);
|
||||
|
||||
@Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})")
|
||||
int insertByUser(User user);
|
||||
|
||||
@Insert("INSERT INTO user(name, age) VALUES(#{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER})")
|
||||
int insertByMap(Map<String, Object> map);
|
||||
|
||||
}
|
||||
4
Chapter3-2-8/src/main/resources/application.properties
Normal file
4
Chapter3-2-8/src/main/resources/application.properties
Normal file
@@ -0,0 +1,4 @@
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/test
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=123456
|
||||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.didispace;
|
||||
|
||||
import com.didispace.domain.User;
|
||||
import com.didispace.domain.UserMapper;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
@Transactional
|
||||
public class ApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Test
|
||||
@Rollback
|
||||
public void testUserMapper() throws Exception {
|
||||
// insert一条数据,并select出来验证
|
||||
userMapper.insert("AAA", 20);
|
||||
User u = userMapper.findByName("AAA");
|
||||
Assert.assertEquals(20, u.getAge().intValue());
|
||||
// update一条数据,并select出来验证
|
||||
u.setAge(30);
|
||||
userMapper.update(u);
|
||||
u = userMapper.findByName("AAA");
|
||||
Assert.assertEquals(30, u.getAge().intValue());
|
||||
// 删除这条数据,并select验证
|
||||
userMapper.delete(u.getId());
|
||||
u = userMapper.findByName("AAA");
|
||||
Assert.assertEquals(null, u);
|
||||
|
||||
u = new User("BBB", 30);
|
||||
userMapper.insertByUser(u);
|
||||
Assert.assertEquals(30, userMapper.findByName("BBB").getAge().intValue());
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("name", "CCC");
|
||||
map.put("age", 40);
|
||||
userMapper.insertByMap(map);
|
||||
Assert.assertEquals(40, userMapper.findByName("CCC").getAge().intValue());
|
||||
|
||||
List<User> userList = userMapper.findAll();
|
||||
for(User user : userList) {
|
||||
Assert.assertEquals(null, user.getId());
|
||||
Assert.assertNotEquals(null, user.getName());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
60
Chapter3-2-9/pom.xml
Normal file
60
Chapter3-2-9/pom.xml
Normal file
@@ -0,0 +1,60 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.didispace</groupId>
|
||||
<artifactId>Chapter3-2-9</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Chapter3-2-9</name>
|
||||
<description>Spring Boot with JDBCTemplate</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.9.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.21</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-core</artifactId>
|
||||
<version>5.0.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
13
Chapter3-2-9/src/main/java/com/didispace/Application.java
Executable file
13
Chapter3-2-9/src/main/java/com/didispace/Application.java
Executable file
@@ -0,0 +1,13 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.didispace.service;
|
||||
|
||||
/**
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @date 16/3/17 下午7:04.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
public interface UserService {
|
||||
|
||||
/**
|
||||
* 新增一个用户
|
||||
* @param name
|
||||
* @param age
|
||||
*/
|
||||
void create(String name, Integer age);
|
||||
|
||||
/**
|
||||
* 根据name删除一个用户高
|
||||
* @param name
|
||||
*/
|
||||
void deleteByName(String name);
|
||||
|
||||
/**
|
||||
* 获取用户总量
|
||||
*/
|
||||
Integer getAllUsers();
|
||||
|
||||
/**
|
||||
* 删除所有用户
|
||||
*/
|
||||
void deleteAllUsers();
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.didispace.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @date 16/3/17 下午6:44.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Override
|
||||
public void create(String name, Integer age) {
|
||||
jdbcTemplate.update("insert into user(NAME, AGE) values(?, ?)", name, age);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteByName(String name) {
|
||||
jdbcTemplate.update("delete from user where NAME = ?", name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getAllUsers() {
|
||||
return jdbcTemplate.queryForObject("select count(1) from user", Integer.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAllUsers() {
|
||||
jdbcTemplate.update("delete from user");
|
||||
}
|
||||
}
|
||||
6
Chapter3-2-9/src/main/resources/application.properties
Normal file
6
Chapter3-2-9/src/main/resources/application.properties
Normal file
@@ -0,0 +1,6 @@
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/test
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=
|
||||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
|
||||
flyway.locations=classpath:/db
|
||||
9
Chapter3-2-9/src/main/resources/db/V1__Base_version.sql
Normal file
9
Chapter3-2-9/src/main/resources/db/V1__Base_version.sql
Normal file
@@ -0,0 +1,9 @@
|
||||
DROP TABLE IF EXISTS `user` ;
|
||||
|
||||
|
||||
CREATE TABLE `user` (
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
|
||||
`name` varchar(30) NOT NULL COMMENT '姓名',
|
||||
`age` int(5) DEFAULT NULL COMMENT '年龄',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
48
Chapter3-2-9/src/test/java/com/didispace/ApplicationTests.java
Executable file
48
Chapter3-2-9/src/test/java/com/didispace/ApplicationTests.java
Executable file
@@ -0,0 +1,48 @@
|
||||
package com.didispace;
|
||||
|
||||
import com.didispace.service.UserService;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest
|
||||
public class ApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private UserService userSerivce;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
// 准备,清空user表
|
||||
userSerivce.deleteAllUsers();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
// 插入5个用户
|
||||
userSerivce.create("a", 1);
|
||||
userSerivce.create("b", 2);
|
||||
userSerivce.create("c", 3);
|
||||
userSerivce.create("d", 4);
|
||||
userSerivce.create("e", 5);
|
||||
|
||||
// 查数据库,应该有5个用户
|
||||
Assert.assertEquals(5, userSerivce.getAllUsers().intValue());
|
||||
|
||||
// 删除两个用户
|
||||
userSerivce.deleteByName("a");
|
||||
userSerivce.deleteByName("e");
|
||||
|
||||
// 查数据库,应该有5个用户
|
||||
Assert.assertEquals(3, userSerivce.getAllUsers().intValue());
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
56
Chapter4-1-3/pom.xml
Normal file
56
Chapter4-1-3/pom.xml
Normal file
@@ -0,0 +1,56 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.didispace</groupId>
|
||||
<artifactId>Chapter4-1-3</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Chapter4-1-3</name>
|
||||
<description>Spring Boot project</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.10.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.16.20</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
38
Chapter4-1-3/src/main/java/com/didispace/Application.java
Normal file
38
Chapter4-1-3/src/main/java/com/didispace/Application.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@EnableAsync
|
||||
@Configuration
|
||||
class TaskPoolConfig {
|
||||
|
||||
@Bean("taskExecutor")
|
||||
public Executor taskExecutor() {
|
||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||
executor.setCorePoolSize(10);
|
||||
executor.setMaxPoolSize(20);
|
||||
executor.setQueueCapacity(200);
|
||||
executor.setKeepAliveSeconds(60);
|
||||
executor.setThreadNamePrefix("taskExecutor-");
|
||||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
||||
return executor;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
48
Chapter4-1-3/src/main/java/com/didispace/async/Task.java
Normal file
48
Chapter4-1-3/src/main/java/com/didispace/async/Task.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package com.didispace.async;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @date 16/5/16 下午12:58.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class Task {
|
||||
|
||||
public static Random random = new Random();
|
||||
|
||||
@Async("taskExecutor")
|
||||
public void doTaskOne() throws Exception {
|
||||
log.info("开始做任务一");
|
||||
long start = System.currentTimeMillis();
|
||||
Thread.sleep(random.nextInt(10000));
|
||||
long end = System.currentTimeMillis();
|
||||
log.info("完成任务一,耗时:" + (end - start) + "毫秒");
|
||||
}
|
||||
|
||||
@Async("taskExecutor")
|
||||
public void doTaskTwo() throws Exception {
|
||||
log.info("开始做任务二");
|
||||
long start = System.currentTimeMillis();
|
||||
Thread.sleep(random.nextInt(10000));
|
||||
long end = System.currentTimeMillis();
|
||||
log.info("完成任务二,耗时:" + (end - start) + "毫秒");
|
||||
}
|
||||
|
||||
@Async("taskExecutor")
|
||||
public void doTaskThree() throws Exception {
|
||||
log.info("开始做任务三");
|
||||
long start = System.currentTimeMillis();
|
||||
Thread.sleep(random.nextInt(10000));
|
||||
long end = System.currentTimeMillis();
|
||||
log.info("完成任务三,耗时:" + (end - start) + "毫秒");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.didispace;
|
||||
|
||||
import com.didispace.async.Task;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest
|
||||
public class ApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private Task task;
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
|
||||
task.doTaskOne();
|
||||
task.doTaskTwo();
|
||||
task.doTaskThree();
|
||||
|
||||
Thread.currentThread().join();
|
||||
}
|
||||
|
||||
}
|
||||
60
Chapter4-1-4/pom.xml
Normal file
60
Chapter4-1-4/pom.xml
Normal file
@@ -0,0 +1,60 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.didispace</groupId>
|
||||
<artifactId>Chapter4-1-4</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Chapter4-1-4</name>
|
||||
<description>Spring Boot project</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.10.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.16.20</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
36
Chapter4-1-4/src/main/java/com/didispace/Application.java
Normal file
36
Chapter4-1-4/src/main/java/com/didispace/Application.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@EnableAsync
|
||||
@Configuration
|
||||
class TaskPoolConfig {
|
||||
|
||||
@Bean("taskExecutor")
|
||||
public Executor taskExecutor() {
|
||||
ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
|
||||
executor.setPoolSize(20);
|
||||
executor.setThreadNamePrefix("taskExecutor-");
|
||||
|
||||
executor.setWaitForTasksToCompleteOnShutdown(true);
|
||||
executor.setAwaitTerminationSeconds(60);
|
||||
return executor;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
49
Chapter4-1-4/src/main/java/com/didispace/async/Task.java
Normal file
49
Chapter4-1-4/src/main/java/com/didispace/async/Task.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package com.didispace.async;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @date 16/5/16 下午12:58.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class Task {
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Async("taskExecutor")
|
||||
public void doTaskOne() throws Exception {
|
||||
log.info("开始做任务一");
|
||||
long start = System.currentTimeMillis();
|
||||
log.info(stringRedisTemplate.randomKey());
|
||||
long end = System.currentTimeMillis();
|
||||
log.info("完成任务一,耗时:" + (end - start) + "毫秒");
|
||||
}
|
||||
|
||||
@Async("taskExecutor")
|
||||
public void doTaskTwo() throws Exception {
|
||||
log.info("开始做任务二");
|
||||
long start = System.currentTimeMillis();
|
||||
log.info(stringRedisTemplate.randomKey());
|
||||
long end = System.currentTimeMillis();
|
||||
log.info("完成任务二,耗时:" + (end - start) + "毫秒");
|
||||
}
|
||||
|
||||
@Async("taskExecutor")
|
||||
public void doTaskThree() throws Exception {
|
||||
log.info("开始做任务三");
|
||||
long start = System.currentTimeMillis();
|
||||
log.info(stringRedisTemplate.randomKey());
|
||||
long end = System.currentTimeMillis();
|
||||
log.info("完成任务三,耗时:" + (end - start) + "毫秒");
|
||||
}
|
||||
|
||||
}
|
||||
2
Chapter4-1-4/src/main/resources/application.properties
Normal file
2
Chapter4-1-4/src/main/resources/application.properties
Normal file
@@ -0,0 +1,2 @@
|
||||
spring.redis.pool.max-wait=5000
|
||||
spring.redis.pool.max-active=10
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.didispace;
|
||||
|
||||
import com.didispace.async.Task;
|
||||
import lombok.SneakyThrows;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest
|
||||
public class ApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private Task task;
|
||||
|
||||
@Test
|
||||
@SneakyThrows
|
||||
public void test() {
|
||||
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
task.doTaskOne();
|
||||
task.doTaskTwo();
|
||||
task.doTaskThree();
|
||||
|
||||
if (i == 9999) {
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
56
Chapter4-1-5/pom.xml
Normal file
56
Chapter4-1-5/pom.xml
Normal file
@@ -0,0 +1,56 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.didispace</groupId>
|
||||
<artifactId>Chapter4-1-5</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Chapter4-1-5</name>
|
||||
<description>Spring Boot project</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.10.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.16.20</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
38
Chapter4-1-5/src/main/java/com/didispace/Application.java
Normal file
38
Chapter4-1-5/src/main/java/com/didispace/Application.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@EnableAsync
|
||||
@Configuration
|
||||
class TaskPoolConfig {
|
||||
|
||||
@Bean("taskExecutor")
|
||||
public Executor taskExecutor() {
|
||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||
executor.setCorePoolSize(10);
|
||||
executor.setMaxPoolSize(20);
|
||||
executor.setQueueCapacity(200);
|
||||
executor.setKeepAliveSeconds(60);
|
||||
executor.setThreadNamePrefix("taskExecutor-");
|
||||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
||||
return executor;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
32
Chapter4-1-5/src/main/java/com/didispace/async/Task.java
Normal file
32
Chapter4-1-5/src/main/java/com/didispace/async/Task.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package com.didispace.async;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.scheduling.annotation.AsyncResult;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @date 16/5/16 下午12:58.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class Task {
|
||||
|
||||
public static Random random = new Random();
|
||||
|
||||
@Async("taskExecutor")
|
||||
public Future<String> run() throws Exception {
|
||||
long sleep = random.nextInt(10000);
|
||||
log.info("开始任务,需耗时:" + sleep + "毫秒");
|
||||
Thread.sleep(sleep);
|
||||
log.info("完成任务");
|
||||
return new AsyncResult<>("test");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.didispace;
|
||||
|
||||
import com.didispace.async.Task;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest
|
||||
public class ApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private Task task;
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
Future<String> futureResult = task.run();
|
||||
String result = futureResult.get(5, TimeUnit.SECONDS);
|
||||
log.info(result);
|
||||
}
|
||||
|
||||
}
|
||||
54
Chapter4-2-6/pom.xml
Normal file
54
Chapter4-2-6/pom.xml
Normal file
@@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.didispace</groupId>
|
||||
<artifactId>demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>demo</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.1.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@RequestMapping(value = "/test", method = RequestMethod.GET)
|
||||
public String testLogLevel() {
|
||||
logger.debug("Logger Level :DEBUG");
|
||||
logger.info("Logger Level :INFO");
|
||||
logger.error("Logger Level :ERROR");
|
||||
return "";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoApplication.class, args);
|
||||
}
|
||||
}
|
||||
1
Chapter4-2-6/src/main/resources/application.properties
Normal file
1
Chapter4-2-6/src/main/resources/application.properties
Normal file
@@ -0,0 +1 @@
|
||||
management.security.enabled=false
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class DemoApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@ spring.datasource.username=root
|
||||
spring.datasource.password=123456
|
||||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
|
||||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
|
||||
spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop
|
||||
spring.jpa.properties.hibernate.show_sql=true
|
||||
|
||||
|
||||
46
Chapter5-2-1/pom.xml
Normal file
46
Chapter5-2-1/pom.xml
Normal file
@@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.didispace</groupId>
|
||||
<artifactId>rabbitmq-hello</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>rabbitmq-hello</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.3.7.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class HelloApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(HelloApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.didispace.rabbit;
|
||||
|
||||
import org.springframework.amqp.core.Queue;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author 翟永超
|
||||
* @create 2016/9/25.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
@Configuration
|
||||
public class RabbitConfig {
|
||||
|
||||
@Bean
|
||||
public Queue helloQueue() {
|
||||
return new Queue("hello");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.didispace.rabbit;
|
||||
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author 翟永超
|
||||
* @create 2016/9/25.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
@Component
|
||||
@RabbitListener(queues = "hello")
|
||||
public class Receiver {
|
||||
|
||||
@RabbitHandler
|
||||
public void process(String hello) {
|
||||
System.out.println("Receiver : " + hello);
|
||||
}
|
||||
|
||||
}
|
||||
22
Chapter5-2-1/src/main/java/com/didispace/rabbit/Sender.java
Normal file
22
Chapter5-2-1/src/main/java/com/didispace/rabbit/Sender.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.didispace.rabbit;
|
||||
|
||||
import org.springframework.amqp.core.AmqpTemplate;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Component
|
||||
public class Sender {
|
||||
|
||||
@Autowired
|
||||
private AmqpTemplate rabbitTemplate;
|
||||
|
||||
public void send() {
|
||||
String context = "hello " + new Date();
|
||||
System.out.println("Sender : " + context);
|
||||
this.rabbitTemplate.convertAndSend("hello", context);
|
||||
}
|
||||
|
||||
}
|
||||
6
Chapter5-2-1/src/main/resources/application.properties
Normal file
6
Chapter5-2-1/src/main/resources/application.properties
Normal file
@@ -0,0 +1,6 @@
|
||||
spring.application.name=rabbitmq-hello
|
||||
|
||||
spring.rabbitmq.host=localhost
|
||||
spring.rabbitmq.port=5672
|
||||
spring.rabbitmq.username=springcloud
|
||||
spring.rabbitmq.password=123456
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.didispace;
|
||||
|
||||
import com.didispace.rabbit.Sender;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = HelloApplication.class)
|
||||
public class HelloApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private Sender sender;
|
||||
|
||||
@Test
|
||||
public void hello() throws Exception {
|
||||
sender.send();
|
||||
}
|
||||
|
||||
}
|
||||
46
Chapter6-1-1/pom.xml
Normal file
46
Chapter6-1-1/pom.xml
Normal file
@@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.didispace</groupId>
|
||||
<artifactId>statemachine</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>statemachine</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.4.2.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.statemachine</groupId>
|
||||
<artifactId>spring-statemachine-core</artifactId>
|
||||
<version>1.2.0.RELEASE</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
26
Chapter6-1-1/src/main/java/com/didispace/Application.java
Normal file
26
Chapter6-1-1/src/main/java/com/didispace/Application.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application implements CommandLineRunner {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private StateMachine<States, Events> stateMachine;
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
stateMachine.start();
|
||||
stateMachine.sendEvent(Events.PAY);
|
||||
stateMachine.sendEvent(Events.RECEIVE);
|
||||
}
|
||||
|
||||
}
|
||||
40
Chapter6-1-1/src/main/java/com/didispace/EventConfig.java
Normal file
40
Chapter6-1-1/src/main/java/com/didispace/EventConfig.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.statemachine.annotation.*;
|
||||
|
||||
/**
|
||||
* 该配置实现了com.didispace.StateMachineConfig类中定义的状态机监听器实现。
|
||||
*/
|
||||
@WithStateMachine
|
||||
public class EventConfig {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@OnTransition(target = "UNPAID")
|
||||
public void create() {
|
||||
logger.info("订单创建,待支付");
|
||||
}
|
||||
|
||||
@OnTransition(source = "UNPAID", target = "WAITING_FOR_RECEIVE")
|
||||
public void pay() {
|
||||
logger.info("用户完成支付,待收货");
|
||||
}
|
||||
|
||||
@OnTransitionStart(source = "UNPAID", target = "WAITING_FOR_RECEIVE")
|
||||
public void payStart() {
|
||||
logger.info("用户完成支付,待收货: start");
|
||||
}
|
||||
|
||||
@OnTransitionEnd(source = "UNPAID", target = "WAITING_FOR_RECEIVE")
|
||||
public void payEnd() {
|
||||
logger.info("用户完成支付,待收货: end");
|
||||
}
|
||||
|
||||
@OnTransition(source = "WAITING_FOR_RECEIVE", target = "DONE")
|
||||
public void receive() {
|
||||
logger.info("用户已收货,订单完成");
|
||||
}
|
||||
|
||||
}
|
||||
6
Chapter6-1-1/src/main/java/com/didispace/Events.java
Normal file
6
Chapter6-1-1/src/main/java/com/didispace/Events.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package com.didispace;
|
||||
|
||||
public enum Events {
|
||||
PAY, // 支付
|
||||
RECEIVE // 收货
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.statemachine.config.EnableStateMachine;
|
||||
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
|
||||
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
|
||||
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
|
||||
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
|
||||
import org.springframework.statemachine.listener.StateMachineListener;
|
||||
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
|
||||
import org.springframework.statemachine.transition.Transition;
|
||||
|
||||
import java.util.EnumSet;
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableStateMachine
|
||||
public class StateMachineConfig extends EnumStateMachineConfigurerAdapter<States, Events> {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineStateConfigurer<States, Events> states)
|
||||
throws Exception {
|
||||
states
|
||||
.withStates()
|
||||
.initial(States.UNPAID)
|
||||
.states(EnumSet.allOf(States.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineTransitionConfigurer<States, Events> transitions)
|
||||
throws Exception {
|
||||
transitions
|
||||
.withExternal()
|
||||
.source(States.UNPAID).target(States.WAITING_FOR_RECEIVE)
|
||||
.event(Events.PAY)
|
||||
.and()
|
||||
.withExternal()
|
||||
.source(States.WAITING_FOR_RECEIVE).target(States.DONE)
|
||||
.event(Events.RECEIVE);
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public void configure(StateMachineConfigurationConfigurer<States, Events> config)
|
||||
// throws Exception {
|
||||
// config
|
||||
// .withConfiguration()
|
||||
// .listener(listener());
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// public StateMachineListener<States, Events> listener() {
|
||||
// return new StateMachineListenerAdapter<States, Events>() {
|
||||
//
|
||||
// @Override
|
||||
// public void transition(Transition<States, Events> transition) {
|
||||
// if(transition.getTarget().getId() == States.UNPAID) {
|
||||
// logger.info("订单创建,待支付");
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// if(transition.getSource().getId() == States.UNPAID
|
||||
// && transition.getTarget().getId() == States.WAITING_FOR_RECEIVE) {
|
||||
// logger.info("用户完成支付,待收货");
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// if(transition.getSource().getId() == States.WAITING_FOR_RECEIVE
|
||||
// && transition.getTarget().getId() == States.DONE) {
|
||||
// logger.info("用户已收货,订单完成");
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// };
|
||||
// }
|
||||
|
||||
}
|
||||
7
Chapter6-1-1/src/main/java/com/didispace/States.java
Normal file
7
Chapter6-1-1/src/main/java/com/didispace/States.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package com.didispace;
|
||||
|
||||
public enum States {
|
||||
UNPAID, // 待支付
|
||||
WAITING_FOR_RECEIVE, // 待收货
|
||||
DONE // 结束
|
||||
}
|
||||
2
Chapter6-1-1/src/main/resources/application.properties
Normal file
2
Chapter6-1-1/src/main/resources/application.properties
Normal file
@@ -0,0 +1,2 @@
|
||||
spring.application.name=statemachine
|
||||
|
||||
69
Chapter6-2-1/pom.xml
Normal file
69
Chapter6-2-1/pom.xml
Normal file
@@ -0,0 +1,69 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.didispace</groupId>
|
||||
<artifactId>Chapter6-2-1</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Chapter6-2-1</name>
|
||||
<description></description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.3.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>pl.project13.maven</groupId>
|
||||
<artifactId>git-commit-id-plugin</artifactId>
|
||||
<version>2.1.15</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>revision</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
22
Chapter6-2-1/src/main/java/com/didispace/Application.java
Normal file
22
Chapter6-2-1/src/main/java/com/didispace/Application.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @blog http://blog.didispace.com
|
||||
*
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
SpringApplication.run(Application.class, args);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.didispace.web;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @blog http://blog.didispace.com
|
||||
*
|
||||
*/
|
||||
@RestController
|
||||
public class HelloController {
|
||||
|
||||
@RequestMapping("/hello")
|
||||
public String index() {
|
||||
return "Hello World";
|
||||
}
|
||||
|
||||
}
|
||||
1
Chapter6-2-1/src/main/resources/application.properties
Normal file
1
Chapter6-2-1/src/main/resources/application.properties
Normal file
@@ -0,0 +1 @@
|
||||
#management.info.git.mode=full
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @blog http://blog.didispace.com
|
||||
*
|
||||
*/
|
||||
@SpringBootTest
|
||||
public class ApplicationTests {
|
||||
|
||||
@Test
|
||||
public void test1() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
61
Chapter9-1-5/api-gateway/pom.xml
Normal file
61
Chapter9-1-5/api-gateway/pom.xml
Normal file
@@ -0,0 +1,61 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.didispace</groupId>
|
||||
<artifactId>api-gateway</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>api-gateway</name>
|
||||
<description>Spring Cloud project</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.3.5.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-zuul</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-eureka</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>Brixton.RELEASE</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.didispace;
|
||||
|
||||
import com.didispace.filter.AccessFilter;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.client.SpringCloudApplication;
|
||||
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@EnableZuulProxy
|
||||
@SpringCloudApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(Application.class).web(true).run(args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AccessFilter accessFilter() {
|
||||
return new AccessFilter();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.didispace.filter;
|
||||
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
public class AccessFilter extends ZuulFilter {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(AccessFilter.class);
|
||||
|
||||
@Override
|
||||
public String filterType() {
|
||||
return "pre";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int filterOrder() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldFilter() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object run() {
|
||||
RequestContext ctx = RequestContext.getCurrentContext();
|
||||
HttpServletRequest request = ctx.getRequest();
|
||||
|
||||
log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));
|
||||
|
||||
Object accessToken = request.getParameter("accessToken");
|
||||
if(accessToken == null) {
|
||||
log.warn("access token is empty");
|
||||
ctx.setSendZuulResponse(false);
|
||||
ctx.setResponseStatusCode(401);
|
||||
return null;
|
||||
}
|
||||
log.info("access token ok");
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
spring.application.name=api-gateway
|
||||
server.port=5555
|
||||
|
||||
# routes to serviceId
|
||||
zuul.routes.api-a.path=/api-a/**
|
||||
zuul.routes.api-a.serviceId=service-A
|
||||
|
||||
zuul.routes.api-b.path=/api-b/**
|
||||
zuul.routes.api-b.serviceId=service-B
|
||||
|
||||
# routes to url
|
||||
zuul.routes.api-a-url.path=/api-a-url/**
|
||||
zuul.routes.api-a-url.url=http://localhost:2222/
|
||||
|
||||
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
|
||||
62
Chapter9-1-5/eureka-server/pom.xml
Normal file
62
Chapter9-1-5/eureka-server/pom.xml
Normal file
@@ -0,0 +1,62 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.didispace</groupId>
|
||||
<artifactId>eureka-server</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>eureka-server</name>
|
||||
<description>Spring Cloud project</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.3.5.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-eureka-server</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>Brixton.RELEASE</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
|
||||
|
||||
@EnableEurekaServer
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(Application.class).web(true).run(args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
server.port=1111
|
||||
#eureka.instance.hostname=localhost
|
||||
|
||||
eureka.client.register-with-eureka=false
|
||||
eureka.client.fetch-registry=false
|
||||
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
|
||||
61
Chapter9-1-5/service-A/pom.xml
Normal file
61
Chapter9-1-5/service-A/pom.xml
Normal file
@@ -0,0 +1,61 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.didispace</groupId>
|
||||
<artifactId>service-A</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>service-A</name>
|
||||
<description>Spring Cloud project</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.3.5.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-eureka</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>Brixton.RELEASE</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
@EnableDiscoveryClient
|
||||
@SpringBootApplication
|
||||
public class ComputeServiceApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(ComputeServiceApplication.class).web(true).run(args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.didispace.web;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class ComputeController {
|
||||
|
||||
private final Logger logger = Logger.getLogger(getClass());
|
||||
|
||||
@Autowired
|
||||
private DiscoveryClient client;
|
||||
|
||||
@RequestMapping(value = "/add" ,method = RequestMethod.GET)
|
||||
public String add(@RequestParam Integer a, @RequestParam Integer b) {
|
||||
ServiceInstance instance = client.getLocalServiceInstance();
|
||||
Integer r = a + b;
|
||||
logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
|
||||
return "From Service-A, Result is " + r;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
spring.application.name=service-A
|
||||
|
||||
server.port=2222
|
||||
|
||||
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.didispace;
|
||||
|
||||
import com.didispace.web.HelloController;
|
||||
import com.didispace.web.ComputeController;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -27,14 +27,11 @@ public class ApplicationTests {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
|
||||
mvc = MockMvcBuilders.standaloneSetup(new ComputeController()).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getHello() throws Exception {
|
||||
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(equalTo("Hello World")));
|
||||
}
|
||||
|
||||
}
|
||||
61
Chapter9-1-5/service-B/pom.xml
Normal file
61
Chapter9-1-5/service-B/pom.xml
Normal file
@@ -0,0 +1,61 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.didispace</groupId>
|
||||
<artifactId>service-B</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>service-B</name>
|
||||
<description>Spring Cloud project</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.3.5.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-eureka</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>Brixton.RELEASE</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
@EnableDiscoveryClient
|
||||
@SpringBootApplication
|
||||
public class ComputeServiceApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(ComputeServiceApplication.class).web(true).run(args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.didispace.web;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class ComputeController {
|
||||
|
||||
private final Logger logger = Logger.getLogger(getClass());
|
||||
|
||||
@Autowired
|
||||
private DiscoveryClient client;
|
||||
|
||||
@RequestMapping(value = "/add" ,method = RequestMethod.GET)
|
||||
public String add(@RequestParam Integer a, @RequestParam Integer b) {
|
||||
ServiceInstance instance = client.getLocalServiceInstance();
|
||||
Integer r = a + b;
|
||||
logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
|
||||
return "From Service-B, Result is " + r;
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user