Hystrix turbine is not working - spring-boot

I am configuring hystrix turbine dashbord using ConfigPropertyBasedDiscovery . When I hit normal stream URL, it works fine
http://localhost:8080/turbine.stream?cluster=EXAMPLE
But when I try to load this cluster stream in dashbord it show below error "unnable to connect command matrix" on browser and on console "EventSource's response has a MIME type ("text/plain") that is not "text/event-stream". Aborting the connection.". Please have look on screen shot.
Below is my config.properties
turbine.aggregator.clusterConfig=EXAMPLE,EXAMPLE1
turbine.instanceUrlSuffix=:9080/hystrix.stream
turbine.ConfigPropertyBasedDiscovery.EXAMPLE.instances=localhost
turbine.ConfigPropertyBasedDiscovery.EXAMPLE1.instances=139.126.244.170
InstanceDiscovery.impl=com.netflix.turbine.discovery.ConfigPropertyBasedDiscovery
My Application.java
#EnableHystrixDashboard
public class Application extends SpringBootApplication {
private static Class<Application> applicationClass = Application.class;
public static void main(String[] args) {
SpringApplication.run(applicationClass, args);
TurbineInit.init();
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
super.configure(application);
return application.sources(applicationClass);
}
}
And servlet registration for "/turbine.stream"
#Configuration
public class ServletRegistrationConfig {
#Bean
public ServletRegistrationBean registerTurbineBean(){
return new ServletRegistrationBean(new TurbineStreamServlet(), "/turbine.stream");
}
}
Please assist me in this. And let me know if I missed out in configuration.

Related

Spring Boot and GlassFish

I actually have a spring boot application that i deployed on glassfish server 5.0 .
The probleme is that the jsp pages are not showing when i type the link.
I correctly configured the main class:
#SpringBootApplication
public class CaisseApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(CaisseApplication.class, args);
}
}
the controller looks like this:
#Controller
#RequestMapping("/imports_caisses_et_grandsLivres/")public class controller {
#RequestMapping(value = "")
public String index(Map<Object, Object> model) {
return "index";
}
}
The jsp pages are correctly created, and to acces them, i'm using application.properties:
spring.mvc.view.prefix= /WEB-INF/views/
spring.mvc.view.suffix= .jsp
Sorry, the brackets {} was missing in:
#Controller
#RequestMapping({"/imports_caisses_et_grandsLivres/"})

In Spring-based web application, where is my console/log output going to?

I'm writing a simple Spring-based web application and deploying it to Websphere Liberty 8.5.5.9; I've gotten past my deployment problems and the application seems to start (according to the Liberty console.log). However, I'm not seeing any console or log output. My application main class, which contains print statements in the main method, is:
#Configuration
public class UserSettingApplication extends SpringBootServletInitializer implements WebApplicationInitializer {
ServletContext servletContext;
private static final LoggerUtils logger = new LoggerUtils( UserSettingApplication.class );
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
builder.sources(UserSettingApplication.class);
return builder;
}
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.addListener(RequestContextListener.class);
this.servletContext=servletContext;
super.onStartup(servletContext);
}
public static void main(String[] args) throws Exception {
System.out.println( "Entering UserSettingApplication.main" );
SpringApplicationBuilder applicationBuilder = new UserSettingApplication().configure(new SpringApplicationBuilder());
applicationBuilder.run(args);
System.out.println( "Entering UserSettingApplication.main" );
}
#Override
protected WebApplicationContext run(SpringApplication application) {
WebApplicationContext webApplicationContext = super.run(application);
Environment env = webApplicationContext.getEnvironment();
String sessionName = env.getProperty("server.session.cookie.name", "xplore-session-id");
servletContext.getSessionCookieConfig().setName(sessionName);
return webApplicationContext;
}
#Bean
protected RequestContextListener requestContextListener() {
return new RequestContextListener();
}
#Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
logger.info("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
logger.info(beanName);
}
};
}
}
Shouldn't I be seeing the print statements in the main method in the WASLiberty console.log?
Shouldn't I be seeing the print statements in the main method in the WASLiberty console.log?
You will not see any printouts from the main method as it is not executed in the Liberty. The main method is used for standalone apps started from the command line not run from the app server. Put your messages in the configure method like below and you will see it.
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
System.out.println("########################################Starting app");
System.out.println() will NOT write into files such as console.log.
Instead it will write into console such as commandline window or eclipse console.
You need to use loggers such as log4j or Java.util.logging for writing into files.
You can find more info here what-is-the-difference-between-java-logger-and-system-out-println

Setting Spring #Profile from JNDI for SpringBoot

I'm deploying my SpringBoot Application to a Tomcat 8.5 container.
Similar as described here: http://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html I modified my SpringBootApplication, so it's deployable as war.
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
}
Similiar to this question Activating Spring #Profile Using JNDI I wan't the application to select the active Profile using an JNDI entry.
I added an EnvironmentApplicationContextInitializer identical to the one shown in the accepted answer.
However: I do not use any web.xml. So the profile is not being picked up and used.
How do I make the SpringBootApplication using the EnvironmentApplicationContextInitializer?
Thanks to M Deinum's comment I found a solution:
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
// this would be used if run via java -jar service.war
// SpringApplication app = new SpringApplication(Application.class);
// CustomApplicationContextInitializer initializer = new CustomApplicationContextInitializer();
// app.addInitializers(initializer);
// app.run(args);
SpringApplication.run(Application.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// this will be used within an app container
CustomApplicationContextInitializer initializer = new CustomApplicationContextInitializer();
return builder.initializers(initializer).sources(Application.class);
}
}
Note the builder.initializers(initializer) part.

