Use service discovery for event team and media controllers

master
esensar 2017-04-16 17:30:13 +02:00
parent 29ea33cd0a
commit 5d2fbc0453
2 changed files with 17 additions and 8 deletions

View File

@ -5,6 +5,8 @@ import ba.steleks.model.Event;
import ba.steleks.model.EventTeam; import ba.steleks.model.EventTeam;
import ba.steleks.repository.EventTeamJpaRepository; import ba.steleks.repository.EventTeamJpaRepository;
import ba.steleks.repository.EventsJpaRepository; import ba.steleks.repository.EventsJpaRepository;
import ba.steleks.service.Service;
import ba.steleks.service.discovery.ServiceDiscoveryClient;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.data.rest.webmvc.RepositoryRestController; import org.springframework.data.rest.webmvc.RepositoryRestController;
@ -31,19 +33,22 @@ public class EventTeamController {
private RestTemplate restTemplate; private RestTemplate restTemplate;
private ServiceDiscoveryClient discoveryClient;
@Autowired @Autowired
public EventTeamController(RestTemplateBuilder restTemplateBuilder, EventTeamJpaRepository repository) { public EventTeamController(EventTeamJpaRepository repository, RestTemplateBuilder restTemplateBuilder, ServiceDiscoveryClient discoveryClient) {
this.restTemplate = restTemplateBuilder.build();
this.repository = repository; this.repository = repository;
this.restTemplate = restTemplateBuilder.build();
this.discoveryClient = discoveryClient;
} }
@RequestMapping(path = "/eventTeams", method = RequestMethod.POST) @RequestMapping(path = "/eventTeams", method = RequestMethod.POST)
public ResponseEntity<?> add(@RequestBody EventTeam eventTeam) throws ExternalServiceException { public ResponseEntity<?> add(@RequestBody EventTeam eventTeam) throws ExternalServiceException {
String oviTimoviNeki = "http://localhost:9010/teams/{id}"; String teamsServiceBase = discoveryClient.getServiceUrl(Service.TEAMS);
try { try {
String response = restTemplate.getForObject(oviTimoviNeki, String.class, eventTeam.getTeamId()); String response = restTemplate.getForObject(teamsServiceBase + "/teams/{id}", String.class, eventTeam.getTeamId());
EventTeam result = repository.save(eventTeam); EventTeam result = repository.save(eventTeam);
URI location = ServletUriComponentsBuilder URI location = ServletUriComponentsBuilder
.fromCurrentRequest().path("/{id}") .fromCurrentRequest().path("/{id}")

View File

@ -6,6 +6,8 @@ import ba.steleks.error.exception.ExternalServiceException;
import ba.steleks.model.Event; import ba.steleks.model.Event;
import ba.steleks.model.Media; import ba.steleks.model.Media;
import ba.steleks.repository.MediaJpaRepository; import ba.steleks.repository.MediaJpaRepository;
import ba.steleks.service.Service;
import ba.steleks.service.discovery.ServiceDiscoveryClient;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.data.rest.webmvc.RepositoryRestController; import org.springframework.data.rest.webmvc.RepositoryRestController;
@ -25,19 +27,21 @@ public class MediaController {
private MediaJpaRepository repository; private MediaJpaRepository repository;
private RestTemplate restTemplate; private RestTemplate restTemplate;
private ServiceDiscoveryClient discoveryClient;
@Autowired @Autowired
public MediaController(RestTemplateBuilder restTemplateBuilder, MediaJpaRepository repository) { public MediaController(MediaJpaRepository repository, RestTemplate restTemplate, ServiceDiscoveryClient discoveryClient) {
this.repository = repository; this.repository = repository;
this.restTemplate = restTemplateBuilder.build(); this.restTemplate = restTemplate;
this.discoveryClient = discoveryClient;
} }
@RequestMapping(path = "/medias", method = RequestMethod.POST) @RequestMapping(path = "/medias", method = RequestMethod.POST)
public ResponseEntity<?> add(@RequestBody Media media) throws ExternalServiceException { public ResponseEntity<?> add(@RequestBody Media media) throws ExternalServiceException {
String oviUseriNeki = "http://localhost:8090/users/{id}"; String usersServiceBase = discoveryClient.getServiceUrl(Service.USERS);
try { try {
String response = restTemplate.getForObject(oviUseriNeki, String.class, media.getCreatedById()); String response = restTemplate.getForObject(usersServiceBase + "/users/{id}", String.class, media.getCreatedById());
Media result = repository.save(media); Media result = repository.save(media);
URI location = ServletUriComponentsBuilder URI location = ServletUriComponentsBuilder
.fromCurrentRequest().path("/{id}") .fromCurrentRequest().path("/{id}")