Skip to content

Commit

Permalink
update deps, add example k8s config, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
zeusbaba committed Oct 24, 2018
1 parent f8c3f6d commit 5ff9ca4
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 80 deletions.
41 changes: 41 additions & 0 deletions k8s.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: learnmake-ms
spec:
selector:
matchLabels:
app: learnmake-ms
template:
metadata:
labels:
app: learnmake-ms
spec:
containers:
- name: learnmake-ms
image: gcr.io/catpet-205415/learnmake-microservices:1.0.8
ports:
- containerPort: 8080
#resources:
# limits:
# memory: "200Mi"
# requests:
# memory: "100Mi"
livenessProbe:
httpGet:
port: 8080
path: /api/pulse
initialDelaySeconds: 5
periodSeconds: 30
successThreshold: 1
failureThreshold: 2

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: learnmake-ms-ingress
spec:
backend:
serviceName: learnmake-ms
servicePort: 8080
19 changes: 13 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>learnmake.microservices</groupId>
<artifactId>learnmake-microservices</artifactId>
<version>1.0.4-SNAPSHOT</version>
<version>1.0.8</version>
<packaging>jar</packaging>

<name>learnmake-microservices</name>
Expand All @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Expand All @@ -29,15 +29,16 @@
FIXME: make sure you already have created a project at Google Cloud Platform
e.g. my dev-project is: gcr.io/catpet-205415
-->
<docker.image.prefix>gcr.io/my-gcp-project</docker.image.prefix>
<docker.image.prefix>gcr.io/catpet-205415</docker.image.prefix>

<!-- keep dependency versions organized -->
<versions.jib_maven_plugin>0.9.7</versions.jib_maven_plugin>
<versions.guava>25.1-jre</versions.guava>
<versions.jib-maven-plugin>0.9.13</versions.jib-maven-plugin>
<versions.guava>27.0-jre</versions.guava>
<versions.commons_lang3>3.7</versions.commons_lang3>
<versions.logback_classic>1.2.3</versions.logback_classic>
<versions.logstash_logback_encoder>5.1</versions.logstash_logback_encoder>
<versions.slf4j_api>1.7.25</versions.slf4j_api>
<version.lombok>1.18.2</version.lombok>

</properties>

Expand Down Expand Up @@ -72,6 +73,12 @@
<artifactId>commons-lang3</artifactId>
<version>${versions.commons_lang3}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>${version.lombok}</version>
</dependency>
<!-- Logging - required for Kibana integration -->
<dependency>
<groupId>ch.qos.logback</groupId>
Expand Down Expand Up @@ -115,7 +122,7 @@
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>${versions.jib_maven_plugin}</version>
<version>${versions.jib-maven-plugin}</version>
<configuration>
<allowInsecureRegistries>true</allowInsecureRegistries>
<from>
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/learnmake/microservices/RunApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import learnmake.microservices.config.AppContextInitializer;
import learnmake.microservices.config.MainConfig;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -11,13 +12,13 @@
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;

