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

dist cache example

parent 22b36c7a
...@@ -45,6 +45,17 @@ ...@@ -45,6 +45,17 @@
<version>8.0.8-dmr</version> <version>8.0.8-dmr</version>
</dependency> </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> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
......
...@@ -5,7 +5,7 @@ package de.codecentric.resilience.redis; ...@@ -5,7 +5,7 @@ package de.codecentric.resilience.redis;
*/ */
import de.codecentric.resilience.redis.domain.User; 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -14,14 +14,16 @@ import org.springframework.boot.SpringApplication; ...@@ -14,14 +14,16 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.annotation.EnableCaching;
import java.util.stream.Stream;
@EnableCaching @EnableCaching
@SpringBootApplication @SpringBootApplication
public class UserServiceApplication implements CommandLineRunner{ public class UserServiceApplication implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass()); protected final Log logger = LogFactory.getLog(getClass());
@Autowired @Autowired
private UserService userService; private UserRepository userRepository;
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args); SpringApplication.run(UserServiceApplication.class, args);
...@@ -29,11 +31,11 @@ public class UserServiceApplication implements CommandLineRunner{ ...@@ -29,11 +31,11 @@ public class UserServiceApplication implements CommandLineRunner{
@Override @Override
public void run(String... strings) throws Exception { public void run(String... strings) throws Exception {
Stream.of(new User("Bob", "Miller"), new User("John", "Doe"), new User("Theresa", "Gee"))
User user = new User("Bob","Miller"); .forEach(user -> {
userRepository.save(user);
User user1 = userService.createUser(user); logger.info(user);
logger.info(user1); });
} }
} }
\ 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; ...@@ -12,6 +12,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional; import java.util.Optional;
@RestController @RestController
...@@ -40,6 +41,13 @@ class UserRestController { ...@@ -40,6 +41,13 @@ class UserRestController {
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND)); .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}") @PutMapping("/{id}")
ResponseEntity updateUser(@PathVariable String id, @RequestBody User user) { ResponseEntity updateUser(@PathVariable String id, @RequestBody User user) {
Assert.notNull(user, "the user can't be null!"); Assert.notNull(user, "the user can't be null!");
......
...@@ -14,6 +14,8 @@ import org.springframework.cache.annotation.CachePut; ...@@ -14,6 +14,8 @@ import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
@Service @Service
public class UserService { public class UserService {
...@@ -45,6 +47,12 @@ public class UserService { ...@@ -45,6 +47,12 @@ public class UserService {
return this.userRepository.findOne(id); return this.userRepository.findOne(id);
} }
public List<User> getAllUser() {
logger.info("Loading user from database");
return this.userRepository.findAll();
}
private void latency() { private void latency() {
try { try {
logger.info("add latency 3s"); 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