Browse Source

feature 集成Sentinel

黎有为 1 year ago
parent
commit
f549dbf698

+ 4 - 0
Provider/pom.xml

@@ -30,6 +30,10 @@
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-bootstrap</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.alibaba.cloud</groupId>
+            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
+        </dependency>
     </dependencies>
 
     <build>

+ 0 - 22
Provider/src/main/java/com/citygis/provider/ProviderApplication.java

@@ -1,16 +1,8 @@
 package com.citygis.provider;
 
-import com.citygis.provider.config.NacosConfigInfo;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
-import org.springframework.cloud.context.config.annotation.RefreshScope;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RestController;
 
 @SpringBootApplication
 @EnableDiscoveryClient
@@ -19,18 +11,4 @@ public class ProviderApplication {
     public static void main(String[] args) {
         SpringApplication.run(ProviderApplication.class, args);
     }
-
-    @RestController
-    @RefreshScope
-    public static class EchoController {
-        @Autowired
-        private NacosConfigInfo configInfo;
-
-        @GetMapping("/echo/{string}")
-        public String hello(@PathVariable String string) throws JsonProcessingException {
-            ObjectMapper objectMapper = new ObjectMapper();
-            String str = objectMapper.writeValueAsString(configInfo);
-            return "Hello! I am Provider. info: " + str + string;
-        }
-    }
 }

+ 26 - 0
Provider/src/main/java/com/citygis/provider/controller/EchoController.java

@@ -0,0 +1,26 @@
+package com.citygis.provider.controller;
+
+import com.alibaba.csp.sentinel.annotation.SentinelResource;
+import com.citygis.provider.config.NacosConfigInfo;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cloud.context.config.annotation.RefreshScope;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RefreshScope
+public class EchoController {
+    @Autowired
+    private NacosConfigInfo configInfo;
+
+    @GetMapping("/echo/{string}")
+    @SentinelResource("echo")
+    public String hello(@PathVariable String string) throws JsonProcessingException {
+        ObjectMapper objectMapper = new ObjectMapper();
+        String str = objectMapper.writeValueAsString(configInfo);
+        return "Hello! I am Provider. info: " + str + string;
+    }
+}