(Thirty-seven) java version of spring cloud micro Services Architecture b2b2c e-commerce platform -API Gateway Service

1 Spring Cloud Gateway

In the micro-service architecture, gateway services as a unified entrance, all external clients need to go through it to access scheduling and filtering functions can be achieved include dynamic routing, load balancing, authorization and authentication, such as limiting.

Spring Spring Cloud Gateway is the official gateway based on technology developed by Spring 5.0, Spring Boot 2.0 and Project Reactor, which are intended to provide a simple and effective service for the micro-architecture of unified API routing management, and provide them with crosscutting concerns for example: security, monitoring / indicators and elasticity.

This article deals with a simple example to build a gateway service.

Gateway 2 building

2.1 New Spring Boot project, the introduction of its dependencies

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath/>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-gateway</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

2.2 application.properties Configuration

spring.application.name=api-gateway
server.port=8888
#注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1002/eureka/,http://localhost:1003/eureka/

#实例默认通过使用域名形式注册到注册中心:false
eureka.instance.prefer-ip-address=true

#实例名
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}

#是否与服务注册于发现组件进行结合,通过 serviceId 转发到具体的服务实例
#默认为false,设为true便开启通过服务中心的自动根据 serviceId 创建路由的功能
#其中微服务应用名默认大写访问
spring.cloud.gateway.discovery.locator.enabled=true

2.3 Add Filter, to achieve a simple certification authority

@Configuration
public class TokenFilter implements GlobalFilter, Ordered {

    @Override
    public int getOrder() {
        return -100;
    }

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String token = exchange.getRequest().getQueryParams().getFirst("token");
       //url不含token参数时返回401状态码
        if (token == null || token.isEmpty()) {
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }
}

2.4 entry program annotate and add @SpringCloudApplication start

@SpringCloudApplication
public class ApiGatewayApplication {

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

}

Start the application, visit localhost: 8888 / EUREKA-CLIENT / hello, see page 401 status code, and the token parameter request again, you can see the results returned to normal (note the service name in uppercase).

Guess you like

Origin blog.csdn.net/vvx0206/article/details/93966581