Skip to content

Commit

Permalink
Merge pull request #7 from sazzeo/config-server/actuator
Browse files Browse the repository at this point in the history
Config server/actuator
  • Loading branch information
sazzeo authored Aug 10, 2022
2 parents 7f202ee + cdb8e9b commit 400e92c
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 35 deletions.
6 changes: 6 additions & 0 deletions d-gateway/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ dependencies {

//jwt토큰 parsing 하는데 Base64로 인코딩하는데 자바 11버전 부터는 기본 설정이 되지 않아 추가해야함.
implementation 'javax.xml.bind:jaxb-api:2.3.1'

//config
implementation 'org.springframework.cloud:spring-cloud-starter-config'
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap:3.1.3'
//actuator
implementation 'org.springframework.boot:spring-boot-starter-actuator:2.7.2'
}

//버전 알아서 찾아주는 애
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.example.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.trace.http.HttpTraceRepository;
import org.springframework.boot.actuate.trace.http.InMemoryHttpTraceRepository;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class GatewayApplication {
Expand All @@ -10,4 +13,10 @@ public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}


//actuator httptrace 사용을 위해 등록
@Bean
public HttpTraceRepository httpTraceRepository() {
return new InMemoryHttpTraceRepository();
}
}
32 changes: 29 additions & 3 deletions d-gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@ spring:
# uri: lb://USER-SERVICE
# predicates:
# - Path=/user-service/**