Expose/Filter Controller Request Mappings by Port/Connector

I have a relatively simple Spring Boot application that, by default, is secured over SSL on port 9443 using a self-signed certificate, which works great for serving up APIs to, say, a mobile app. However, I would now like to develop an unsecured web application with its own frontend and serve up a subset of the content I allow over SSL.
This is what I've come up with so far, which enables port 8080 over HTTP in addition to port 9443, the latter I've defined in application.properties:
#SpringBootApplication
#ComponentScan
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
tomcat.addAdditionalTomcatConnectors(createWebsiteConnector());
return tomcat;
}
private Connector createWebsiteConnector() {
Connector connector = new Connector(TomcatEmbeddedServletContainerFactory.DEFAULT_PROTOCOL);
connector.setPort(8080);
return connector;
}
}
I am now faced with the task of only exposing endpoints to the 8080 connection, and all of them to 9443. Obviously, the latter currently works by default, but right now 8080 can access everything 9443 can. Ideally, I would like to control access to certain request mappings defined in a "shared" controller that both connections have access to, i.e. something like this:
#RequestMapping(value = "/public", method = RequestMethod.GET)
public List<String> getPublicInfo() {
// ...
}
#HTTPSOnly
#RequestMapping(value = "/secured", method = RequestMethod.GET)
public List<String> getSecuredInfo() {
// ...
}
I assume something like what I have above isn't actually possible, but does anyone know how I could achieve the same effect?
Thanks in advance for any help!
Alright, I think I actually managed to solve this myself, but I'm open to other suggestions if anyone thinks they have a better solution:
#SpringBootApplication
#ComponentScan
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
tomcat.addAdditionalTomcatConnectors(createWebsiteConnector());
return tomcat;
}
private Connector createWebsiteConnector() {
Connector connector = new Connector(TomcatEmbeddedServletContainerFactory.DEFAULT_PROTOCOL);
connector.setPort(8080);
return connector;
}
private static HashSet<String> uriWhitelist = new HashSet<>(4);
static {
// static website content
uriWhitelist.add("/");
uriWhitelist.add("/index.html");
uriWhitelist.add("/logo_48x48.png");
// public APIs
uriWhitelist.add("/public");
}
private static class PortFilter extends OncePerRequestFilter {
#Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
if (request instanceof RequestFacade) {
RequestFacade requestFacade = (RequestFacade) request;
if (requestFacade.getServerPort() != 9443 && !uriWhitelist.contains(requestFacade.getRequestURI())) {
// only allow unsecured requests to access whitelisted endpoints
return;
}
}
filterChain.doFilter(request, response);
}
}
#Bean
public FilterRegistrationBean portFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
PortFilter filter = new PortFilter();
filterRegistrationBean.setFilter(filter);
return filterRegistrationBean;
}
}

spring boot not launching static web content

Am trying to launch index.html in my spring boot app but see 404. What dependency am I missing?
build.gradle (multi project)
project('sub-project')
{
apply plugin: 'spring-boot'
compile (
"org.springframework.boot:spring-boot-starter-web:1.0.0.RC5",
"org.springframework.boot:spring-boot-starter-actuator:1.0.0.RC5"
.. few more app specific dependencies
)
project structure:
MainProject
-- sub-project
src
main
resources
index.html
Application class:
#Configuration
#EnableAutoConfiguration
class Application {
public static void main(String[] args) {
SpringApplication.run([SpringServlet, Application, "classpath:/META-INF/com/my/package/bootstrap.xml"] as Object[], args)
}
}
**Launching http://localhost:8080/index.html throws 404.**
Found the root cause. Changing the SpringServlet's Url mappings to "Rest" resources specific path fixed it.
Earlier "/*" was also interpreted by SpringServlet and was not able to render the index.html.
class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run([Application, "classpath:/META-INF/com/my/package/mgmt/bootstrap.xml"] as Object[], args)
}
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application);
}
#Bean
ServletRegistrationBean jerseyServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(new SpringServlet(), "/rest/*");
Map<String, String> params = ["com.sun.jersey.config.property.packages": "com.my.package.mgmt.impl;com.wordnik.swagger.jersey.listing"]
registration.setInitParameters(params)
return registration;
}
#Bean
ServletRegistrationBean jerseyJaxrsConfig() {
ServletRegistrationBean registration = new ServletRegistrationBean(new DefaultJaxrsConfig(), "/api/*");
Map<String, String> params = ["swagger.api.basepath": "http://localhost:8080/api", "api.version": "0.1.0"]
registration.setInitParameters(params)
return registration;
}
#Configuration
public class WebConfig implements WebMvcConfigurer {
/** do not interpret .123 extension as a lotus spreadsheet */
#Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer)
{
configurer.favorPathExtension(false);
}
/**
./resources/public is not working without this
*/
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/public/");
}
}

Resources