From f4b287b84abe051738566dcab6b36e5a61ed8210 Mon Sep 17 00:00:00 2001 From: esensar Date: Sun, 16 Apr 2017 15:18:22 +0200 Subject: [PATCH] User service discovery for getting external service urls --- config | 2 +- .../src/main/resources/application.properties | 1 + .../steleks/controller/EventController.java | 22 +++++-- .../src/main/resources/bootstrap.properties | 2 +- teams/build.gradle | 4 +- teams/src/main/resources/bootstrap.properties | 5 ++ users/build.gradle | 4 +- .../steleks/repository/RestErrorHandler.java | 57 ------------------- users/src/main/resources/bootstrap.properties | 5 ++ 9 files changed, 36 insertions(+), 66 deletions(-) create mode 100644 teams/src/main/resources/bootstrap.properties delete mode 100644 users/src/main/java/ba/steleks/repository/RestErrorHandler.java create mode 100644 users/src/main/resources/bootstrap.properties diff --git a/config b/config index c02e79c..75d72c5 160000 --- a/config +++ b/config @@ -1 +1 @@ -Subproject commit c02e79c9aad0a236f1549e849233f9b9193a025f +Subproject commit 75d72c5a3bcd442d37b756e7e34fa9e4a25b84d6 diff --git a/config_server/src/main/resources/application.properties b/config_server/src/main/resources/application.properties index 953d9a5..ee8bc91 100644 --- a/config_server/src/main/resources/application.properties +++ b/config_server/src/main/resources/application.properties @@ -7,3 +7,4 @@ encrypt.key-store.location=classpath:config-server.jks encrypt.key-store.password=my-s70r3-s3cr3t encrypt.key-store.alias=config-server-key encrypt.key-store.secret=my-k34-s3cr3t +message=Djes dizda levatu \ No newline at end of file diff --git a/events/src/main/java/ba/steleks/controller/EventController.java b/events/src/main/java/ba/steleks/controller/EventController.java index c26a877..e561fc9 100644 --- a/events/src/main/java/ba/steleks/controller/EventController.java +++ b/events/src/main/java/ba/steleks/controller/EventController.java @@ -5,6 +5,8 @@ import ba.steleks.repository.EventsJpaRepository; import ba.steleks.model.Event; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.data.rest.webmvc.RepositoryRestController; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -16,6 +18,7 @@ import org.springframework.web.client.RestTemplate; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import java.net.URI; +import java.util.List; /** * Created by admin on 01/04/2017. @@ -28,19 +31,28 @@ public class EventController { private RestTemplate restTemplate; + private DiscoveryClient discoveryClient; + @Autowired - public EventController(RestTemplateBuilder restTemplateBuilder, EventsJpaRepository repository) { - this.restTemplate = restTemplateBuilder.build(); + public EventController(EventsJpaRepository repository, RestTemplateBuilder restTemplateBuilder, DiscoveryClient discoveryClient) { this.repository = repository; + this.restTemplate = restTemplateBuilder.build(); + this.discoveryClient = discoveryClient; } @RequestMapping(path = "/events", method = RequestMethod.POST) public ResponseEntity add(@RequestBody Event event) throws ExternalServiceException { - String oviUseriNeki = "http://localhost:8090/users/{id}"; - try { + List usersInstances = discoveryClient.getInstances("users"); + if(usersInstances == null || usersInstances.size() == 0) { + System.err.print("Users service not found!"); + throw new ExternalServiceException(); + } - String response = restTemplate.getForObject(oviUseriNeki, String.class, event.getCreatedById()); + ServiceInstance usersService = usersInstances.get(0); + String usersServiceBase = usersService.getUri().toString(); + try { + String response = restTemplate.getForObject(usersServiceBase + "/users/{id}", String.class, event.getCreatedById()); Event result = repository.save(event); URI location = ServletUriComponentsBuilder .fromCurrentRequest().path("/{id}") diff --git a/events/src/main/resources/bootstrap.properties b/events/src/main/resources/bootstrap.properties index cf02ab0..404808c 100644 --- a/events/src/main/resources/bootstrap.properties +++ b/events/src/main/resources/bootstrap.properties @@ -1,4 +1,4 @@ -spring.application.name=steleks +spring.application.name=events # N.B. this is the default: spring.cloud.config.uri=http://localhost:8888 spring.cloud.config.username=root diff --git a/teams/build.gradle b/teams/build.gradle index 000e6fb..88bc271 100644 --- a/teams/build.gradle +++ b/teams/build.gradle @@ -31,9 +31,11 @@ dependencies { compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot-starter-data-rest') runtime('com.h2database:h2') + compile('org.springframework.cloud:spring-cloud-starter-config') + compile('org.springframework.boot:spring-boot-starter-actuator') testCompile('org.springframework.boot:spring-boot-starter-test') compile('mysql:mysql-connector-java') compile('org.springframework.cloud:spring-cloud-starter-eureka') - compile(':common') + compile project(':common') testCompile('org.springframework.cloud:spring-cloud-starter-eureka-server') } diff --git a/teams/src/main/resources/bootstrap.properties b/teams/src/main/resources/bootstrap.properties new file mode 100644 index 0000000..3576461 --- /dev/null +++ b/teams/src/main/resources/bootstrap.properties @@ -0,0 +1,5 @@ +spring.application.name=teams +# N.B. this is the default: +spring.cloud.config.uri=http://localhost:8888 +spring.cloud.config.username=root +spring.cloud.config.password=root \ No newline at end of file diff --git a/users/build.gradle b/users/build.gradle index b6ff1cc..bfef237 100644 --- a/users/build.gradle +++ b/users/build.gradle @@ -31,11 +31,13 @@ dependencies { compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot-starter-data-rest') runtime('com.h2database:h2') + compile('org.springframework.cloud:spring-cloud-starter-config') + compile('org.springframework.boot:spring-boot-starter-actuator') testCompile('org.springframework.boot:spring-boot-starter-test') compile('mysql:mysql-connector-java') compile('org.hibernate:hibernate-validator') compile('org.springframework.cloud:spring-cloud-starter-eureka') - compile(':common') + compile project(':common') testCompile('org.springframework.cloud:spring-cloud-starter-eureka-server') } diff --git a/users/src/main/java/ba/steleks/repository/RestErrorHandler.java b/users/src/main/java/ba/steleks/repository/RestErrorHandler.java deleted file mode 100644 index 57f33b1..0000000 --- a/users/src/main/java/ba/steleks/repository/RestErrorHandler.java +++ /dev/null @@ -1,57 +0,0 @@ -package ba.steleks.repository; - -import com.fasterxml.jackson.databind.util.JSONPObject; -import jdk.nashorn.internal.runtime.JSONFunctions; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.context.MessageSource; -import org.springframework.context.i18n.LocaleContextHolder; -import org.springframework.data.rest.webmvc.support.RepositoryConstraintViolationExceptionMessage; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.validation.BindingResult; -import org.springframework.validation.FieldError; -import org.springframework.web.bind.MethodArgumentNotValidException; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.client.HttpStatusCodeException; -import org.springframework.web.servlet.ModelAndView; -import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; - -import java.util.HashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; - -/** - * Created by admin on 01/04/2017. - */ -@ControllerAdvice -public class RestErrorHandler extends ResponseEntityExceptionHandler { - - @ExceptionHandler(javax.validation.ConstraintViolationException.class) - @ResponseStatus(HttpStatus.BAD_REQUEST) - @ResponseBody - public Map handleCustomException(javax.validation.ConstraintViolationException ex) { - - Map map= new HashMap(); - map.put("status", HttpStatus.BAD_REQUEST.toString()); - map.put("error", ex.getLocalizedMessage()); - - return map; - - } - - @ExceptionHandler(HttpStatusCodeException.class) - @ResponseBody - public ResponseEntity> handleAllExceptions(HttpStatusCodeException ex) { - Map map= new HashMap(); - map.put("status", ex.getStatusCode().toString()); - map.put("error", ex.getLocalizedMessage()); - - return ResponseEntity.status(ex.getStatusCode()).body(map); - } - -} diff --git a/users/src/main/resources/bootstrap.properties b/users/src/main/resources/bootstrap.properties new file mode 100644 index 0000000..be741b1 --- /dev/null +++ b/users/src/main/resources/bootstrap.properties @@ -0,0 +1,5 @@ +spring.application.name=users +# N.B. this is the default: +spring.cloud.config.uri=http://localhost:8888 +spring.cloud.config.username=root +spring.cloud.config.password=root \ No newline at end of file