User service discovery for getting external service urls
parent
17b8179b5f
commit
f4b287b84a
2
config
2
config
|
@ -1 +1 @@
|
|||
Subproject commit c02e79c9aad0a236f1549e849233f9b9193a025f
|
||||
Subproject commit 75d72c5a3bcd442d37b756e7e34fa9e4a25b84d6
|
|
@ -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
|
|
@ -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<ServiceInstance> 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}")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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')
|
||||
}
|
||||
|
|
|
@ -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
|
|
@ -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')
|
||||
|
||||
}
|
||||
|
|
|
@ -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<String, String> handleCustomException(javax.validation.ConstraintViolationException ex) {
|
||||
|
||||
Map<String, String> map= new HashMap<String, String>();
|
||||
map.put("status", HttpStatus.BAD_REQUEST.toString());
|
||||
map.put("error", ex.getLocalizedMessage());
|
||||
|
||||
return map;
|
||||
|
||||
}
|
||||
|
||||
@ExceptionHandler(HttpStatusCodeException.class)
|
||||
@ResponseBody
|
||||
public ResponseEntity<Map<String, String>> handleAllExceptions(HttpStatusCodeException ex) {
|
||||
Map<String, String> map= new HashMap<String, String>();
|
||||
map.put("status", ex.getStatusCode().toString());
|
||||
map.put("error", ex.getLocalizedMessage());
|
||||
|
||||
return ResponseEntity.status(ex.getStatusCode()).body(map);
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
Reference in New Issue