Spring Boot with Hazelcast and Tomcat - spring-boot

How do you use Hazelcast as a http session store with embedded Tomcat with Spring Boot and Spring Security? I see there is a EmbeddedServletContainerCustomizer and SpringAwareWebFilter but I don't understand how to use it.

As described in Hazelcast's documentation, you need to configure Hazelcast's SpringAwareWebFilter and SessionListener. You can do so in Spring Boot by declaring a FilterRegistrationBean and a ServletListenerRegistrationBean respectively:
#Bean
public FilterRegistrationBean hazelcastFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean(new SpringAwareWebFilter());
registration.addUrlPatterns("/*");
registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE);
// Configure init parameters as appropriate:
// registration.addInitParameter("foo", "bar");
return registration;
}
#Bean
public ServletListenerRegistrationBean<SessionListener> hazelcastSessionListener() {
return new ServletListenerRegistrationBean<SessionListener>(new SessionListener());
}
SpringAwareWebFilter and SessionListener are both in Hazelcast's hazelcast-wm module so you'll need to add a dependency on com.hazelcast:hazelcast-wm to your pom.xml or build.gradle. hazelcast-wm also requires Spring Security to be on the classpath.
Now, when you run your application, you should see log output from Hazelcast during startup that's similar to the following:
2014-12-17 10:29:32.401 INFO 94332 --- [ost-startStop-1] com.hazelcast.config.XmlConfigLocator : Loading 'hazelcast-default.xml' from classpath.
2014-12-17 10:29:32.435 INFO 94332 --- [ost-startStop-1] c.hazelcast.web.HazelcastInstanceLoader : Creating a new HazelcastInstance for session replication
2014-12-17 10:29:32.582 INFO 94332 --- [ost-startStop-1] c.h.instance.DefaultAddressPicker : [LOCAL] [dev] [3.3.3] Prefer IPv4 stack is true.
2014-12-17 10:29:32.590 INFO 94332 --- [ost-startStop-1] c.h.instance.DefaultAddressPicker : [LOCAL] [dev] [3.3.3] Picked Address[169.254.144.237]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true
2014-12-17 10:29:32.612 INFO 94332 --- [ost-startStop-1] c.h.spi.impl.BasicOperationScheduler : [169.254.144.237]:5701 [dev] [3.3.3] Starting with 16 generic operation threads and 16 partition operation threads.
2014-12-17 10:29:32.657 INFO 94332 --- [ost-startStop-1] com.hazelcast.system : [169.254.144.237]:5701 [dev] [3.3.3] Hazelcast 3.3.3 (20141112 - eadb69c) starting at Address[169.254.144.237]:5701
2014-12-17 10:29:32.657 INFO 94332 --- [ost-startStop-1] com.hazelcast.system : [169.254.144.237]:5701 [dev] [3.3.3] Copyright (C) 2008-2014 Hazelcast.com
2014-12-17 10:29:32.661 INFO 94332 --- [ost-startStop-1] com.hazelcast.instance.Node : [169.254.144.237]:5701 [dev] [3.3.3] Creating MulticastJoiner
2014-12-17 10:29:32.664 INFO 94332 --- [ost-startStop-1] com.hazelcast.core.LifecycleService : [169.254.144.237]:5701 [dev] [3.3.3] Address[169.254.144.237]:5701 is STARTING
2014-12-17 10:29:38.482 INFO 94332 --- [ost-startStop-1] com.hazelcast.cluster.MulticastJoiner : [169.254.144.237]:5701 [dev] [3.3.3]
Members [1] {
Member [169.254.144.237]:5701 this
}
2014-12-17 10:29:38.503 INFO 94332 --- [ost-startStop-1] com.hazelcast.core.LifecycleService : [169.254.144.237]:5701 [dev] [3.3.3] Address[169.254.144.237]:5701 is STARTED

Why not uses Spring-session? It is pretty easy.
Instead of using Tomcat’s HttpSession, we are actually persisting the values in Redis. Spring Session replaces the HttpSession with an implementation that is backed by Redis. When Spring Security’s SecurityContextPersistenceFilter saves the SecurityContext to the HttpSession it is then persisted into Redis.
#EnableRedisHttpSession
public class HttpSessionConfig {
}
#src/main/resources/application.properties
spring.redis.host=localhost
spring.redis.password=secret
spring.redis.port=6379
http://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot.html

Related

Springboot logging layout

