Skip to content

Commit 0f1d04a

Browse files
committed
#68 Add service discovery starter
1 parent 62ff65a commit 0f1d04a

File tree

16 files changed

+87
-129
lines changed

16 files changed

+87
-129
lines changed

backend/config/crypto/keys.bash

+11-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ ENV="$2"
88
ALIAS="itmo-dating"
99
ALIAS_BACKEND="$ALIAS-backend"
1010
VALIDITY=1
11-
BACKEND_INSTALL_PATH="foundation/src/main/resources/keystore"
11+
FOUNDATION_INSTALL_PATH="foundation/src/main/resources/keystore"
12+
STARTER_SERVICE_DISCOVERY_INSTALL_PATH="starter-service-discovery/src/main/resources/keystore"
1213
CONSUL_INSTALL_PATH="consul/config"
1314
GATEWAY_INSTALL_PATH="gateway/src/main/resources/keystore"
1415
PASSWORD="$ITMO_DATING_KEY_STORE_PASSWORD"
@@ -81,14 +82,13 @@ function distribute() {
8182
echo "Phase: Distribute"
8283

8384
echo "Copying package to the backend..."
84-
copy "$BACKEND_INSTALL_PATH" "$ALIAS_BACKEND.p12"
85-
copy "$BACKEND_INSTALL_PATH" "$ALIAS_BACKEND.jks"
86-
copy "$BACKEND_INSTALL_PATH" "$ALIAS_BACKEND.crt"
85+
copy "$FOUNDATION_INSTALL_PATH" "$ALIAS_BACKEND.p12"
86+
87+
echo "Copying package to the starter-service-discovery..."
88+
copy "$STARTER_SERVICE_DISCOVERY_INSTALL_PATH" "$ALIAS_BACKEND.jks"
8789

8890
echo "Copying package to the gateway..."
8991
copy "$GATEWAY_INSTALL_PATH" "$ALIAS_BACKEND.p12"
90-
copy "$GATEWAY_INSTALL_PATH" "$ALIAS_BACKEND.jks"
91-
copy "$GATEWAY_INSTALL_PATH" "$ALIAS_BACKEND.crt"
9292

9393
echo "Copying keys to the consul..."
9494
copy "$CONSUL_INSTALL_PATH" "$ALIAS_BACKEND.key"
@@ -107,15 +107,14 @@ function remove() {
107107
function clear() {
108108
echo "Phase: Clear"
109109

110-
echo "Removing package from the backend..."
111-
remove "$BACKEND_INSTALL_PATH" "$ALIAS_BACKEND.p12"
112-
remove "$BACKEND_INSTALL_PATH" "$ALIAS_BACKEND.jks"
113-
remove "$BACKEND_INSTALL_PATH" "$ALIAS_BACKEND.crt"
110+
echo "Removing package from the foundation..."
111+
remove "$FOUNDATION_INSTALL_PATH" "$ALIAS_BACKEND.p12"
112+
113+
echo "Removing package from the starter-service-discovery..."
114+
remove "$STARTER_SERVICE_DISCOVERY_INSTALL_PATH" "$ALIAS_BACKEND.jks"
114115

115116
echo "Removing package from the gateway..."
116117
remove "$GATEWAY_INSTALL_PATH" "$ALIAS_BACKEND.p12"
117-
remove "$GATEWAY_INSTALL_PATH" "$ALIAS_BACKEND.jks"
118-
remove "$GATEWAY_INSTALL_PATH" "$ALIAS_BACKEND.crt"
119118

120119
echo "Removing keys from the consul..."
121120
remove "$CONSUL_INSTALL_PATH" "$ALIAS_BACKEND.key"

backend/foundation/build.gradle.kts

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ plugins {
55
}
66

77
dependencies {
8+
api(project(":starter-service-discovery"))
9+
810
api(libs.org.springframework.boot.spring.boot)
911
api(libs.org.springframework.boot.spring.boot.starter.webflux)
1012
api(libs.org.springframework.boot.spring.boot.starter.data.r2dbc)

backend/foundation/src/main/kotlin/ru/ifmo/se/dating/spring/SpringConfiguration.kt

-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package ru.ifmo.se.dating.spring
22

3-
import org.springframework.cloud.client.discovery.EnableDiscoveryClient
43
import org.springframework.context.annotation.Bean
54
import org.springframework.context.annotation.Configuration
6-
import org.springframework.scheduling.annotation.EnableScheduling
75
import java.time.Clock
86

97
@Configuration
10-
@EnableScheduling
11-
@EnableDiscoveryClient
128
class SpringConfiguration {
139
@Bean
1410
fun clock(): Clock = Clock.systemDefaultZone()

backend/foundation/src/main/resources/application-foundation-test.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
spring:
2-
cloud:
3-
consul:
4-
enabled: false
2+
config:
3+
import: application-service-discovery-test.yml
54
security:
65
auth:
76
token:

backend/foundation/src/main/resources/application-foundation.yml

+7-32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
spring:
2+
config:
3+
import: application-service-discovery.yml
4+
datasource:
5+
driver-class-name: org.postgresql.Driver
6+
liquibase:
7+
change-log: database/changelog.sql
18
server:
29
ssl:
310
enabled: true
@@ -6,38 +13,6 @@ server:
613
key-store-password: ${KEY_STORE_PASSWORD}
714
protocol: TLSv1.3
815
enabled-protocols: TLSv1.3
9-
spring:
10-
cloud:
11-
consul:
12-
scheme: https
13-
host: server.dc1.consul
14-
port: 8501
15-
tls:
16-
key-store-instance-type: JKS
17-
certificate-path: keystore/itmo-dating-backend.jks
18-
certificate-password: ${server.ssl.key-store-password}
19-
key-store-path: keystore/itmo-dating-backend.jks
20-
key-store-password: ${server.ssl.key-store-password}
21-
discovery:
22-
enabled: true
23-
scheme: https
24-
instance-id: ${spring.application.name}:${HOSTNAME}
25-
heartbeat:
26-
enabled: true
27-
datasource:
28-
driver-class-name: org.postgresql.Driver
29-
liquibase:
30-
change-log: database/changelog.sql
31-
management:
32-
health:
33-
consul:
34-
enabled: true
35-
diskspace:
36-
enabled: false
37-
endpoints:
38-
web:
39-
exposure:
40-
include: health
4116
springdoc:
4217
api-docs:
4318
path: /openapi

backend/gateway/build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ plugins {
33
}
44

55
dependencies {
6+
implementation(project(":starter-service-discovery"))
7+
68
implementation(libs.org.springframework.boot.spring.boot)
7-
implementation(libs.org.springframework.boot.spring.boot.starter.actuator)
89
implementation(libs.org.springframework.cloud.spring.cloud.starter.gateway)
9-
implementation(libs.org.springframework.cloud.spring.cloud.starter.consul.discovery)
1010

1111
implementation(libs.org.springdoc.springdoc.openapi.starter.webflux.ui)
1212

backend/gateway/src/main/kotlin/ru/ifmo/se/dating/gateway/Application.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package ru.ifmo.se.dating.gateway
22

33
import org.springframework.boot.autoconfigure.SpringBootApplication
44
import org.springframework.boot.runApplication
5-
import org.springframework.cloud.client.discovery.EnableDiscoveryClient
5+
import org.springframework.context.annotation.ComponentScan
66

77
@SpringBootApplication
8-
@EnableDiscoveryClient
8+
@ComponentScan(basePackages = ["ru.ifmo.se.dating"])
99
class Application
1010

1111
fun main(args: Array<String>) {

backend/gateway/src/main/kotlin/ru/ifmo/se/dating/gateway/KeyStoreExtractor.kt

-30
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
spring:
2-
cloud:
3-
consul:
4-
enabled: false
2+
config:
3+
import: application-service-discovery-test.yml

backend/gateway/src/main/resources/application.yml

+10-34
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
server:
2-
ssl:
3-
enabled: true
4-
key-store-type: PKCS12
5-
key-store: classpath:keystore/itmo-dating-backend.p12
6-
key-store-password: ${KEY_STORE_PASSWORD}
7-
protocol: TLSv1.3
8-
enabled-protocols: TLSv1.3
91
spring:
2+
config:
3+
import: application-service-discovery.yml
104
application:
115
name: gateway
126
cloud:
@@ -97,32 +91,14 @@ spring:
9791
- Method=GET
9892
- Path=/api/locations
9993

100-
consul:
101-
scheme: https
102-
host: server.dc1.consul
103-
port: 8501
104-
tls:
105-
key-store-instance-type: JKS
106-
certificate-path: keystore/itmo-dating-backend.jks
107-
certificate-password: ${server.ssl.key-store-password}
108-
key-store-path: keystore/itmo-dating-backend.jks
109-
key-store-password: ${server.ssl.key-store-password}
110-
discovery:
111-
enabled: true
112-
scheme: https
113-
instance-id: ${spring.application.name}:${HOSTNAME}
114-
heartbeat:
115-
enabled: true
116-
management:
117-
health:
118-
consul:
119-
enabled: true
120-
diskspace:
121-
enabled: false
122-
endpoints:
123-
web:
124-
exposure:
125-
include: health
94+
server:
95+
ssl:
96+
enabled: true
97+
key-store-type: PKCS12
98+
key-store: classpath:keystore/itmo-dating-backend.p12
99+
key-store-password: ${KEY_STORE_PASSWORD}
100+
protocol: TLSv1.3
101+
enabled-protocols: TLSv1.3
126102
springdoc:
127103
swagger-ui:
128104
path: /swagger-ui.html

backend/settings.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ include(
1111
":authik",
1212
":matchmaker",
1313
":people",
14+
":starter-service-discovery",
1415
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
plugins {
2+
id("buildlogic.kotlin-library-conventions")
3+
kotlin("plugin.spring")
4+
}
5+
6+
dependencies {
7+
api(libs.org.springframework.boot.spring.boot.starter.actuator)
8+
api(libs.org.springframework.cloud.spring.cloud.starter.consul.discovery)
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package ru.ifmo.se.dating.spring.consul
2+
3+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient
4+
5+
@EnableDiscoveryClient
6+
class ConsulConfiguration
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
package ru.ifmo.se.dating.spring.security.ssl
1+
package ru.ifmo.se.dating.spring.consul
22

33
import jakarta.annotation.PostConstruct
44
import org.springframework.beans.factory.annotation.Value
55
import org.springframework.context.annotation.Profile
66
import org.springframework.stereotype.Component
7-
import ru.ifmo.se.dating.logging.Log
8-
import ru.ifmo.se.dating.logging.Log.Companion.autoLog
97
import java.io.File
108
import java.nio.file.Files
119
import java.nio.file.Paths
@@ -20,19 +18,15 @@ class KeyStoreExtractor(
2018
@Value("\${spring.cloud.consul.tls.key-store-path}")
2119
private val keystorePath: String,
2220
) {
23-
private val log = Log.autoLog()
24-
2521
@PostConstruct
2622
fun extractCertificate() {
2723
extract(certificatePath)
2824
extract(keystorePath)
2925
}
3026

3127
private fun extract(path: String) {
32-
log.info("Extracting file '$path' from the jar...")
3328
val inputStream = javaClass.classLoader.getResourceAsStream(path)!!
3429
File(path).parentFile?.mkdirs()
3530
Files.copy(inputStream, Paths.get(path), StandardCopyOption.REPLACE_EXISTING)
36-
log.info("File '$path' extracted successfully!")
3731
}
3832
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
spring:
2+
cloud:
3+
consul:
4+
enabled: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
spring:
2+
cloud:
3+
consul:
4+
scheme: https
5+
host: server.dc1.consul
6+
port: 8501
7+
tls:
8+
key-store-instance-type: JKS
9+
certificate-path: keystore/itmo-dating-backend.jks
10+
certificate-password: ${server.ssl.key-store-password}
11+
key-store-path: keystore/itmo-dating-backend.jks
12+
key-store-password: ${server.ssl.key-store-password}
13+
discovery:
14+
enabled: true
15+
scheme: https
16+
instance-id: ${spring.application.name}:${HOSTNAME}
17+
heartbeat:
18+
enabled: true
19+
management:
20+
health:
21+
consul:
22+
enabled: true
23+
diskspace:
24+
enabled: false
25+
endpoints:
26+
web:
27+
exposure:
28+
include: health

0 commit comments

Comments
 (0)