Skip to content

Commit de06a28

Browse files
Rodrigo Dos SantosRodrigo Dos Santos
authored andcommitted
Fix Cors, added call to service
1 parent 1f35629 commit de06a28

File tree

30 files changed

+1045
-199
lines changed

30 files changed

+1045
-199
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,19 @@ Configuration(`Deployment/Services`) for Kubernetes look at [.github/workflows/k
299299

300300
### GRAALVM
301301

302+
First need to compile project
303+
304+
`mvn clean package`
305+
302306
To run an app native using Spring Native
303307

308+
`
309+
mvn spring-boot:build-image -pl authentication-service
310+
`
311+
312+
PS: Valid modules are `authentication-service, person-service, user-service, kotlin-service, edge-server and edge-server`.
313+
314+
Or to run with GraalVM Plugin.
304315
`
305316
mvn package -Pnative -q -pl authentication-service -am -B -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -DskipTests
306317
`
@@ -359,7 +370,8 @@ Access it [Swagger UI](http://localhost:{SERVICE_PORT}/swagger-ui.html) - `http:
359370
* [X] Replace Eureka/Spring Config Server to Consul
360371
* [X] Add Query DSL
361372
* [X] Java - Migrate Zuul to Spring Cloud Gateway
362-
373+
* [ ] React - Need to fix new layout
374+
* [ ] Go - Fix call to another service
363375

364376
### References
365377
[Pattern Microservice Architecture](https://microservices.io/patterns/microservices.html)

authentication-service/src/main/java/com/microservice/authentication/AuthenticationServiceApplication.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2424
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
25+
import org.springframework.boot.info.BuildProperties;
2526
import org.springframework.boot.info.GitProperties;
2627
import org.springframework.context.annotation.Bean;
2728
import org.springframework.context.annotation.Primary;
@@ -66,6 +67,12 @@ static BeanFactoryPostProcessor removeErrorSecurityFilter() {
6667
((DefaultListableBeanFactory)beanFactory).removeBeanDefinition("errorPageSecurityInterceptor");
6768
}
6869

70+
@Bean
71+
@ConditionalOnMissingBean
72+
BuildProperties buildProperties() {
73+
return new BuildProperties(new Properties());
74+
}
75+
6976
@ConditionalOnProperty(prefix = "configuration", name = "initialLoad", havingValue = "true", matchIfMissing = true)
7077
@Bean
7178
CommandLineRunner runner(AuthenticationCommonRepository authenticationCommonRepository,

docker/docker-compose.yml

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,24 @@ services:
3030
- ./spring-cloud-config:/spring-cloud-config:ro
3131

3232
gateway:
33-
image: edge-server
33+
image: edge-server:latest
3434
container_name: gateway
3535
environment:
3636
- SPRING_REDIS_HOST=redisdb
37-
- SPRING_PROFILES_ACTIVE=dev
38-
# - CONSUL_URL=service-discovery:8500
37+
- SPRING_PROFILES_ACTIVE=consul,dev
38+
- CONSUL_URL=service-discovery:8500
3939
- SPRING_CONFIG_IMPORT=consul:service-discovery:8500
4040
- SERVER_PORT=8080
4141
- SPRING_DATA_MONGODB_URI=mongodb://mongodb-datasource:27017
4242
- SPRING_DATA_MONGODB_DATABASE=docker
4343
- MANAGEMENT_ENDPOINTS_WEB_CORS_ALLOW_CREDENTIALS=false
4444
- DEBUG=true
4545
- LOGGING_LEVEL_COM_SPRINGBOOT=debug
46-
- LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_WEB=debug
46+
- LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_SECURITY=trace
4747
- OPENTRACING_JAEGER_UDP_SENDER_HOST=jaeger
4848
- SPRING_MAIN_ALLOW_CIRCULAR_REFERENCES=true
49+
- SPRING_MAIN_WEB_APPLICATION_TYPE=reactive
50+
# - SPRING_CLOUD_GATEWAY_GLOBAL_CORS_ADD_TO_SIMPLE_URL_HANDLER_MAPPING=true
4951
depends_on:
5052
- service-discovery
5153
ports:
@@ -192,11 +194,12 @@ services:
192194
- post-api
193195

194196
person-api:
195-
image: person-service
197+
image: person-service:latest
196198
container_name: person-api
197199
environment:
198-
- SPRING_PROFILES_ACTIVE=dev
200+
- SPRING_PROFILES_ACTIVE=consul,dev
199201
- CONSUL_URL=service-discovery:8500
202+
- SPRING_CONFIG_IMPORT=consul:service-discovery:8500
200203
- SERVER_PORT=8082
201204
- SPRING_CLOUD_KUBERNETES_ENABLED=false
202205
- SPRING_DATA_MONGODB_URI=mongodb://mongodb-datasource:27017
@@ -224,8 +227,9 @@ services:
224227
image: kotlin-service
225228
container_name: task-api
226229
environment:
227-
- SPRING_PROFILES_ACTIVE=dev
230+
- SPRING_PROFILES_ACTIVE=consul,dev
228231
- CONSUL_URL=service-discovery:8500
232+
- SPRING_CONFIG_IMPORT=consul:service-discovery:8500
229233
- SERVER_PORT=9092
230234
- SPRING_CLOUD_KUBERNETES_ENABLED=false
231235
- SPRING_DATA_MONGODB_URI=mongodb://mongodb-datasource:27017
@@ -273,11 +277,12 @@ services:
273277
- task-api
274278

275279
user-api:
276-
image: user-service
280+
image: user-service:latest
277281
container_name: user-api
278282
environment:
279-
- SPRING_PROFILES_ACTIVE=dev
283+
- SPRING_PROFILES_ACTIVE=consul,dev
280284
- CONSUL_URL=service-discovery:8500
285+
- SPRING_CONFIG_IMPORT=consul:service-discovery:8500
281286
- SERVER_PORT=8083
282287
- SPRING_CLOUD_KUBERNETES_ENABLED=false
283288
- SPRING_DATA_MONGODB_URI=mongodb://mongodb-datasource:27017
@@ -301,7 +306,7 @@ services:
301306
- user-api
302307

303308
authentication-api:
304-
image: authentication-service #fielcapao/microservices-design-patterns-authentication-service
309+
image: authentication-service:latest #fielcapao/microservices-design-patterns-authentication-service
305310
container_name: authentication-api
306311
environment:
307312
- SPRING_PROFILES_ACTIVE=consul,dev
@@ -427,5 +432,46 @@ services:
427432
aliases:
428433
- jaeger
429434

435+
zookeeper:
436+
image: antrea/confluentinc-zookeeper:6.2.0
437+
hostname: zookeeper
438+
container_name: zookeeper
439+
ports:
440+
- '2181:2181'
441+
environment:
442+
ZOOKEEPER_CLIENT_PORT: 2181
443+
ZOOKEEPER_TICK_TIME: 2000
444+
445+
broker:
446+
image: antrea/confluentinc-kafka:6.2.0
447+
hostname: broker
448+
container_name: broker
449+
depends_on:
450+
- zookeeper
451+
ports:
452+
- '9092:9092'
453+
- '9101:9101'
454+
environment:
455+
KAFKA_BROKER_ID: 1
456+
KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
457+
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
458+
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:29092,PLAINTEXT_HOST://localhost:9092
459+
# NOTE: Not supported by current container
460+
# KAFKA_METRIC_REPORTERS: io.confluent.metrics.reporter.ConfluentMetricsReporter
461+
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
462+
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
463+
KAFKA_CONFLUENT_LICENSE_TOPIC_REPLICATION_FACTOR: 1
464+
KAFKA_CONFLUENT_BALANCER_TOPIC_REPLICATION_FACTOR: 1
465+
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
466+
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
467+
KAFKA_JMX_PORT: 9101
468+
KAFKA_JMX_HOSTNAME: localhost
469+
# TODO: Uncomment once enable schema registry
470+
# KAFKA_CONFLUENT_SCHEMA_REGISTRY_URL: http://schema-registry:8081
471+
CONFLUENT_METRICS_REPORTER_BOOTSTRAP_SERVERS: broker:29092
472+
CONFLUENT_METRICS_REPORTER_TOPIC_REPLICAS: 1
473+
CONFLUENT_METRICS_ENABLE: 'true'
474+
CONFLUENT_SUPPORT_CUSTOMER_ID: 'anonymous'
475+
430476
networks:
431477
net:

edge-server/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
<dependency>
9999
<groupId>javax.servlet</groupId>
100100
<artifactId>javax.servlet-api</artifactId>
101-
<scope>provided</scope>
101+
<!-- <scope>provided</scope>-->
102102
</dependency>
103103

104104
<dependency>

edge-server/src/main/java/com/springboot/edgeserver/EdgeServerApplication.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
1414
import org.springframework.context.annotation.Bean;
1515
import org.springframework.context.annotation.Primary;
16-
import org.springframework.core.env.Environment;
17-
import org.springframework.core.env.Profiles;
1816
import org.springframework.data.redis.connection.RedisConnectionFactory;
1917
import org.springframework.data.redis.core.RedisTemplate;
2018
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
@@ -26,9 +24,6 @@
2624
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
2725
import org.springframework.session.web.http.CookieSerializer;
2826
import org.springframework.session.web.http.DefaultCookieSerializer;
29-
import org.springframework.web.cors.CorsConfiguration;
30-
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
31-
import org.springframework.web.filter.CorsFilter;
3227
import org.springframework.web.reactive.config.EnableWebFlux;
3328

3429
@Slf4j
@@ -74,25 +69,6 @@ public CookieSerializer cookieSerializer() {
7469
return serializer;
7570
}
7671

77-
@Bean
78-
CorsFilter corsWebFilter(Environment environment) {
79-
log.debug("active profiles: {}", environment.getActiveProfiles());
80-
CorsConfiguration corsConfig = new CorsConfiguration();
81-
corsConfig.setAllowCredentials(true);
82-
if (environment.acceptsProfiles(Profiles.of("prod"))) {
83-
corsConfig.addAllowedOrigin("https://spendingbetter.com");
84-
} else {
85-
corsConfig.addAllowedOrigin("*");
86-
}
87-
corsConfig.addAllowedHeader("*");
88-
corsConfig.addAllowedMethod("*");
89-
90-
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
91-
source.registerCorsConfiguration("/**", corsConfig);
92-
93-
return new CorsFilter(source);
94-
}
95-
9672
@Bean
9773
public RouteLocator apiRoutes(RouteLocatorBuilder builder, @Value("${grafanaUrl:http://localhost:3000}") String grafanaUrl) {
9874
return builder.routes()

edge-server/src/main/resources/application.yml

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@ spring:
33
allow-bean-definition-overriding: true
44
application:
55
name: edge-server
6-
cloud:
7-
consul:
8-
config:
9-
fail-fast: ${FAIL_FAST:true}
10-
format: yaml
11-
config:
12-
import: consul:${consul_url:localhost:8500}
13-
146
management:
157
endpoints:
168
web:
@@ -23,6 +15,33 @@ opentracing:
2315
spring:
2416
web:
2517
skip-pattern: "/actuator.*|/api-docs.*|/swagger.*|.*\\.png|.*\\.css|.*\\.js|.*\\.html|/favicon.ico|/hystrix.stream"
18+
19+
---
20+
spring:
21+
config:
22+
activate:
23+
on-profile: consul
24+
import: consul:${consul_url:localhost:8500}
25+
cloud:
26+
consul:
27+
config:
28+
fail-fast: ${FAIL_FAST:true}
29+
format: yaml
30+
gateway:
31+
globalcors:
32+
cors-configurations:
33+
'[/**]':
34+
allowedOrigins:
35+
- "http://localhost:3000"
36+
- "http://localhost:8080"
37+
allowedHeaders: "*"
38+
allowedMethods: "*"
39+
allowCredentials: true
40+
default-filters:
41+
- DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
42+
autoconfigure:
43+
exclude: org.springframework.cloud.consul.config.ConsulConfigAutoConfiguration
44+
2645
#zuul:
2746
# prefix: /api
2847
# sensitiveHeaders:
@@ -36,4 +55,21 @@ opentracing:
3655
# swaggerUri: api-documentation # additional custom path for swagger documentation, use zuul.default-swagger-url as default
3756
# protocol: http:// # additional protocol info, default: http://
3857
# allowedGroups: # springfox swagger groups filtering. if list not defined or empty - all groups are showed.
39-
# - securityGroup
58+
# - securityGroup
59+
60+
---
61+
spring:
62+
config:
63+
activate:
64+
on-profile: prod
65+
cloud:
66+
gateway:
67+
globalcors:
68+
cors-configurations:
69+
'[/**]':
70+
allowedOrigins: "https://spendingbetter.com"
71+
allowedHeaders: "*"
72+
allowedMethods: "*"
73+
allowCredentials: true
74+
default-filters:
75+
- DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin

go-service/go.mod

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,17 @@ go 1.15
55
require (
66
github.com/dgrijalva/jwt-go v3.2.0+incompatible
77
github.com/go-playground/validator/v10 v10.4.0
8-
github.com/google/btree v1.0.0 // indirect
9-
github.com/hashicorp/consul/api v1.7.0
10-
github.com/hashicorp/golang-lru v0.5.1 // indirect
8+
github.com/hashicorp/consul v1.13.1
9+
github.com/hashicorp/consul/api v1.14.0
1110
github.com/joho/godotenv v1.3.0
1211
github.com/labstack/echo-contrib v0.9.0
1312
github.com/labstack/echo/v4 v4.1.17
1413
github.com/labstack/gommon v0.3.0
1514
github.com/prometheus/client_golang v1.7.1 // indirect
16-
github.com/stretchr/testify v1.6.1 // indirect
1715
go.mongodb.org/mongo-driver v1.4.1
18-
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 // indirect
1916
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
20-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
21-
google.golang.org/protobuf v1.25.0 // indirect
2217
gopkg.in/yaml.v2 v2.2.8
2318
k8s.io/api v0.20.1 // indirect
2419
k8s.io/apimachinery v0.20.1
2520
k8s.io/client-go v0.20.0
26-
k8s.io/klog v1.0.0 // indirect
27-
k8s.io/utils v0.0.0-20201110183641-67b214c5f920 // indirect
2821
)

0 commit comments

Comments
 (0)