Create service discovery client in common module
parent
f4b287b84a
commit
29ea33cd0a
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package ba.steleks.service;/**
|
||||
* Created by ensar on 16/04/17.
|
||||
*/
|
||||
|
||||
public enum Service {
|
||||
USERS,
|
||||
EVENTS,
|
||||
TEAMS;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package ba.steleks.service;/**
|
||||
* Created by ensar on 16/04/17.
|
||||
*/
|
||||
|
||||
public interface ServiceIdProvider {
|
||||
String getServiceId(Service service);
|
||||
}
|
|
@ -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<ServiceInstance> 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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
2
config
2
config
|
@ -1 +1 @@
|
|||
Subproject commit 75d72c5a3bcd442d37b756e7e34fa9e4a25b84d6
|
||||
Subproject commit 0d028147e90627b077c5435b557d6b3d7d19623c
|
|
@ -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 {
|
||||
|
||||
|
|
|
@ -7,4 +7,3 @@ 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
|
|
@ -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')
|
||||
}
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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 {
|
||||
|
||||
|
|
|
@ -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<ServiceInstance> 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);
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
||||
|
Reference in New Issue