SpringCloud微服务安全(三)网关安全 3-2 常见的微服务安全整体架构

1. 整体架构

这个图适用于中小公司的微服务架构

微服务:SpringBoot 写的Rest服务

服务注册与发现:微服务所必备的。每个微服务都会到上边去注册。不管是微服务之间的调用,还是服务网关到微服务的转发,都是通过服务注册和发现拿到服务的信息,来进行服务的调用或转发。

配置中心:统一管理配置的地方。

服务网关:所有外部请求的入口。微服务不会直接向外暴露,都是通过服务网关来进行转发。

安全中心:整个微服务的认证授权。

熔断限流:统一的管理微服务的限流、熔断、降级等。。

数据总线:左边大块里发生的所有事情,都会通过数据总线(消息队列,根据业务场景,可能是多种)

调用链监控 Tracing:一个请求进来经过了哪些微服务,每个微服务上耗了多长时间,服务的性能瓶颈在哪里。

指标监控 metrics:流量、业务指标

日志监控 log:把所有微服务的日志统一采集起来

健康检查和报警:比如某一个服务的响应时间大于多少就报警 ,某一天的流水有异常了就报警,日志出现某些元素就报警。等等

2. 案例演示 -- 订单微服务&价格微服务

微服务之间使用 RestTemplate 进行远程调用。

2.1 订单微服务

(1)pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.imooc.security</groupId>
	<artifactId>this-order-api</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>this-order-api</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>11</java.version>
	</properties>

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

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </dependency>
		<dependency>
			<groupId>org.junit.platform</groupId>
			<artifactId>junit-platform-commons</artifactId>
			<version>RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.10</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

(2)指定端口为: 9090

spring:
  application:
    name: order-api
server:
  port: 9090

(3)订单实体

@Data
public class OrderInfo {
    private Long productId;
}

(4)订单接口

@RestController
@RequestMapping("/orders")
@Slf4j
public class OrderController {

    private RestTemplate restTemplate = new RestTemplate();

    @PostMapping
    public PriceInfo create(@RequestBody OrderInfo info){
        PriceInfo priceInfo = restTemplate.getForObject("http://localhost:9080/prices/" + info.getProductId(),PriceInfo.class);
        log.info("price is:{}",priceInfo.getPrice());
        return priceInfo;
    }
}

2.2 价格微服务

(1)pom.xml

参考订单微服务的 pom.xml

(2)指定端口为 9080

(3)价格实体

@Data
public class PriceInfo {

    private Long id;

    private BigDecimal price;

}

(4)价格微服务接口

@RestController
@RequestMapping("/prices")
@Slf4j
public class PriceController {

    @GetMapping("/{id}")
    public PriceInfo create(@PathVariable Long id){
        log.info("productId is {}" ,id);
        PriceInfo info = new PriceInfo();
        info.setId(id);
        info.setPrice(new BigDecimal(100));
        return info;
    }
}

2.3 启动两个服务,并用 API Tester 请求订单服务间接请求价格微服务获取价格信息

Guess you like

Origin blog.csdn.net/weixin_42405670/article/details/116356465