Commit 5c42b3fb authored by Benjamin Wilms's avatar Benjamin Wilms

dist cache example

parent 22b36c7a
......@@ -45,6 +45,17 @@
<version>8.0.8-dmr</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
......
......@@ -5,7 +5,7 @@ package de.codecentric.resilience.redis;
*/
import de.codecentric.resilience.redis.domain.User;
import de.codecentric.resilience.redis.service.UserService;
import de.codecentric.resilience.redis.domain.UserRepository;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -14,14 +14,16 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import java.util.stream.Stream;
@EnableCaching
@SpringBootApplication
public class UserServiceApplication implements CommandLineRunner{
public class UserServiceApplication implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass());
@Autowired
private UserService userService;
private UserRepository userRepository;
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
......@@ -29,11 +31,11 @@ public class UserServiceApplication implements CommandLineRunner{
@Override
public void run(String... strings) throws Exception {
User user = new User("Bob","Miller");
User user1 = userService.createUser(user);
logger.info(user1);
Stream.of(new User("Bob", "Miller"), new User("John", "Doe"), new User("Theresa", "Gee"))
.forEach(user -> {
userRepository.save(user);
logger.info(user);
});
}
}
\ No newline at end of file
package de.codecentric.resilience.redis.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author Benjamin Wilms
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
......@@ -12,6 +12,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
......@@ -40,6 +41,13 @@ class UserRestController {
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
@GetMapping()
ResponseEntity<List<User>> get() {
return Optional.ofNullable(userService.getAllUser())
.map(result -> new ResponseEntity<>(result, HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
@PutMapping("/{id}")
ResponseEntity updateUser(@PathVariable String id, @RequestBody User user) {
Assert.notNull(user, "the user can't be null!");
......
......@@ -14,6 +14,8 @@ import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
......@@ -45,6 +47,12 @@ public class UserService {
return this.userRepository.findOne(id);
}
public List<User> getAllUser() {
logger.info("Loading user from database");
return this.userRepository.findAll();
}
private void latency() {
try {
logger.info("add latency 3s");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment