Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eureka + gateway #3

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,30 @@


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/first-service") //Api Gateway에서 실행시
//@RequestMapping("/") // Zuul에서 실행시
public class FirstServiceController {



Environment env ; //yml파일에 있는 정보 가져오기

@Autowired
public FirstServiceController(Environment env) {
this.env = env;
}

@GetMapping("/welcome")
public String welcom() {
return "welcome to the First service";
Expand All @@ -25,7 +38,8 @@ public String message(@RequestHeader("first-request") String headerValue) {
}

@GetMapping("/check")
public String check() {
return "Hi!! Firsts Service";
public String check(HttpServletRequest request) {
log.info("Server port= {}" , request.getServerPort());
return "Hi!! Firsts Service " + "<br/> server port: " + env.getProperty("local.server.port") ;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
server:
port: 8081 # port번호 0 : 랜덤 포트를 사용하겠다는 의미
port: 0 # port번호 0 : 랜덤 포트를 사용하겠다는 의미

spring:
application:
Expand All @@ -12,5 +12,7 @@ eureka:
instance:
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}} #인스턴스 id 부여해주기
client:
register-with-eureka: false
fetch-registry: false #서버로부터 인스턴스들의 정보를 주기적으로 가져올 것인지?
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://localhost:8761/eureka
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ eureka:
instance:
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}} #인스턴스 id 부여해주기
client:
register-with-eureka: false
fetch-registry: false #서버로부터 인스턴스들의 정보를 주기적으로 가져올 것인지?
register-with-eureka: true
fetch-registry: true #서버로부터 인스턴스들의 정보를 주기적으로 가져올 것인지?
1 change: 1 addition & 0 deletions b-zuul-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ dependencies {
//유레카 [클라이언트] 등록
implementation 'io.spring.gradle:dependency-management-plugin:1.0.11.RELEASE'
implementation 'org.springframework.cloud:spring-cloud-starter-config:3.1.3'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-zuul:2.2.10.RELEASE'

}
Expand Down
7 changes: 7 additions & 0 deletions b-zuul-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
server:
port: 8000

eureka:
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://localhost:8761/eureka

spring:
application:
name: zuul-service #각각 마이크로 서비스마다 부여할 고유 아이디(중복 불가) => 유레카에 대문자로 등록됨. (대소문자 구분x)
Expand Down
8 changes: 4 additions & 4 deletions c-api-gateway-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ server:
port: 8000
eureka:
client:
fetch-registry: false
register-with-eureka: false
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://localhost:8761/eureka

Expand All @@ -17,15 +17,15 @@ spring:
gateway:
routes:
- id: first-service
uri: http://localhost:8081/
uri: lb://ZUUL-CLIENT-FIRST-SERVICE
predicates:
- Path=/first-service/**
filters: #필터 추가하기
# - AddRequestHeader=first-request , first-request-header2 # key , value => 필터 클래스 없이 그냥 등록하는 법. 단순 작업만 가능
# - AddResponseHeader=first-response , first-response-header2
- CustomFilter
- id: first-service
uri: http://localhost:8082/
uri: lb://ZUUL-CLIENT-SECOND-SERVICE
predicates:
- Path=/second-service/**
filters:
Expand Down
35 changes: 35 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## Gateway + Eureka
<hr />

```
게이트웨이와 유레카로 로드밸런싱 해보기
```

1. Eureka Server
2. Gateway
3. Service 1
4. Service 2

### Eureka Server
<hr/>

- ecommerce
<br/>

### Gateway
<hr/>

- c-api-gateway-service
<br/>
>yml 파일에 유레카 service name으로 표기한다.
```
//application.yml

uri: lb://ZUUL-CLIENT-FIRST-SERVICE
uri: lb://ZUUL-CLIENT-SECOND-SERVICE
```

### Service
<hr/>
1. b-zuul-client-first-service <br/>
2. b-zuul-client-second-service