티스토리 뷰

spring cloud gateway 를 Local 에 설정한 과정을 작성.

 

1. spring boot project 생성

spring boot initializr ( https://start.spring.io/ ) 에서 spring boot 프로젝트를 생성하여 저장합니다.

 

 

2. Spring Cloud Gateway 관련 의존성 추가.

build.gradle

compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-gateway', version: '2.2.1.RELEASE'

 

3. RouteLocator 정의

java version : SpringCloudSampleApplication.java

@SpringBootApplication
public class SpringCloudSampleApplication {

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

	@RefreshScope
	@Bean
	public RouteLocator myRoutes(RouteLocatorBuilder builder) {
		return builder.routes()
			.route(p -> p.path("/order/**").uri("http://localhost:8081"))
			.route(p -> p.path("/shipment/**").uri("http://localhost:8082"))
			.build();
	}
}

yml version : bootstrap.yml

spring:
  cloud:
    gateway:
      routes:
        - id: order
          uri: http://localhost:8081
          predicates:
            - Path=/order/**
        - id: shipment
          uri: http://localhost:8082
          predicates:
            - Path=/shipment/**

 


여기 까지 설정을 완료하면 Path Route Predicate 에 매칭된 요청들은 각 route 규칙에 따라서 전달된다.

localhost:8080/order/list -> localhost:8081/order/list
localhost:8080/shipment/1 -> localhost:8082/shipment/1

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함