Skip to content

Commit e557106

Browse files
committed
Add microraft code.
1 parent 5681351 commit e557106

File tree

10 files changed

+335
-3
lines changed

10 files changed

+335
-3
lines changed

raft/mircoraft/pom.xml

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,105 @@
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66
<parent>
7-
<groupId>ie.emeraldjava</groupId>
8-
<artifactId>raft</artifactId>
9-
<version>0.0.1-SNAPSHOT</version>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.7.18</version>
10+
<relativePath/> <!-- lookup parent from repository -->
1011
</parent>
1112

1213
<artifactId>mircoraft</artifactId>
14+
<packaging>jar</packaging>
1315

1416
<properties>
1517
<maven.compiler.source>21</maven.compiler.source>
1618
<maven.compiler.target>21</maven.compiler.target>
1719
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1820
</properties>
1921

22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-web</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-actuator</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.projectlombok</groupId>
33+
<artifactId>lombok</artifactId>
34+
<version>1.18.34</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-test</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>io.microraft</groupId>
44+
<artifactId>microraft</artifactId>
45+
<version>0.7</version>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.openapitools</groupId>
50+
<artifactId>jackson-databind-nullable</artifactId>
51+
<version>0.2.1</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.springdoc</groupId>
55+
<artifactId>springdoc-openapi-ui</artifactId>
56+
<version>1.7.0</version>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>org.webjars</groupId>
61+
<artifactId>webjars-locator-core</artifactId>
62+
<version>0.59</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.webjars</groupId>
66+
<artifactId>bootstrap</artifactId>
67+
<version>5.3.3</version>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.webjars</groupId>
71+
<artifactId>jquery</artifactId>
72+
<version>3.7.1</version>
73+
</dependency>
74+
</dependencies>
75+
76+
<build>
77+
<plugins>
78+
79+
<plugin>
80+
<groupId>org.openapitools</groupId>
81+
<artifactId>openapi-generator-maven-plugin</artifactId>
82+
<version>6.2.1</version>
83+
<executions>
84+
<execution>
85+
<goals>
86+
<goal>generate</goal>
87+
</goals>
88+
<configuration>
89+
<skipValidateSpec>true</skipValidateSpec>
90+
<inputSpec>./src/main/resources/openapi/microraft.yml</inputSpec>
91+
<generatorName>spring</generatorName>
92+
<configOptions>
93+
<openApiNullable>false</openApiNullable>
94+
<interfaceOnly>true</interfaceOnly>
95+
</configOptions>
96+
</configuration>
97+
</execution>
98+
</executions>
99+
</plugin>
100+
101+
<plugin>
102+
<groupId>org.springframework.boot</groupId>
103+
<artifactId>spring-boot-maven-plugin</artifactId>
104+
</plugin>
105+
</plugins>
106+
</build>
107+
20108
</project>