@Slf4j
@SpringBootApplication
@Import(MainConfig.class)
public class RunApplication implements CommandLineRunner {

private Logger logger = LoggerFactory.getLogger(RunApplication.class);
@Autowired
Environment environment;
@Autowired
Environment environment;

public static void main(String[] args) {
new SpringApplicationBuilder(RunApplication.class)
Expand All @@ -32,6 +33,6 @@ public void run(String... args) throws Exception {
sb.append(" ").append(option);
}
sb = sb.length() == 0 ? sb.append("No extra options specified") : sb;
logger.info(String.format("APP launched with options: %s", sb.toString()));
log.info(String.format("APP launched with options: %s", sb.toString()));
}
}
12 changes: 8 additions & 4 deletions src/main/java/learnmake/microservices/app/ApiController.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package learnmake.microservices.app;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
@RequestMapping("/api")
public class ApiController extends BaseController {
public class ApiController {


// TODO: API endpoints goes here

@GetMapping("/pulse")
public @ResponseBody
String pulseCheck() {
ResponseEntity<String> pulseCheck() {

logThis(ApiController.class.getSimpleName() + ".pulseCheck", "pulse");
return "OK";//new ResponseEntity<>("OK", HttpStatus.OK);
log.info(".pulseCheck {}", "/pulse");
return new ResponseEntity<>("OK", HttpStatus.OK);
}
}
24 changes: 0 additions & 24 deletions src/main/java/learnmake/microservices/app/BaseController.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import learnmake.microservices.utils.AppConstants;
import learnmake.microservices.utils.CookieUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.FlashMap;
Expand All @@ -14,14 +15,13 @@
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Slf4j
public class RequestInterceptor implements HandlerInterceptor {

private static final Logger LOGGER = LoggerFactory.getLogger(RequestInterceptor.class);

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

LOGGER.debug("Request Intercepted! "
log.debug("Request Intercepted! "
+ "method: {} - servletPath: {}", request.getMethod(), request.getServletPath()
);

Expand All @@ -46,7 +46,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons

response.sendRedirect(request.getContextPath());
} catch (IOException ioex) {
LOGGER.error(RequestInterceptor.class.getSimpleName()+".preHandle", ioex);
log.error(RequestInterceptor.class.getSimpleName()+".preHandle", ioex);
}

return false;
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/learnmake/microservices/app/WebController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package learnmake.microservices.app;

import learnmake.microservices.utils.AppConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
Expand All @@ -9,21 +10,22 @@
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Slf4j
@Controller
@RequestMapping("/")
public class WebController extends BaseController {
public class WebController {

@RequestMapping(method = RequestMethod.GET, produces=MediaType.TEXT_HTML_VALUE)
public String getMainPage() {

logThis(WebController.class.getSimpleName() + ".getMainPage", "getMainPage");
log.info(".getMainPage");
return AppConstants.getIndex();
}

@GetMapping("/hello")
public String getHello(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {

logThis(WebController.class.getSimpleName() + ".getHello", "hello");
log.info(".getHello");
model.addAttribute("name", name);
return "hello";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package learnmake.microservices.config;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContextInitializer;
Expand All @@ -12,13 +13,12 @@
import java.util.Iterator;
import java.util.Map;

@Slf4j
public class AppContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

private static final Logger logger = LoggerFactory.getLogger(AppContextInitializer.class);

@Override
public void initialize(ConfigurableApplicationContext ac) {
logger.info("Application started with active profiles: " + Arrays.asList(ac.getEnvironment().getActiveProfiles()));
log.info("Application started with active profiles: " + Arrays.asList(ac.getEnvironment().getActiveProfiles()));

Map<String, Object> mapElements = new HashMap<>();
for (Iterator<PropertySource<?>> it = (ac.getEnvironment()).getPropertySources().iterator(); it.hasNext();) {
Expand All @@ -32,7 +32,7 @@ public void initialize(ConfigurableApplicationContext ac) {
for (Map.Entry<String, Object> entry : mapElements.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
logger.debug(key + " : " + value);
log.debug(key + " : " + value);
}
}

Expand Down
68 changes: 37 additions & 31 deletions src/main/java/learnmake/microservices/config/WebMvcConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,51 @@
import java.util.Locale;

@Configuration
@ComponentScan(basePackages = { "learnmake.microservices.app" })
@ComponentScan(basePackages = {"learnmake.microservices.app"})
public class WebMvcConfig implements WebMvcConfigurer {

@Value("${spring.application.name}")
private String appName;
@Value("${spring.application.name}")
private String appName;

@Autowired
private RequestMappingHandlerAdapter requestMappingHandlerAdapter;
@Autowired
private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

@PostConstruct
public void init() {
requestMappingHandlerAdapter.setIgnoreDefaultModelOnRedirect(true); // avoid model attributes appearing in the URL after a redirect.
}
@PostConstruct
public void init() {
requestMappingHandlerAdapter.setIgnoreDefaultModelOnRedirect(
true); // avoid model attributes appearing in the URL after a redirect.
}

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer contentNegotiationConfigurer) {
contentNegotiationConfigurer.defaultContentType(MediaType.APPLICATION_JSON_UTF8);
}
@Override
public void configureContentNegotiation(
ContentNegotiationConfigurer contentNegotiationConfigurer) {
contentNegotiationConfigurer.defaultContentType(MediaType.APPLICATION_JSON_UTF8);
}

@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(new Locale("nb", "NO"));
return localeResolver;
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(new Locale("nb", "NO"));
return localeResolver;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/learnmake/wro/**", "/wro/**").setCacheControl(CacheControl.noCache());
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/learnmake/wro/**", "/wro/**")
.setCacheControl(CacheControl.noCache());
}

@Bean
public RequestInterceptor requestInterceptor() {
return new RequestInterceptor();
}
@Bean
public RequestInterceptor requestInterceptor() {
return new RequestInterceptor();
}

@Override
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(requestInterceptor()).addPathPatterns("/**");
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(requestInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/api/pulse")
;
}

}

0 comments on commit 5ff9ca4

Please sign in to comment.