Ignite and Spring Boot - spring

How do I use Ignite with Spring Boot? I googled it but without success. Has anyone experience with the combination of Spring Boot and Ignite?
Is that the correct way to run Ignite in with Spring Boot?
Apache Ignite Loading Twice with Spring-Boot?

Currently there is now direct integration with Spring Boot, so you should manually start a node within the application using Ignition.start() method.

I've got test project spring boot + ignite. I hope that will help:
github project

For me use case, i create a bean and start ignite inside it after that return the ignite. it will start ignite only one time at start time.

Use following steps to integrate ignite with spring boot.
1. Add following dependency in POM.xml file
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-core</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-spring</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-spring-data</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-indexing</artifactId>
<version>${ignite.version}</version>
</dependency>
2. Create the Ignite bean instance
#Bean
public Ignite igniteInstance() {
IgniteConfiguration cfg = new IgniteConfiguration();
Ignite igniteInst= Ignition.start(cfg);
return igniteInst;
}
3. Configure the repository
#RepositoryConfig(cacheName = "cacheName")
public interface RepositoryName extends IgniteRepository<V, K> {
}
4. Autowired the RepositoryName interface which extends the IgniteRepository in service layer
#Component
public class ServiceImpl
#Autowired
RepositoryName repositoryName;
}
5. You can use 5th steps apart from 4th steps to inject the ignite bean in service layer
#Component
public class ServiceImpl {
#Autowired
Ignite ignite;
void abcMethod(){
IgniteCache<K, V> igniteCache = ignite.cache("CacheName");
}
}

Related

How to integrate Resilience4j metrics to Micrometer in Camel Spring Boot

I am using Camel with Spring Boot and Micrometer. In one of my routes I am using a circuitbreaker with Resilience4j:
.circuitBreaker()
.resilience4jConfiguration()
.timeoutEnabled(true)
.timeoutDuration(2000)
.end()
I am using Micrometer managed by Spring. Before moving to resilience4j with Hystrix I could simply bind it to my Micrometer registry:
#Configuration()
public class MetricsRegistryBuilder {
#Bean
HystrixMetricsBinder registerHystrixMetricsBinder() {
return new HystrixMetricsBinder();
}
}
For Resilience4j there does not exist a binder unfortunately. There is some documentation about how to bind the Resilience4j CircuitBreakerRegistry to Micrometer:
https://resilience4j.readme.io/docs/micrometer
and also how to do it with Spring:
https://resilience4j.readme.io/docs/getting-started-3
I tried to simply autowire the Resilience4j CircuitBreakerRegistry to Micrometer:
#Configuration()
public class MetricsRegistryBuilder {
#Autowired
private CircuitBreakerRegistry circuitBreakerRegistry;
}
Unfortunately Spring does not find the CircuitBreakerRegistry Bean.
Therefore my question is how to bind the CircuitBreakerRegistry, or more abstract the metrics from Resilience4j, to Micrometer when using Camel with Spring?
The only other possible solution I could think of is to manage all Resilience4j configuration manually, define the beans, and hand it over to my Camel configuration. This though seems to me to a lot of work and boilerplate code considering the simple task of binding my Resilience4j metrics.
I am using the following versions:
Camel 3.4.3
Spring 2.3.3.RELEASE
Micormeter 1.5.4
Also I am using camel spring boot starter dependencies:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-micrometer-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-resilience4j-starter</artifactId>
</dependency>
Why can't you use our Spring Boot 2 Starter?
The Starter autoconfigures the CircuitBreakerRegistry, MeterRegistry and allows external configuration.

What kind of view technology used in spring boot by default

What kind of view technology used in spring boot by default when I add the 'Spring Boot Web Starter'.
If I want to use the JSP, I need to include the 'tomcat-embed-jasper' or 'Spring Boot Thymeleaf Starter' for thymeleaf templates. So I would like to know the default view technology of 'Spring Boot Web Starter'
By default there is no view You need to configure and add their dependencies.If You are using Spring Boot older versions then You can refer above answer but if You are using Spring Boot 2 then add on more dependency for thymeleaf-
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
JSP is supported by Spring out-of-the-box.
It can be configured like this
#EnableWebMvc
#Configuration
public class ApplicationConfiguration implements WebMvcConfigurer {
#Bean
public ViewResolver jspViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setPrefix("/WEB-INF/views/");
bean.setSuffix(".jsp");
return bean;
}
}
or in properties file
spring.mvc.view.prefix: /WEB-INF/views/
spring.mvc.view.suffix: .jsp
For Thymeleaf
Spring Boot will provide auto-configuration for Thymeleaf with below dependency in pom.xml
Please make a note of version used. Also you might need to provide view properties like above
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>1.5.6.RELEASE</version>
</dependency>