I don't understand what the numbers after logging level indicate
2019-03-05 10:57:51.112 INFO 45469 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.52
2019-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1358 ms
2019-03-05 10:57:51.698 INFO 45469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2019-03-05 10:57:51.702 INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
For example INFO 45469 what does 45469 indicate?
Other example
1857 [main] 2021-06-23T17:23:04.222+0300 INFO org.springframework.boot.SpringApplication -
1856 [main] 2021-06-23T17:23:04.221+0300 DEBUG org.springframework.boot.StartupInfoLogger -
1853 [main] 2021-06-23T17:23:04.218+0300 INFO org.springframework.boot.StartupInfoLogger -
what is the purpose of the first column?
By looking at the log4j2.xml in spring boot project, it seems the default pattern for the console is :
<Property name="CONSOLE_LOG_PATTERN">
%clr{%d{${LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}
</Property>
So the number should indicates the PID, so the ID of the process which runs your app.
Personally I never used the default format (for my own logs, yours are the startup logs). My first thing is to replace it whith my configuration. It looks and based on this it should be the PID (process ID).

How to move Zuul gateway configurations to configuration server

I'm starting out with microservices architecture and spring cloud.
I'm trying to get configurations for my Spring Zuul Gateway from the spring configuration server.
I've added below properties in bootstrap.properties file of the gateway service:
spring.application.name=api-gateway
spring.cloud.config.uri=http://localhost:8888
spring.profiles.active=dev
Even though these properties work for all other services, for the gateway they do not work.
The annotations I'm using for the gateway are:
#EnableZuulProxy
#EnableDiscoveryClient
#SpringBootApplication
public class ApiGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayApplication.class, args);
}
}
The annotation for other services is only:
#SpringBootApplication
My configuration server has a connected git repository with a file: api-gateway-dev.properties
logs of gateway:
:: Spring Boot :: (v2.3.0.M4)
2020-04-17 21:42:39.983 INFO 10340 --- [ restartedMain]
c.nyo.apigateway.ApiGatewayApplication : The following profiles are
active: dev 2020-04-17 21:42:41.926 WARN 10340 --- [ restartedMain]
o.s.boot.actuate.endpoint.EndpointId : Endpoint ID
'service-registry' contains invalid characters, please migrate to a
valid format. 2020-04-17 21:42:41.969 WARN 10340 --- [
restartedMain] o.s.boot.actuate.endpoint.EndpointId : Endpoint ID
'hystrix.stream' contains invalid characters, please migrate to a
valid format. 2020-04-17 21:42:42.240 INFO 10340 --- [
restartedMain] o.s.cloud.context.scope.GenericScope : BeanFactory
id=a11a283e-7de6-3470-b177-65c08eab7398 2020-04-17 21:42:43.859 INFO
10340 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer :
Tomcat initialized with port(s): 8765 (http) 2020-04-17 21:42:43.880
INFO 10340 --- [ restartedMain]
o.apache.catalina.core.StandardService : Starting service [Tomcat]
logs of a service that is getting configurations:
:: Spring Boot :: (v2.3.0.M4)
2020-04-17 21:54:01.414 INFO 5180 --- [ restartedMain]
c.c.c.ConfigServicePropertySourceLocator : Fetching config from server
at : http://localhost:8888 2020-04-17 21:54:04.184 INFO 5180 --- [
restartedMain] c.c.c.ConfigServicePropertySourceLocator : Located
environment: name=users-service, profiles=[dev], label=null,
version=244114f5a11aa7d4a0acb5750ddad144f7de1be5, state=null
2020-04-17 21:54:04.186 INFO 5180 --- [ restartedMain]
b.c.PropertySourceBootstrapConfiguration : Located property source:
[BootstrapPropertySource {name='bootstrapProperties-configClient'},
BootstrapPropertySource
{name='bootstrapProperties-https://my-user#bitbucket.org/my-user/configs.git/users-service-dev.properties'},
BootstrapPropertySource
{name='bootstrapProperties-https://my-user#bitbucket.org/my-user/configs.git/users-service.properties'}]
2020-04-17 21:54:04.197 INFO 5180 --- [ restartedMain]
c.n.U.UsersServiceApplication : The following profiles are
active: dev
Is it logical to make gateway configuration dynamic?
Why don't I get the configuration from the configuration server?
Found the problem. I was missing:
implementation 'org.springframework.cloud:spring-cloud-starter-config'
in my build.gradle file dependencies

2 Hazelcast Members per Spring Boot Instance

