From 5d2fbc04538ca1d49a1cacb16b6159e88f2a5351 Mon Sep 17 00:00:00 2001 From: esensar Date: Sun, 16 Apr 2017 17:30:13 +0200 Subject: [PATCH 1/2] Use service discovery for event team and media controllers --- .../ba/steleks/controller/EventTeamController.java | 13 +++++++++---- .../java/ba/steleks/controller/MediaController.java | 12 ++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/events/src/main/java/ba/steleks/controller/EventTeamController.java b/events/src/main/java/ba/steleks/controller/EventTeamController.java index 96c152d..d9e91d9 100644 --- a/events/src/main/java/ba/steleks/controller/EventTeamController.java +++ b/events/src/main/java/ba/steleks/controller/EventTeamController.java @@ -5,6 +5,8 @@ import ba.steleks.model.Event; import ba.steleks.model.EventTeam; import ba.steleks.repository.EventTeamJpaRepository; 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.boot.web.client.RestTemplateBuilder; import org.springframework.data.rest.webmvc.RepositoryRestController; @@ -31,19 +33,22 @@ public class EventTeamController { private RestTemplate restTemplate; + private ServiceDiscoveryClient discoveryClient; + @Autowired - public EventTeamController(RestTemplateBuilder restTemplateBuilder, EventTeamJpaRepository repository) { - this.restTemplate = restTemplateBuilder.build(); + public EventTeamController(EventTeamJpaRepository repository, RestTemplateBuilder restTemplateBuilder, ServiceDiscoveryClient discoveryClient) { this.repository = repository; + this.restTemplate = restTemplateBuilder.build(); + this.discoveryClient = discoveryClient; } @RequestMapping(path = "/eventTeams", method = RequestMethod.POST) public ResponseEntity add(@RequestBody EventTeam eventTeam) throws ExternalServiceException { - String oviTimoviNeki = "http://localhost:9010/teams/{id}"; + String teamsServiceBase = discoveryClient.getServiceUrl(Service.TEAMS); 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); URI location = ServletUriComponentsBuilder .fromCurrentRequest().path("/{id}") diff --git a/events/src/main/java/ba/steleks/controller/MediaController.java b/events/src/main/java/ba/steleks/controller/MediaController.java index 9ed1308..f2f886f 100644 --- a/events/src/main/java/ba/steleks/controller/MediaController.java +++ b/events/src/main/java/ba/steleks/controller/MediaController.java @@ -6,6 +6,8 @@ import ba.steleks.error.exception.ExternalServiceException; import ba.steleks.model.Event; import ba.steleks.model.Media; 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.boot.web.client.RestTemplateBuilder; import org.springframework.data.rest.webmvc.RepositoryRestController; @@ -25,19 +27,21 @@ public class MediaController { private MediaJpaRepository repository; private RestTemplate restTemplate; + private ServiceDiscoveryClient discoveryClient; @Autowired - public MediaController(RestTemplateBuilder restTemplateBuilder, MediaJpaRepository repository) { + public MediaController(MediaJpaRepository repository, RestTemplate restTemplate, ServiceDiscoveryClient discoveryClient) { this.repository = repository; - this.restTemplate = restTemplateBuilder.build(); + this.restTemplate = restTemplate; + this.discoveryClient = discoveryClient; } @RequestMapping(path = "/medias", method = RequestMethod.POST) public ResponseEntity add(@RequestBody Media media) throws ExternalServiceException { - String oviUseriNeki = "http://localhost:8090/users/{id}"; + String usersServiceBase = discoveryClient.getServiceUrl(Service.USERS); 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); URI location = ServletUriComponentsBuilder .fromCurrentRequest().path("/{id}") From f170b57ea5b0eabab4b3aae66d40a42810eded1f Mon Sep 17 00:00:00 2001 From: esensar Date: Sun, 16 Apr 2017 17:53:15 +0200 Subject: [PATCH 2/2] Fix media controller rest template --- config | 2 +- .../src/main/java/ba/steleks/controller/MediaController.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config b/config index 0d02814..75a784b 160000 --- a/config +++ b/config @@ -1 +1 @@ -Subproject commit 0d028147e90627b077c5435b557d6b3d7d19623c +Subproject commit 75a784ba6ed8f6e70ee91808d3752443e3a04d1d diff --git a/events/src/main/java/ba/steleks/controller/MediaController.java b/events/src/main/java/ba/steleks/controller/MediaController.java index f2f886f..09cf5e1 100644 --- a/events/src/main/java/ba/steleks/controller/MediaController.java +++ b/events/src/main/java/ba/steleks/controller/MediaController.java @@ -30,9 +30,9 @@ public class MediaController { private ServiceDiscoveryClient discoveryClient; @Autowired - public MediaController(MediaJpaRepository repository, RestTemplate restTemplate, ServiceDiscoveryClient discoveryClient) { + public MediaController(MediaJpaRepository repository, RestTemplateBuilder restTemplateBuilder, ServiceDiscoveryClient discoveryClient) { this.repository = repository; - this.restTemplate = restTemplate; + this.restTemplate = restTemplateBuilder.build(); this.discoveryClient = discoveryClient; }