Jackson Object Mapper not working when extended configuration provided but working when providing class level/field level annotations in Spring Boot

The below object mapper configuration is not working when I add jjwt security to spring boot application.
#Configuration
public class CustomObjectMapper extends ObjectMapper {
/**
* Default serial version id generated.
*/
private static final long serialVersionUID = 1L;
public CustomObjectMapper() {
this.setSerializationInclusion(Include.NON_EMPTY);
this.registerModule(new ThreeTenModule());
this.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
}
}
Security dependencies added here
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency>
where as the below Jackson annotations are working on class/field levels.
#JsonInclude(Include.NON_EMPTY)
Why the bean configured custom object mapper not been used for serialization & deserialization? Any other libraries configured object mapper overriding my custom mapper?
After a long investigation, i have noticed #EnableWebMvc annotated configuration bean available in one dependent library. And got to know from here that #EnableWebMvc disables Spring Boot's MVC auto-configuration, thus giving complete control to provide customer MVC configuration. HTTP Message Convertors will also be included in Spring MVC component which in turn disables my custom jackson object mapper configuration.
PS: As jjwt imports jackson databind dependency by default, it fell in my suspect list. Feel good that i could RCA. Thanks.

Spring-boot not respecting liquibase properties

I'm in the process of setting up liquibase to manage my database in a new spring boot application. I need the liquibase dependency in my classpath to reset the database state after certain integration tests run. During my tests I do not want liquibase to be enabled via spring auto config during application context initialization. I've tried adding liquibase.enabled = false to the application.properties, however when I debug the LiquibaseAutoConfiguration class it appears that enabled is always set to true.
I'm not new to spring, but I am new to spring-boot's auto configuration. Has anyone had issues with spring boot not respecting properties in application.properties?
My setup is fairly minimal:
Relevant code snippets:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = { SpringBootClass.class })
public class databaseTests{
#Before
public void setup() throws LiquibaseException, SQLException {
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(dataSource.getConnection()));
Liquibase liquibase = new Liquibase("db/changelog/db.changelog-master.yaml", new FileSystemResourceAccessor("src/main/resources/"),database );
liquibase.dropAll();
liquibase.update("test");
}
..
}
#SpringBootApplication
#Import({ DataSourceConfig.class, HibernateConfig.class, OauthConfig.class })
#EnableConfigurationProperties
public class SpringBootClass {
..
}
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.6.RELEASE</version>
<!-- <liquibase.version>3.3.5</liquibase.version> -->
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<scope>test</scope>
</dependency>
If you want your tests to consume application.properties you need to run them as a Spring Boot application. Your use of #ContextConfiguration means that you're currently running them as a vanilla Spring Framework application. Replace the #ContextConfiguration annotation with #SpringApplicationConfiguration.
Should have RTFM...
from spring boot documentation
ConfigFileApplicationContextInitializer is an
ApplicationContextInitializer that can apply to your tests to load
Spring Boot application.properties files. You can use this when you
don’t need the full features provided by
#SpringApplicationConfiguration.
#ContextConfiguration(classes = Config.class,
initializers = ConfigFileApplicationContextInitializer.class)
Changing my config to use the initializer worked.

JMX on Spring Boot project

I have annotated a class as follows:
#ManagedResource(objectName="com.myproject.bean.jmx:name=JMXSolrIndexerBean",
description="Index Solr Operations")
public class JMXSolrIndexerBean {
....
}
My pom has the following dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jmx</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
I can't find my MBean in the JConsole... are there any configuration steps I am missing?
Two things:
You don't need the spring-integrtation-jmx dependency to make that work, the actuator starter is enough
Your class needs to be a spring bean if you want Spring Boot to auto-detect JMX annotation on them. So adding #Component on your JMXSolrIndexerBean is all that's needed as long as it is located in a package that is processed by component scan
In other words, that class of yours is just a pojo that spring know nothings about. #ManagedResource is not a stereotype that turns that class in a Spring Bean.

Resources