I am using Hazelcast as a Hibernate 2LC provider in my Spring Boot application in the embedded/P2P mode. Upon starting the application I could see that two members for my Hazelcast cluster are created. When I start a second instance of my whole application, another two members are created.
Currently, I use the following configuration with HazelcastCacheRegionFactory (not local).
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=com.hazelcast.hibernate.HazelcastCacheRegionFactory
spring.jpa.properties.hibernate.generate_statistics=true
In the Hazelcast management centre, it looks like one member is the double of the other one. However, when starting a second instance of my Spring Boot application, both member still exist and in total they increase to four.
I already tried to set the backup count to 0 in my hazelcast.xml.
When starting the first instance of my application, I get the following logs:
2019-10-09 12:04:51.055 WARN 21088 --- [ restartedMain] c.h.instance.HazelcastInstanceFactory : Hazelcast is starting in a Java modular environment (Java 9 and newer) but without proper access to required Java packages. Use additional Java arguments to provide Hazelcast access to Java internal API. The internal API access is used to get the best performance results. Arguments to be used:
--add-modules java.se --add-exports java.base/jdk.internal.ref=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.nio=ALL-UNNAMED --add-opens java.base/sun.nio.ch=ALL-UNNAMED --add-opens java.management/sun.management=ALL-UNNAMED --add-opens jdk.management/com.sun.management.internal=ALL-UNNAMED
2019-10-09 12:04:51.058 INFO 21088 --- [ restartedMain] c.h.config.AbstractConfigLocator : Loading 'hazelcast.xml' from the classpath.
2019-10-09 12:04:51.238 WARN 21088 --- [ restartedMain] c.h.config.AbstractXmlConfigHelper : Name of the hazelcast schema location is incorrect, using default
2019-10-09 12:04:51.717 INFO 21088 --- [ restartedMain] com.hazelcast.instance.AddressPicker : [LOCAL] [dev] [3.12.2] Prefer IPv4 stack is true, prefer IPv6 addresses is false
2019-10-09 12:04:53.004 INFO 21088 --- [ restartedMain] com.hazelcast.instance.AddressPicker : [LOCAL] [dev] [3.12.2] Picked [10.29.47.243]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true
2019-10-09 12:04:53.016 INFO 21088 --- [ restartedMain] com.hazelcast.system : [10.29.47.243]:5701 [dev] [3.12.2] Hazelcast 3.12.2 (20190802 - e34b163) starting at [10.29.47.243]:5701
2019-10-09 12:04:53.016 INFO 21088 --- [ restartedMain] com.hazelcast.system : [10.29.47.243]:5701 [dev] [3.12.2] Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved.
2019-10-09 12:04:53.240 INFO 21088 --- [ restartedMain] c.h.s.i.o.impl.BackpressureRegulator : [10.29.47.243]:5701 [dev] [3.12.2] Backpressure is disabled
2019-10-09 12:04:55.174 INFO 21088 --- [ restartedMain] com.hazelcast.instance.Node : [10.29.47.243]:5701 [dev] [3.12.2] Creating MulticastJoiner
2019-10-09 12:04:55.313 INFO 21088 --- [ restartedMain] c.h.s.i.o.impl.OperationExecutorImpl : [10.29.47.243]:5701 [dev] [3.12.2] Starting 4 partition threads and 3 generic threads (1 dedicated for priority tasks)
2019-10-09 12:04:55.314 INFO 21088 --- [ restartedMain] c.h.internal.diagnostics.Diagnostics : [10.29.47.243]:5701 [dev] [3.12.2] Diagnostics disabled. To enable add -Dhazelcast.diagnostics.enabled=true to the JVM arguments.
2019-10-09 12:04:55.328 INFO 21088 --- [ restartedMain] com.hazelcast.core.LifecycleService : [10.29.47.243]:5701 [dev] [3.12.2] [10.29.47.243]:5701 is STARTING
2019-10-09 12:04:57.466 INFO 21088 --- [ restartedMain] c.h.internal.cluster.ClusterService : [10.29.47.243]:5701 [dev] [3.12.2]
Members {size:1, ver:1} [
Member [10.29.47.243]:5701 - bf933bd9-9214-4022-a104-514bc5f56267 this
]
2019-10-09 12:04:57.486 INFO 21088 --- [ restartedMain] c.h.i.m.ManagementCenterService : [10.29.47.243]:5701 [dev] [3.12.2] Hazelcast will connect to Hazelcast Management Center on address:
http://localhost:8080/hazelcast-mancenter
2019-10-09 12:04:57.495 INFO 21088 --- [ restartedMain] com.hazelcast.core.LifecycleService : [10.29.47.243]:5701 [dev] [3.12.2] [10.29.47.243]:5701 is STARTED
2019-10-09 12:04:57.544 INFO 21088 --- [MC.State.Sender] com.hazelcast.client.impl.ClientEngine : [10.29.47.243]:5701 [dev] [3.12.2] Applying a new client selector :ClientSelector{any}
2019-10-09 12:04:57.651 INFO 21088 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-10-09 12:04:58.089 INFO 21088 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2019-10-09 12:04:58.163 INFO 21088 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2019-10-09 12:04:58.274 INFO 21088 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate Core {5.3.10.Final}
2019-10-09 12:04:58.276 INFO 21088 --- [ restartedMain] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2019-10-09 12:04:58.533 INFO 21088 --- [ restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-10-09 12:04:58.777 INFO 21088 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2019-10-09 12:04:59.122 INFO 21088 --- [ restartedMain] c.h.h.HazelcastCacheRegionFactory : Starting up HazelcastCacheRegionFactory
2019-10-09 12:04:59.124 INFO 21088 --- [ restartedMain] c.h.config.AbstractConfigLocator : Loading 'hazelcast.xml' from the classpath.
2019-10-09 12:04:59.127 WARN 21088 --- [ restartedMain] c.h.config.AbstractXmlConfigHelper : Name of the hazelcast schema location is incorrect, using default
2019-10-09 12:04:59.165 INFO 21088 --- [ restartedMain] com.hazelcast.instance.AddressPicker : [LOCAL] [dev] [3.12.2] Prefer IPv4 stack is true, prefer IPv6 addresses is false
2019-10-09 12:04:59.949 INFO 21088 --- [ restartedMain] com.hazelcast.instance.AddressPicker : [LOCAL] [dev] [3.12.2] Picked [10.29.47.243]:5703, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5703], bind any local is true
2019-10-09 12:04:59.950 INFO 21088 --- [ restartedMain] com.hazelcast.system : [10.29.47.243]:5703 [dev] [3.12.2] Hazelcast 3.12.2 (20190802 - e34b163) starting at [10.29.47.243]:5703
2019-10-09 12:04:59.950 INFO 21088 --- [ restartedMain] com.hazelcast.system : [10.29.47.243]:5703 [dev] [3.12.2] Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved.
2019-10-09 12:04:59.956 INFO 21088 --- [ restartedMain] c.h.s.i.o.impl.BackpressureRegulator : [10.29.47.243]:5703 [dev] [3.12.2] Backpressure is disabled
2019-10-09 12:05:01.343 INFO 21088 --- [ restartedMain] com.hazelcast.instance.Node : [10.29.47.243]:5703 [dev] [3.12.2] Creating MulticastJoiner
2019-10-09 12:05:01.348 INFO 21088 --- [ restartedMain] c.h.s.i.o.impl.OperationExecutorImpl : [10.29.47.243]:5703 [dev] [3.12.2] Starting 4 partition threads and 3 generic threads (1 dedicated for priority tasks)
2019-10-09 12:05:01.349 INFO 21088 --- [ restartedMain] c.h.internal.diagnostics.Diagnostics : [10.29.47.243]:5703 [dev] [3.12.2] Diagnostics disabled. To enable add -Dhazelcast.diagnostics.enabled=true to the JVM arguments.
2019-10-09 12:05:01.349 INFO 21088 --- [ restartedMain] com.hazelcast.core.LifecycleService : [10.29.47.243]:5703 [dev] [3.12.2] [10.29.47.243]:5703 is STARTING
2019-10-09 12:05:01.473 INFO 21088 --- [ restartedMain] c.h.i.cluster.impl.MulticastJoiner : [10.29.47.243]:5703 [dev] [3.12.2] Trying to join to discovered node: [10.29.47.243]:5701
2019-10-09 12:05:01.478 INFO 21088 --- [cached.thread-2] com.hazelcast.nio.tcp.TcpIpConnector : [10.29.47.243]:5703 [dev] [3.12.2] Connecting to /10.29.47.243:5701, timeout: 10000, bind-any: true
2019-10-09 12:05:01.486 INFO 21088 --- [.IO.thread-in-0] com.hazelcast.nio.tcp.TcpIpConnection : [10.29.47.243]:5701 [dev] [3.12.2] Initialized new cluster connection between /10.29.47.243:5701 and /10.29.47.243:51948
2019-10-09 12:05:01.487 INFO 21088 --- [.IO.thread-in-0] com.hazelcast.nio.tcp.TcpIpConnection : [10.29.47.243]:5703 [dev] [3.12.2] Initialized new cluster connection between /10.29.47.243:51948 and /10.29.47.243:5701
2019-10-09 12:05:07.483 INFO 21088 --- [ration.thread-0] c.h.internal.cluster.ClusterService : [10.29.47.243]:5701 [dev] [3.12.2]
Members {size:2, ver:2} [
Member [10.29.47.243]:5701 - bf933bd9-9214-4022-a104-514bc5f56267 this
Member [10.29.47.243]:5703 - 9862ded6-c26d-4da8-929f-f8616a4bf633
]
2019-10-09 12:05:07.489 INFO 21088 --- [ration.thread-0] c.h.internal.cluster.ClusterService : [10.29.47.243]:5703 [dev] [3.12.2]
Members {size:2, ver:2} [
Member [10.29.47.243]:5701 - bf933bd9-9214-4022-a104-514bc5f56267
Member [10.29.47.243]:5703 - 9862ded6-c26d-4da8-929f-f8616a4bf633 this
]
2019-10-09 12:05:08.481 WARN 21088 --- [ restartedMain] com.hazelcast.instance.Node : [10.29.47.243]:5703 [dev] [3.12.2] Config seed port is 5701 and cluster size is 2. Some of the ports seem occupied!
2019-10-09 12:05:08.482 INFO 21088 --- [ restartedMain] c.h.i.m.ManagementCenterService : [10.29.47.243]:5703 [dev] [3.12.2] Hazelcast will connect to Hazelcast Management Center on address:
http://localhost:8080/hazelcast-mancenter
2019-10-09 12:05:08.487 INFO 21088 --- [ restartedMain] com.hazelcast.core.LifecycleService : [10.29.47.243]:5703 [dev] [3.12.2] [10.29.47.243]:5703 is STARTED
How could I solve the issue of having two members per application instance? Is there an actual use of having two members per instance?
Solution by #Mesut
I added the following statement in the application.properties file of Spring Boot (config could be placed elsewhere).
spring.jpa.properties.hibernate.cache.hazelcast.instance_name=your-instance-name
You also have to create a bean with the name 'your-instance-name'. In Spring Boot this could be achieved by annotating a method which returns a Hazelcast instance with #Bean. This could be done in your main class or a #Configuration class.
#Bean
public Config hazelcastConfig() {
Config config = new Config();
config.setInstanceName("your-instance-name");
config.setProperty("hazelcast.phone.home.enabled", "false");
return config;
}
#Bean
public HazelcastInstance hazelcastInstance(Config config) {
return Hazelcast.newHazelcastInstance(config);
}
This results in having only one member per instance of your application (very good). However, the connection to the management centre does not work anymore (not that important in my situation).
If you wanna share the same instance, you should be using the same instance name in both hibernate config and hazelcast.xml.
hibernate.cache.hazelcast.instance_name is the configuration parameter in hibernate side.
You can check documentation here for more info.