## user-service prefix 제거하기
#acturator정보
- id: user-service
uri: lb://USER-SERVICE
predicates:
- Path=/user-service/actuator/**
- Method=GET , POST
filters:
- RemoveRequestHeader=Cookie
- RewritePath=/user-service/(?<segment>.*), /$\{segment} # user-service prefix 제거하기

- id: user-service
uri: lb://USER-SERVICE
predicates:
Expand All @@ -40,6 +48,15 @@ spring:
- RemoveRequestHeader=Cookie
- RewritePath=/user-service/(?<segment>.*), /$\{segment}

- id: user-service
uri: lb://USER-SERVICE
predicates:
- Path=/user-service/health_check #포트체크
- Method=GET
filters:
- RemoveRequestHeader=Cookie
- RewritePath=/user-service/(?<segment>.*), /$\{segment}

- id: user-service
uri: lb://USER-SERVICE
predicates:
Expand All @@ -65,4 +82,13 @@ spring:
args:
baseMessage : Spring Cloud Gateway GlobalFilter
preLogger : true
postLogger : true
postLogger : true


management:
endpoints:
web:
exposure:
include: refresh , health , beans , httptrace


7 changes: 7 additions & 0 deletions d-gateway/src/main/resources/bootstrap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spring:
cloud:
config:
uri: http://127.0.0.1:8888 # config-server url
name: ecommerce # *.yml name
profiles:
active: gateway #application-profile.yml 중 profile 명 기입
6 changes: 5 additions & 1 deletion d-user-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ dependencies {
implementation 'org.modelmapper:modelmapper:3.1.0' //데이터 바인딩을 쉽게 하기 위해 추가

// jwt 토큰

implementation 'io.jsonwebtoken:jjwt:0.9.1'

//spring config server 사용을 위한 의존성
implementation 'org.springframework.cloud:spring-cloud-starter-config'
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap:3.1.3'
implementation 'org.springframework.boot:spring-boot-starter-actuator:2.7.2' //spring cloud actuator

}

dependencyManagement {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.duserservice.config;


import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Data
public class EnvironmentDto {

private String serverPort;
private String localServerPort;
private String tokenSecret;
private String tokenExpirationTime;


@Builder
public EnvironmentDto(String serverPort, String localServerPort, String tokenSecret, String tokenExpirationTime) {
this.serverPort = serverPort;
this.localServerPort = localServerPort;
this.tokenSecret = tokenSecret;
this.tokenExpirationTime = tokenExpirationTime;
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.example.duserservice.controller;


import com.example.duserservice.config.EnvironmentDto;
import com.example.duserservice.service.UserService;
import com.example.duserservice.vo.RequestLogin;
import com.example.duserservice.vo.RequestUser;
import com.example.duserservice.vo.ResponseUser;
import com.example.duserservice.vo.UserDto;
import com.example.duserservice.vo.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
Expand All @@ -29,8 +27,14 @@ public class UserApiController {

//상태 체크 함수
@GetMapping("/health_check")
public String status() {
return String.format("User Service on Port %s" , env.getProperty("local.server.port"));
public EnvironmentDto status() {

return EnvironmentDto.builder()
.localServerPort(env.getProperty("local.server.port"))
.serverPort(env.getProperty("server.port"))
.tokenSecret(env.getProperty("token.secret_key"))
.tokenExpirationTime(env.getProperty("token.expiration_time"))
.build();
}

@PostMapping("/users")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public PasswordEncoder passwordEncoder() {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeHttpRequests().antMatchers("/actuator/**").permitAll().and() //actuator 엔드포인트 사용하기위해 열기
.authorizeHttpRequests().antMatchers("/**").permitAll()
.and()
.addFilter(getAuthenticationFilter()); //UsernamePasswordAuthenticationFilter를 상속받는 클래스기 때문에 이자리로 대체 됨.
Expand Down
12 changes: 11 additions & 1 deletion d-user-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ logging:
com.example.duserservice : info



#token:
# expiration_time: 86400000 #하루짜리 토큰
# secret:user_token
# secret:user_token

#actuator 엔드 포인트를 더 사용하기 위해 추가 /actuator/refresh
management:
endpoints:
web:
exposure:
include: refresh , health , beans


7 changes: 7 additions & 0 deletions d-user-service/src/main/resources/bootstrap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spring:
cloud:
config:
uri: http://127.0.0.1:8888 # config-server url
name: ecommerce # *.yml name
profiles:
active: user-service
13 changes: 13 additions & 0 deletions e-config-service/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
dependencies {

developmentOnly 'org.springframework.boot:spring-boot-devtools'
implementation 'org.springframework.cloud:spring-cloud-config-server:3.1.3'

}


dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.econfigservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigServiceApplication {

public static void main(String[] args) {
SpringApplication.run(ConfigServiceApplication.class, args);
}

}
27 changes: 27 additions & 0 deletions e-config-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
server:
port: 8888

spring:
application:
name: config-service
cloud:
config:
server:
git:
# uri: C:\code\study\msa-spring-cloud-config # local git repository path
uri: https://github.com/sazzeo/mas-config-yml-repo # remote git repository path
# username: private repository 이용시 설정 추가
# password:


# git이 아닌 로컬 폴더 이용시
#spring:
# application:
# name: config-service
# profiles:
# active : native
# cloud:
# config:
# server:
# native:
# search-locations: #로컬 폴더명
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.econfigservice;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class EConfigServiceApplicationTests {

@Test
void contextLoads() {
}

}
42 changes: 19 additions & 23 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
# MSA Toy Project
# Spring Cloud Config

``` text
spring cloud config 로 yml파일 한번에 관리하기
```
MSA Toy Project만들어보기
```
1. e-config-service
2. d-user-service
3. d-gateway-service
4. ecommerce (Eureka Server)

1. d-user-service
2. d-order-service
3. d-category-service
4. d-gateway-service
5. ecommerce (Eureka Server)
## Config

## Eureka Server
1. e-config-service

1.ecommerce
```text
- Spring Cloud Config Server 의존성 추가
- application.yml 파일에 remote / local git repository 주소 등록 설정
```


## Service
## service

1. d-user-service
2. d-order-service
3. category-service
2. d-gateway-service


### User Service

```text
- 스프링 sequrity로 로그인 서비스 구축
- Authentication Filter
- jwt 로 토큰 발행후 Response 헤더 추가
- Spring Cloud Config 의존성 추가
- actuator , bootstrap 의존성 추가
- config-server 정보를 bootstrap.yml 파일에 추가
- actuator endpoint 정보를 application.yml 파일에 추가
```

## Gateway

1. d-api-gateway-service

>AuthorizationHeaderFilter로 요청 token을 파싱해 유효값 확인
4 changes: 3 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ include 'c-api-gateway-service' //api 클라이언트 서비스
include 'd-user-service'
include 'd-gateway'
include 'd-catalog-service'
include 'd-order-service'
include 'd-order-service'

include 'e-config-service'

0 comments on commit 400e92c

Please sign in to comment.