diff --git a/common/build.gradle b/common/build.gradle index 419b462..4cefa0b 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -7,6 +7,7 @@ buildscript { } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") + classpath('io.spring.gradle:dependency-management-plugin:0.5.4.RELEASE') } } @@ -27,5 +28,13 @@ dependencies { compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot-starter-data-rest') compile('org.springframework.boot:spring-boot-starter-web') + compile('org.springframework.cloud:spring-cloud-starter-eureka') + testCompile('org.springframework.cloud:spring-cloud-starter-eureka-server') testCompile('rg.springframework.boot:spring-boot-starter-test') } + +dependencyManagement { + imports { + mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR5" + } +} diff --git a/common/src/main/java/ba/steleks/service/DefaultServiceIdProvider.java b/common/src/main/java/ba/steleks/service/DefaultServiceIdProvider.java new file mode 100644 index 0000000..834121f --- /dev/null +++ b/common/src/main/java/ba/steleks/service/DefaultServiceIdProvider.java @@ -0,0 +1,41 @@ +package ba.steleks.service;/** + * Created by ensar on 16/04/17. + */ + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Component; + +import java.util.logging.Logger; + +@Primary +@Component +@RefreshScope +public class DefaultServiceIdProvider implements ServiceIdProvider { + private static final Logger logger = + Logger.getLogger(DefaultServiceIdProvider.class.getName()); + + public static final String NO_SERVICE = "NO_SERVICE"; + + @Value("${users.name}") + private String usersName; + @Value("${events.name}") + private String eventsName; + @Value("${teams.name}") + private String teamsName; + + @Override + public String getServiceId(Service service) { + switch (service) { + case USERS: + return usersName; + case TEAMS: + return teamsName; + case EVENTS: + return eventsName; + default: + return NO_SERVICE; + } + } +} diff --git a/common/src/main/java/ba/steleks/service/Service.java b/common/src/main/java/ba/steleks/service/Service.java new file mode 100644 index 0000000..a082ab7 --- /dev/null +++ b/common/src/main/java/ba/steleks/service/Service.java @@ -0,0 +1,9 @@ +package ba.steleks.service;/** + * Created by ensar on 16/04/17. + */ + +public enum Service { + USERS, + EVENTS, + TEAMS; +} diff --git a/common/src/main/java/ba/steleks/service/ServiceConstants.java b/common/src/main/java/ba/steleks/service/ServiceConstants.java new file mode 100644 index 0000000..12164bd --- /dev/null +++ b/common/src/main/java/ba/steleks/service/ServiceConstants.java @@ -0,0 +1,24 @@ +package ba.steleks.service; + +/** + * Created by ensar on 16/04/17. + */ + +import org.springframework.beans.factory.annotation.Value; + +import java.util.logging.Logger; + +public class ServiceConstants { + private static final Logger logger = + Logger.getLogger(ServiceConstants.class.getName()); + + static { + } + + @Value("${users.name}") + public static String USERS_SERVICE_NAME; + @Value("${events.name}") + public static String EVENTS_SERVICE_NAME; + @Value("${teams.name}") + public static String TEAMS_SERVICE_NAME; +} diff --git a/common/src/main/java/ba/steleks/service/ServiceIdProvider.java b/common/src/main/java/ba/steleks/service/ServiceIdProvider.java new file mode 100644 index 0000000..0e92fc1 --- /dev/null +++ b/common/src/main/java/ba/steleks/service/ServiceIdProvider.java @@ -0,0 +1,7 @@ +package ba.steleks.service;/** + * Created by ensar on 16/04/17. + */ + +public interface ServiceIdProvider { + String getServiceId(Service service); +} diff --git a/common/src/main/java/ba/steleks/service/discovery/EurekaServiceDiscoveryClient.java b/common/src/main/java/ba/steleks/service/discovery/EurekaServiceDiscoveryClient.java new file mode 100644 index 0000000..88ebedc --- /dev/null +++ b/common/src/main/java/ba/steleks/service/discovery/EurekaServiceDiscoveryClient.java @@ -0,0 +1,49 @@ +package ba.steleks.service.discovery; + +/** + * Created by ensar on 16/04/17. + */ + +import ba.steleks.error.exception.ExternalServiceException; +import ba.steleks.service.Service; +import ba.steleks.service.ServiceIdProvider; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.logging.Logger; + +@Primary +@Component +public class EurekaServiceDiscoveryClient implements ServiceDiscoveryClient { + private static final Logger logger = + Logger.getLogger(EurekaServiceDiscoveryClient.class.getName()); + + private DiscoveryClient discoveryClient; + private ServiceIdProvider serviceIdProvider; + + @Autowired + public EurekaServiceDiscoveryClient(DiscoveryClient discoveryClient, ServiceIdProvider serviceIdProvider) { + this.discoveryClient = discoveryClient; + this.serviceIdProvider = serviceIdProvider; + } + + @Override + public String getServiceUrl(Service service) throws ExternalServiceException { + String serviceId = serviceIdProvider.getServiceId(service); + List usersInstances = discoveryClient.getInstances(serviceId); + if(usersInstances == null || usersInstances.size() == 0) { + System.err.print(service.toString() + " service not found!"); + throw new ExternalServiceException(); + } + + ServiceInstance serviceInstance = usersInstances.get(0); + if(serviceInstance == null) { + throw new ExternalServiceException(); + } + return serviceInstance.getUri().toString(); + } +} diff --git a/common/src/main/java/ba/steleks/service/discovery/ServiceDiscoveryClient.java b/common/src/main/java/ba/steleks/service/discovery/ServiceDiscoveryClient.java new file mode 100644 index 0000000..448ae86 --- /dev/null +++ b/common/src/main/java/ba/steleks/service/discovery/ServiceDiscoveryClient.java @@ -0,0 +1,12 @@ +package ba.steleks.service.discovery; + +import ba.steleks.error.exception.ExternalServiceException; +import ba.steleks.service.Service; + +/** + * Created by ensar on 16/04/17. + */ +public interface ServiceDiscoveryClient { + + String getServiceUrl(Service service) throws ExternalServiceException; +} diff --git a/config b/config index 75d72c5..0d02814 160000 --- a/config +++ b/config @@ -1 +1 @@ -Subproject commit 75d72c5a3bcd442d37b756e7e34fa9e4a25b84d6 +Subproject commit 0d028147e90627b077c5435b557d6b3d7d19623c diff --git a/config_server/src/main/java/ba/steleks/ConfigServerApplication.java b/config_server/src/main/java/ba/steleks/ConfigServerApplication.java index 3b7599e..ed12c96 100644 --- a/config_server/src/main/java/ba/steleks/ConfigServerApplication.java +++ b/config_server/src/main/java/ba/steleks/ConfigServerApplication.java @@ -1,10 +1,14 @@ package ba.steleks; import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; +import org.springframework.context.annotation.Configuration; +@Configuration @EnableConfigServer +@EnableAutoConfiguration @SpringBootApplication public class ConfigServerApplication { diff --git a/config_server/src/main/resources/application.properties b/config_server/src/main/resources/application.properties index ee8bc91..0594c6f 100644 --- a/config_server/src/main/resources/application.properties +++ b/config_server/src/main/resources/application.properties @@ -6,5 +6,4 @@ security.user.password=root 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 +encrypt.key-store.secret=my-k34-s3cr3t \ No newline at end of file diff --git a/eureka-service/build.gradle b/eureka-service/build.gradle index db2fd6c..03798a1 100644 --- a/eureka-service/build.gradle +++ b/eureka-service/build.gradle @@ -29,5 +29,7 @@ dependencyManagement { dependencies { compile('org.springframework.cloud:spring-cloud-starter-eureka-server') + compile('org.springframework.cloud:spring-cloud-starter-config') + compile('org.springframework.boot:spring-boot-starter-actuator') testCompile('org.springframework.boot:spring-boot-starter-test') } diff --git a/eureka-service/src/main/resources/application.properties b/eureka-service/src/main/resources/application.properties deleted file mode 100644 index 4944e79..0000000 --- a/eureka-service/src/main/resources/application.properties +++ /dev/null @@ -1,7 +0,0 @@ -server.port=8761 - -eureka.client.register-with-eureka=false -eureka.client.fetch-registry=false - -logging.level.com.netflix.eureka=OFF -logging.level.com.netflix.discovery=OFF \ No newline at end of file diff --git a/eureka-service/src/main/resources/bootstrap.properties b/eureka-service/src/main/resources/bootstrap.properties new file mode 100644 index 0000000..3f11de6 --- /dev/null +++ b/eureka-service/src/main/resources/bootstrap.properties @@ -0,0 +1,5 @@ +spring.application.name=eureka-service +# 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/events/src/main/java/ba/steleks/EventsApplication.java b/events/src/main/java/ba/steleks/EventsApplication.java index 2f15b6b..a047ca1 100644 --- a/events/src/main/java/ba/steleks/EventsApplication.java +++ b/events/src/main/java/ba/steleks/EventsApplication.java @@ -1,5 +1,6 @@ package ba.steleks; +import ba.steleks.service.ServiceConstants; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; @@ -24,34 +25,6 @@ public class EventsApplication { } } -@RefreshScope -@RestController -class MessageRestController { - - @Value("${message:Hello default}") - private String message; - - @Value("${user.password}") - private String password; - - @RequestMapping("/message") - String getMessage() { - return this.message; - } - - //@RequestMapping(path ="/temp", value = "/{id}", method = RequestMethod.GET) - //String getTemp(@PathVariable("id") Long id){ - // return "temp " + Long.toString(id); - //} - - @RequestMapping(value = "/whoami/{username}", method = RequestMethod.GET) - public String whoami(@PathVariable("username") String username) { - return String.format("Hello! You're %s and you'll become a(n) root, " + - "but only if your password is '%s'!\n", - username, password); - } -} - @RestController class ServiceInstanceRestController { diff --git a/events/src/main/java/ba/steleks/controller/EventController.java b/events/src/main/java/ba/steleks/controller/EventController.java index e561fc9..6edf6d0 100644 --- a/events/src/main/java/ba/steleks/controller/EventController.java +++ b/events/src/main/java/ba/steleks/controller/EventController.java @@ -1,12 +1,12 @@ package ba.steleks.controller; +import ba.steleks.service.Service; +import ba.steleks.service.discovery.ServiceDiscoveryClient; import ba.steleks.error.exception.ExternalServiceException; 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; @@ -18,7 +18,6 @@ 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. @@ -31,10 +30,10 @@ public class EventController { private RestTemplate restTemplate; - private DiscoveryClient discoveryClient; + private ServiceDiscoveryClient discoveryClient; @Autowired - public EventController(EventsJpaRepository repository, RestTemplateBuilder restTemplateBuilder, DiscoveryClient discoveryClient) { + public EventController(EventsJpaRepository repository, RestTemplateBuilder restTemplateBuilder, ServiceDiscoveryClient discoveryClient) { this.repository = repository; this.restTemplate = restTemplateBuilder.build(); this.discoveryClient = discoveryClient; @@ -42,15 +41,7 @@ public class EventController { @RequestMapping(path = "/events", method = RequestMethod.POST) public ResponseEntity add(@RequestBody Event event) throws ExternalServiceException { - - List usersInstances = discoveryClient.getInstances("users"); - if(usersInstances == null || usersInstances.size() == 0) { - System.err.print("Users service not found!"); - throw new ExternalServiceException(); - } - - ServiceInstance usersService = usersInstances.get(0); - String usersServiceBase = usersService.getUri().toString(); + String usersServiceBase = discoveryClient.getServiceUrl(Service.USERS); try { String response = restTemplate.getForObject(usersServiceBase + "/users/{id}", String.class, event.getCreatedById()); Event result = repository.save(event); diff --git a/events/src/main/resources/application.properties b/events/src/main/resources/application.properties deleted file mode 100644 index 555a01a..0000000 --- a/events/src/main/resources/application.properties +++ /dev/null @@ -1,30 +0,0 @@ -spring.application.name=events -server.port = 9020 -spring.datasource.url = jdbc:mysql://localhost:3306/events -spring.datasource.username = root -spring.datasource.password = root -spring.jpa.generate-ddl=true -user.password=dizda -# -#spring.datasource.url = jdbc:mysql://localhost:3306/events -#spring.datasource.username = root -#spring.datasource.password = 1DvaTri! -# -## Keep the connection alive if idle for a long time (needed in production) -#spring.datasource.testWhileIdle = true -#spring.datasource.validationQuery = SELECT 1 -# -## Show or not log for each sql query -#spring.jpa.show-sql = true -# -## Hibernate ddl auto (create, create-drop, update) -#spring.jpa.hibernate.ddl-auto = update -# -## Naming strategy -#spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy -# -## Use spring.jpa.properties.* for Hibernate native properties (the prefix is -## stripped before adding them to the entity manager) -# -## The SQL dialect makes Hibernate generate better SQL for the chosen database -#spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect diff --git a/teams/src/main/resources/application.properties b/teams/src/main/resources/application.properties deleted file mode 100644 index 43002cd..0000000 --- a/teams/src/main/resources/application.properties +++ /dev/null @@ -1,6 +0,0 @@ -spring.application.name=teams -server.port=9010 -spring.datasource.url = jdbc:mysql://localhost:3306/teams -spring.datasource.username = root -spring.datasource.password = root -spring.jpa.generate-ddl=true diff --git a/users/src/main/resources/application.properties b/users/src/main/resources/application.properties deleted file mode 100644 index 8d04f5a..0000000 --- a/users/src/main/resources/application.properties +++ /dev/null @@ -1,7 +0,0 @@ -spring.application.name=users -server.port = 8090 -spring.datasource.url = jdbc:mysql://localhost:3306/users -spring.datasource.username = root -spring.datasource.password = root -spring.jpa.generate-ddl=true -