raft/mircoraft/readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# microraft
2+
3+
https://github.com/MicroRaft/MicroRaft
4+
5+
## 08-09-2024
6+
7+
https://github.com/MicroRaft/MicroRaft
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package ie.emeraldjava.raft;
2+
3+
import ie.emeraldjava.raft.service.RaftService;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.CommandLineRunner;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
10+
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
11+
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
12+
13+
@SpringBootApplication(exclude = {
14+
DataSourceAutoConfiguration.class,
15+
DataSourceTransactionManagerAutoConfiguration.class,
16+
HibernateJpaAutoConfiguration.class})
17+
@Slf4j
18+
public class MicroRaftApp implements CommandLineRunner {
19+
20+
public static void main(String[] args) {
21+
SpringApplication.run(MicroRaftApp.class, args);
22+
}
23+
24+
@Autowired
25+
private RaftService raftService;
26+
27+
@Override
28+
public void run(String... args) throws Exception {
29+
log.info("MicroRaftApp: {}",raftService);
30+
}
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ie.emeraldjava.raft.config;
2+
3+
import ie.emeraldjava.raft.interceptor.RaftHeaderInterceptor;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
7+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
8+
9+
/**
10+
* https://www.baeldung.com/spring-extract-custom-header-request
11+
*/
12+
@Configuration
13+
public class HeaderInterceptorConfig implements WebMvcConfigurer {
14+
15+
@Override
16+
public void addInterceptors(final InterceptorRegistry registry) {
17+
registry.addInterceptor(operatorInterceptor());
18+
}
19+
20+
@Bean
21+
public RaftHeaderInterceptor operatorInterceptor() {
22+
return new RaftHeaderInterceptor();
23+
}
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ie.emeraldjava.raft.config;
2+
3+
import ie.emeraldjava.raft.microraft.RaftEndpoint;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.beans.factory.FactoryBean;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
/**
9+
* Creates RaftEndpoints
10+
*/
11+
@Configuration
12+
@Slf4j
13+
public class RaftEndpointFactory implements FactoryBean<RaftEndpoint> {
14+
15+
@Override
16+
public RaftEndpoint getObject() throws Exception {
17+
// Your custom logic to create and configure the bean
18+
RaftEndpoint myBean = RaftEndpoint.newEndpoint();
19+
log.info("new RaftEndpoint:{}",myBean.getId());
20+
return myBean;
21+
}
22+
23+
@Override
24+
public Class<?> getObjectType() {
25+
return RaftEndpoint.class;
26+
}
27+
28+
@Override
29+
public boolean isSingleton() {
30+
return false; // Return 'true' if you want a singleton bean, 'false' for a prototype bean
31+
}
32+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ie.emeraldjava.raft.interceptor;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.web.servlet.HandlerInterceptor;
5+
6+
import javax.servlet.http.HttpServletRequest;
7+
import javax.servlet.http.HttpServletResponse;
8+
9+
/**
10+
*
11+
* https://www.baeldung.com/spring-extract-custom-header-request
12+
*/
13+
@Slf4j
14+
public class RaftHeaderInterceptor implements HandlerInterceptor {
15+
//private final OperatorHolder operatorHolder;
16+
17+
@Override
18+
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
19+
String operator = request.getHeader("raft-operator");
20+
log.info("operator: {}", operator);
21+
// operatorHolder.setOperator(operator);
22+
return true;
23+
}
24+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package ie.emeraldjava.raft.microraft;
2+
3+
import java.util.concurrent.atomic.AtomicInteger;
4+
5+
import static java.util.Objects.requireNonNull;
6+
7+
public class RaftEndpoint implements io.microraft.RaftEndpoint {
8+
9+
private static final AtomicInteger ID_GENERATOR = new AtomicInteger();
10+
11+
public static RaftEndpoint newEndpoint() {
12+
return new RaftEndpoint("node" + ID_GENERATOR.incrementAndGet());
13+
}
14+
15+
private final String id;
16+
17+
private RaftEndpoint(String id) {
18+
this.id = requireNonNull(id);
19+
}
20+
21+
@Override
22+
public Object getId() {
23+
return id;
24+
}
25+
26+
@Override
27+
public int hashCode() {
28+
return id.hashCode();
29+
}
30+
31+
@Override
32+
public boolean equals(Object o) {
33+
if (this == o) {
34+
return true;
35+
}
36+
if (o == null || getClass() != o.getClass()) {
37+
return false;
38+
}
39+
40+
RaftEndpoint that = (RaftEndpoint) o;
41+
42+
return id.equals(that.id);
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return "LocalRaftEndpoint{" + "id=" + id + '}';
48+
}
49+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ie.emeraldjava.raft.microraft.register;
2+
3+
import io.microraft.statemachine.StateMachine;
4+
5+
import java.util.List;
6+
import java.util.function.Consumer;
7+
8+
9+
public class AtomicRegister implements StateMachine {
10+
11+
@Override
12+
public Object runOperation(long commitIndex, Object operation) {
13+
if (operation instanceof NewTermOperation) {
14+
return null;
15+
}
16+
17+
throw new IllegalArgumentException("Invalid operation: " + operation + " at commit index: " + commitIndex);
18+
}
19+
20+
@Override
21+
public Object getNewTermOperation() {
22+
return new NewTermOperation();
23+
}
24+
25+
@Override
26+
public void takeSnapshot(long commitIndex, Consumer<Object> snapshotChunkConsumer) {
27+
throw new UnsupportedOperationException();
28+
}
29+
30+
@Override
31+
public void installSnapshot(long commitIndex, List<Object> snapshotChunks) {
32+
throw new UnsupportedOperationException();
33+
}
34+
35+
private static class NewTermOperation {
36+
}
37+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ie.emeraldjava.raft.service;
2+
3+
import ie.emeraldjava.raft.microraft.RaftEndpoint;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.stereotype.Component;
6+
7+
import javax.annotation.PostConstruct;
8+
import javax.annotation.Resource;
9+
10+
@Component
11+
@Slf4j
12+
public class RaftService {
13+
14+
@Resource
15+
private RaftEndpoint raftEndpoint;
16+
17+
@Resource
18+
private RaftEndpoint raftEndpoint2;
19+
20+
@PostConstruct
21+
public void xx() {
22+
log.info("Raft service started");
23+
log.info("raftEndpoint {}",raftEndpoint.toString());
24+
log.info("raftEndpoint2 {}",raftEndpoint2.toString());
25+
}
26+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
openapi: 3.0.3
2+
info:
3+
title: Banking API Specification for account operations
4+
description: |-
5+
A simple banking API that allows two operations:
6+
- get account balance given account number
7+
- deposit amount to a account
8+
version: 1.0-SNAPSHOT
9+
servers:
10+
- url: https://testenvironment.org/api/v1
11+
- url: https://prodenvironment.org/api/v1
12+
tags:
13+
- name: accounts
14+
description: Operations between bank accounts

0 commit comments

Comments
 (0)