Quellcode durchsuchen

添加API网关服务

chao vor 4 Jahren
Ursprung
Commit
0bcaef2656

+ 25 - 0
caimei365-cloud-gateway/pom.xml

@@ -0,0 +1,25 @@
+<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>caimei365-cloud</artifactId>
+        <groupId>com.caimei365</groupId>
+        <version>0.0.1</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>caimei365-cloud-gateway</artifactId>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-gateway</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
+        </dependency>
+    </dependencies>
+
+</project>

+ 23 - 0
caimei365-cloud-gateway/src/main/java/com/caimei365/cloud/GatewayApplication.java

@@ -0,0 +1,23 @@
+package com.caimei365.cloud;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.context.ConfigurableApplicationContext;
+
+/**
+ * `@EnableEurekaClient`: 声明一个Eureka客户端,只能注册到Eureka Server
+ * `@EnableDiscoveryClient`: 声明一个可以被发现的客户端,可以是其他注册中心
+ *
+ * @author : Charles
+ * @date : 2021/2/23
+ */
+@EnableDiscoveryClient
+@SpringBootApplication
+public class GatewayApplication {
+
+    public static void main(String[] args) {
+        SpringApplication.run(GatewayApplication.class, args);
+    }
+
+}

+ 30 - 0
caimei365-cloud-gateway/src/main/java/com/caimei365/cloud/controller/RulesController.java

@@ -0,0 +1,30 @@
+package com.caimei365.cloud.controller;
+
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * 暴露接口
+ *
+ * @author : Charles
+ * @date : 2020/12/23
+ */
+@RestController
+public class RulesController {
+//    @GetMapping("/api")
+//    @SentinelResource("api")
+//    public Set<ApiDefinition> apiRules() {
+//        return GatewayApiDefinitionManager.getApiDefinitions();
+//    }
+//
+//    @GetMapping("/com/caimei365/cloud")
+//    @SentinelResource("com/caimei365/cloud")
+//    public Set<GatewayFlowRule> apiGateway() {
+//        return GatewayRuleManager.getRules();
+//    }
+//
+//    @GetMapping("/flow")
+//    @SentinelResource("flow")
+//    public List<FlowRule> apiFlow() {
+//        return FlowRuleManager.getRules();
+//    }
+}

+ 31 - 0
caimei365-cloud-gateway/src/main/resources/application.yml

@@ -0,0 +1,31 @@
+server:
+  port: 8008
+
+# 指定当前服务的名称,这个名称会注册到注册中心
+spring:
+  application:
+    name: @artifactId@
+  cloud:              # spring cloud gateway 路由配置方式
+    gateway:
+      discovery:      # 是否与服务发现组件进行结合,通过 serviceId 转发到具体的服务实例。
+        locator:      # 默认为false,设为true便开启通过服务中心的自动根据 serviceId 创建路由的功能。
+          enabled: true
+        lowerCaseServiceId: true       # 将请求路径的服务名配置改成小写
+      routes:
+        - id: user-server           # 自定义的路由 ID,保持唯一性
+          uri: lb://caimei365-cloud-user     # 从注册中心获取服务,且以lb(load-balance)负载均衡方式转发
+          predicates:
+            - Path=/user/**         # 将以/product/HiController开头的请求转发到uri为lb://caimei365-cloud-product的地址上
+        - id: product-server
+          uri: lb://caimei365-cloud-product
+          predicates:
+            - Path=/product/**
+
+# 指定服务注册中心的地址
+eureka:
+  instance:
+    prefer-ip-address: true       # 是否使用 ip 地址注册
+    instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port
+  client:
+    service-url:                  # 设置服务注册中心地址
+      defaultZone: http://localhost:18000/eureka/

+ 1 - 0
pom.xml

@@ -17,6 +17,7 @@
 
     <modules>
         <module>caimei365-cloud-discovery</module>
+        <module>caimei365-cloud-gateway</module>
     </modules>
 
     <properties>