Intellij Springboot problems on startup

I am really new with spring boot, so I started with some tutorials to create a simple Rest example (my final goal is to integrate it with mongodb, but I need to start!).
I created a new project on IntelliJ with Spring Initializr and choosed Web and Mongodb as features.
Then I added only a class,
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class HelloController {
#RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
When I run the project, the console is
2018-09-03 20:08:33.139 INFO 1363 --- [ main]
test.test.TestApplication : Starting TestApplication on
MacBook-Pro-di-Stefano.local with PID 1363
(/Users/stefanomiceli/IdeaProjects/test/target/classes started by
stefanomiceli in /Users/stefanomiceli/IdeaProjects/test)
2018-09-03 20:08:33.141 INFO 1363 --- [ main]
test.test.TestApplication : No active profile set,
falling back to default profiles: default
2018-09-03 20:08:33.173 INFO 1363 --- [ main]
ConfigServletWebServerApplicationContext : Refreshing
org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext#15713d56: startup date [Mon Sep 03 20:08:33 PDT 2018]; root of context hierarchy
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by
org.springframework.cglib.core.ReflectUtils$1
(file:/Users/stefanomiceli/.m2/repository/org/springframework/spring-
core/5.0.8.RELEASE/spring-core-5.0.8.RELEASE.jar) to method
java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of
org.springframework.cglib.core.ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further
illegal reflective access operations
WARNING: All illegal access operations will be denied in a future
release
2018-09-03 20:08:33.950 INFO 1363 --- [ main]
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with
port(s): 8080 (http)
2018-09-03 20:08:33.969 INFO 1363 --- [ main]
o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-09-03 20:08:33.969 INFO 1363 --- [ main]
org.apache.catalina.core.StandardEngine : Starting Servlet Engine:
Apache Tomcat/8.5.32
2018-09-03 20:08:33.972 INFO 1363 --- [ost-startStop-1]
o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat
Native library which allows optimal performance in production
environments was not found on the java.library.path:
[/Users/stefanomiceli/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2018-09-03 20:08:34.021 INFO 1363 --- [ost-startStop-1] o.a.c.c.C.
[Tomcat].[localhost].[/] : Initializing Spring embedded
WebApplicationContext
2018-09-03 20:08:34.021 INFO 1363 --- [ost-startStop-1]
o.s.web.context.ContextLoader : Root WebApplicationContext:
initialization completed in 852 ms
2018-09-03 20:08:34.059 INFO 1363 --- [ost-startStop-1]
o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet
mapped to [/]
2018-09-03 20:08:34.061 INFO 1363 --- [ost-startStop-1]
o.s.b.w.servlet.FilterRegistrationBean : Mapping filter:
'characterEncodingFilter' to: [/*]
2018-09-03 20:08:34.062 INFO 1363 --- [ost-startStop-1]
o.s.b.w.servlet.FilterRegistrationBean : Mapping filter:
'hiddenHttpMethodFilter' to: [/*]
2018-09-03 20:08:34.062 INFO 1363 --- [ost-startStop-1]
o.s.b.w.servlet.FilterRegistrationBean : Mapping filter:
'httpPutFormContentFilter' to: [/*]
2018-09-03 20:08:34.062 INFO 1363 --- [ost-startStop-1]
o.s.b.w.servlet.FilterRegistrationBean : Mapping filter:
'requestContextFilter' to: [/*]
2018-09-03 20:08:34.143 INFO 1363 --- [ main]
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path
[/**/favicon.ico] onto handler of type [class
org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-03 20:08:34.310 INFO 1363 --- [ main]
s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for
#ControllerAdvice:
org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext#15713d56: startup date [Mon Sep 03 20:08:33 PDT 2018]; root of context hierarchy
2018-09-03 20:08:34.344 INFO 1363 --- [ main]
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto
public
org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>>
org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-09-03 20:08:34.345 INFO 1363 --- [ main]
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces= [text/html]}" onto public org.springframework.web.servlet.ModelAndView
org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-09-03 20:08:34.362 INFO 1363 --- [ main]
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path
[/webjars/**] onto handler of type [class
org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-03 20:08:34.362 INFO 1363 --- [ main]
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto
handler of type [class
org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-03 20:08:34.550 INFO 1363 --- [ main]
org.mongodb.driver.cluster : Cluster created with
settings {hosts=[localhost:27017], mode=SINGLE,
requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms',
maxWaitQueueSize=500}
2018-09-03 20:08:34.582 INFO 1363 --- [localhost:27017]
org.mongodb.driver.connection : Opened connection
[connectionId{localValue:1, serverValue:55}] to localhost:27017
2018-09-03 20:08:34.585 INFO 1363 --- [localhost:27017]
org.mongodb.driver.cluster : Monitor thread successfully
connected to server with description
ServerDescription{address=localhost:27017, type=STANDALONE,
state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0,
1]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216,
logicalSessionTimeoutMinutes=30, roundTripTimeNanos=1582641}
2018-09-03 20:08:34.739 INFO 1363 --- [ main]
o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX
exposure on startup
2018-09-03 20:08:34.768 INFO 1363 --- [ main]
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s):
8080 (http) with context path ''
2018-09-03 20:08:34.772 INFO 1363 --- [ main]
test.test.TestApplication : Started TestApplication in
1.834 seconds (JVM running for 7.413)
But when I go on localhost:8080/ I get 404 error. What did I do wrong in my helloworld? Is there anything that should I make before running the application?
You need to either
Fix the project structure in order to place the Application Entry Point (TestApplication in this case) in the base package (see image below).
Or use #ComponentScan like this (not recommended):
#SpringBootApplication
#ComponentScan("prova") // add the names of the packages where the controllers, services, repositories, etc, are going to be stored.
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
Your package structure is wrong:
prova.HelloController
test.test.TestApplication
The HelloController must be on the same level or below TestApplication.
You can do it 2 ways.
1.(preferable) Your controller package naming should be test.test.prova
i.e. Your springboot application should follow the package naming structure of Main class.
e.g. If your Main class has package name = com.test, so the controller package name should be com.test.controller
Another way is adding #ComponentScan to your Main class.
e.g.#SpringBootApplication
#ComponentScan("prova")

spring hazelcast cache cluster showing warning

I have micro service architecture and all the micro services are using the same database. Now i want to have distributed cache in my architecture. For the moment i choose Hazelnut for distributed. As a start i put the following configuration in all my microservices.
#Configuration
#EnableCaching
#AutoConfigureBefore(value = { DatabaseConfiguration.class })
public class CacheConfiguration {
private final Logger LOG = LoggerFactory.getLogger(CacheConfiguration.class);
#Value("${cache.timeToLiveSeconds:3600}")
private final int timeToLiveSeconds = 3600;
#Value("${cache.backupCount:1}")
private final int backupCount = 1;
#Autowired
private Environment env;
#PreDestroy
public void destroy() {
LOG.info("Closing Cache Manager");
Hazelcast.shutdownAll();
}
#Bean
public CacheManager cacheManager(final HazelcastInstance hazelcastInstance) {
LOG.debug("Starting HazelcastCacheManager");
return new HazelcastCacheManager(hazelcastInstance);
}
#Bean
public HazelcastInstance hazelcastInstance() {
LOG.debug("Configuring Hazelcast");
final HazelcastInstance hazelCastInstance = Hazelcast
.getHazelcastInstanceByName("caching_service");
if (hazelCastInstance != null) {
LOG.debug("Hazelcast already initialized");
return hazelCastInstance;
}
final Config config = new Config();
config.setInstanceName("caching_service");
config.getNetworkConfig().setPort(5701);
config.getNetworkConfig().setPortAutoIncrement(true);
// In development, remove multicast auto-configuration
if (env.acceptsProfiles(Constants.DEV)) {
System.setProperty("hazelcast.local.localAddress", "127.0.0.1");
config.getNetworkConfig().getJoin().getAwsConfig().setEnabled(false);
config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(false);
}
config.getMapConfigs().put("default", initializeDefaultMapConfig());
return Hazelcast.newHazelcastInstance(config);
}
private MapConfig initializeDefaultMapConfig() {
final MapConfig mapConfig = new MapConfig();
/*
* Number of backups. If 1 is set as the backup-count for example, then all
* entries of the map will be copied to another JVM for fail-safety. Valid numbers
* are 0 (no backup), 1, 2, 3.
*/
mapConfig.setBackupCount(0);
/*
* Valid values are: NONE (no eviction), LRU (Least Recently Used), LFU (Least
* Frequently Used). NONE is the default.
*/
mapConfig.setEvictionPolicy(EvictionPolicy.LRU);
/*
* Maximum size of the map. When max size is reached, map is evicted based on the
* policy defined. Any integer between 0 and Integer.MAX_VALUE. 0 means
* Integer.MAX_VALUE. Default is 0.
*/
mapConfig.setMaxSizeConfig(
new MaxSizeConfig(0, MaxSizeConfig.MaxSizePolicy.USED_HEAP_SIZE));
return mapConfig;
}
}
Now i started my first micro service and the logs lines were
2017-07-16 10:38:06.581 INFO 13084 --- [ main] c.h.instance.DefaultAddressPicker : [LOCAL] [dev] [3.7.7] Picked [127.0.0.1]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true
2017-07-16 10:38:06.595 INFO 13084 --- [ main] com.hazelcast.system : [127.0.0.1]:5701 [dev] [3.7.7] Hazelcast 3.7.7 (20170404 - e3c56ea) starting at [127.0.0.1]:5701
2017-07-16 10:38:06.595 INFO 13084 --- [ main] com.hazelcast.system : [127.0.0.1]:5701 [dev] [3.7.7] Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
2017-07-16 10:38:06.595 INFO 13084 --- [ main] com.hazelcast.system : [127.0.0.1]:5701 [dev] [3.7.7] Configured Hazelcast Serialization version : 1
2017-07-16 10:38:06.769 INFO 13084 --- [ main] c.h.s.i.o.impl.BackpressureRegulator : [127.0.0.1]:5701 [dev] [3.7.7] Backpressure is disabled
2017-07-16 10:38:07.171 DEBUG 13084 --- [ main] c.h.internal.cluster.ClusterService : [127.0.0.1]:5701 [dev] [3.7.7] Updating members [Member [127.0.0.1]:5701 - b2e3c44d-0392-4813-a2c8-d648695e8f1d this]
2017-07-16 10:38:07.171 DEBUG 13084 --- [ main] c.h.i.p.InternalPartitionService : [127.0.0.1]:5701 [dev] [3.7.7] Adding Member [127.0.0.1]:5701 - b2e3c44d-0392-4813-a2c8-d648695e8f1d this
2017-07-16 10:38:07.293 INFO 13084 --- [ main] c.h.s.i.o.impl.OperationExecutorImpl : [127.0.0.1]:5701 [dev] [3.7.7] Starting 8 partition threads
2017-07-16 10:38:07.295 INFO 13084 --- [ main] c.h.s.i.o.impl.OperationExecutorImpl : [127.0.0.1]:5701 [dev] [3.7.7] Starting 5 generic threads (1 dedicated for priority tasks)
2017-07-16 10:38:07.302 INFO 13084 --- [ main] com.hazelcast.core.LifecycleService : [127.0.0.1]:5701 [dev] [3.7.7] [127.0.0.1]:5701 is STARTING
2017-07-16 10:38:07.302 DEBUG 13084 --- [ main] c.h.i.p.InternalPartitionService : [127.0.0.1]:5701 [dev] [3.7.7] Adding Member [127.0.0.1]:5701 - b2e3c44d-0392-4813-a2c8-d648695e8f1d this
2017-07-16 10:38:07.303 INFO 13084 --- [ main] c.h.n.t.n.NonBlockingIOThreadingModel : [127.0.0.1]:5701 [dev] [3.7.7] TcpIpConnectionManager configured with Non Blocking IO-threading model: 3 input threads and 3 output threads
2017-07-16 10:38:07.305 DEBUG 13084 --- [ main] c.h.n.t.n.NonBlockingIOThreadingModel : [127.0.0.1]:5701 [dev] [3.7.7] IO threads selector mode is SELECT
2017-07-16 10:38:07.317 WARN 13084 --- [ main] com.hazelcast.instance.Node : [127.0.0.1]:5701 [dev] [3.7.7] No join method is enabled! Starting standalone.
2017-07-16 10:38:07.351 INFO 13084 --- [ main] com.hazelcast.core.LifecycleService : [127.0.0.1]:5701 [dev] [3.7.7] [127.0.0.1]:5701 is STARTED
Now i started my second micro service
2017-07-16 10:41:30.841 INFO 17936 --- [ main] c.h.instance.DefaultAddressPicker : [LOCAL] [dev] [3.7.7] Picked [127.0.0.1]:5702, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5702], bind any local is true
2017-07-16 10:41:30.854 INFO 17936 --- [ main] com.hazelcast.system : [127.0.0.1]:5702 [dev] [3.7.7] Hazelcast 3.7.7 (20170404 - e3c56ea) starting at [127.0.0.1]:5702
2017-07-16 10:41:30.855 INFO 17936 --- [ main] com.hazelcast.system : [127.0.0.1]:5702 [dev] [3.7.7] Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
2017-07-16 10:41:30.855 INFO 17936 --- [ main] com.hazelcast.system : [127.0.0.1]:5702 [dev] [3.7.7] Configured Hazelcast Serialization version : 1
2017-07-16 10:41:31.017 INFO 17936 --- [ main] c.h.s.i.o.impl.BackpressureRegulator : [127.0.0.1]:5702 [dev] [3.7.7] Backpressure is disabled
2017-07-16 10:41:31.451 DEBUG 17936 --- [ main] c.h.internal.cluster.ClusterService : [127.0.0.1]:5702 [dev] [3.7.7] Updating members [Member [127.0.0.1]:5702 - db64274e-73bd-4aa2-ae67-f626433b25c6 this]
2017-07-16 10:41:31.452 DEBUG 17936 --- [ main] c.h.i.p.InternalPartitionService : [127.0.0.1]:5702 [dev] [3.7.7] Adding Member [127.0.0.1]:5702 - db64274e-73bd-4aa2-ae67-f626433b25c6 this
2017-07-16 10:41:31.582 INFO 17936 --- [ main] c.h.s.i.o.impl.OperationExecutorImpl : [127.0.0.1]:5702 [dev] [3.7.7] Starting 8 partition threads
2017-07-16 10:41:31.583 INFO 17936 --- [ main] c.h.s.i.o.impl.OperationExecutorImpl : [127.0.0.1]:5702 [dev] [3.7.7] Starting 5 generic threads (1 dedicated for priority tasks)
2017-07-16 10:41:31.590 INFO 17936 --- [ main] com.hazelcast.core.LifecycleService : [127.0.0.1]:5702 [dev] [3.7.7] [127.0.0.1]:5702 is STARTING
2017-07-16 10:41:31.591 DEBUG 17936 --- [ main] c.h.i.p.InternalPartitionService : [127.0.0.1]:5702 [dev] [3.7.7] Adding Member [127.0.0.1]:5702 - db64274e-73bd-4aa2-ae67-f626433b25c6 this
2017-07-16 10:41:31.591 INFO 17936 --- [ main] c.h.n.t.n.NonBlockingIOThreadingModel : [127.0.0.1]:5702 [dev] [3.7.7] TcpIpConnectionManager configured with Non Blocking IO-threading model: 3 input threads and 3 output threads
2017-07-16 10:41:31.592 DEBUG 17936 --- [ main] c.h.n.t.n.NonBlockingIOThreadingModel : [127.0.0.1]:5702 [dev] [3.7.7] IO threads selector mode is SELECT
2017-07-16 10:41:31.633 WARN 17936 --- [ main] com.hazelcast.instance.Node : [127.0.0.1]:5702 [dev] [3.7.7] No join method is enabled! Starting standalone.
2017-07-16 10:41:31.633 WARN 17936 --- [ main] com.hazelcast.instance.Node : [127.0.0.1]:5702 [dev] [3.7.7] Config seed port is 5701 and cluster size is 1. Some of the ports seem occupied!
2017-07-16 10:41:31.663 INFO 17936 --- [ main] com.hazelcast.core.LifecycleService : [127.0.0.1]:5702 [dev] [3.7.7] [127.0.0.1]:5702 is STARTED
so i wanted to know if i am doing things right here since i get this line after started second microservice
[127.0.0.1]:5702 [dev] [3.7.7] Config seed port is 5701 and cluster size is 1. Some of the ports seem occupied!
I have the following queries
1.) Is the the right way to created Hazelcast spring distributed cache mechanism ?
2.) When i say distributed does it mean that if i have same entity cache in both the micro services , doing CRUD on entity in one micro
service will update the cache in other micro services for the same
entity as well.
3.) If i don't want to use spring cache annotations like Cacheable etc... can i remove EnableCaching or it is not a good idea. I want
to create cache programmatically and manage cache programmatically and not
by annotations.
Definitely hazecast can work in your use case.
Check this answer to similar question
https://stackoverflow.com/a/38247515/27563
Check this blogpost with caching example for spring boot.

Resources