Browse Source

0414 cloud eureka

Qing 1 year ago
parent
commit
fe69bca7f4

+ 1 - 0
springcloud-demo/.idea/encodings.xml

@@ -7,6 +7,7 @@
     <file url="file://$PROJECT_DIR$/eureka-client-demo-1/src/main/java" charset="UTF-8" />
     <file url="file://$PROJECT_DIR$/eureka-client-demo-2/src/main/java" charset="UTF-8" />
     <file url="file://$PROJECT_DIR$/eureka-service-demo-1/src/main/java" charset="UTF-8" />
+    <file url="file://$PROJECT_DIR$/eureka-service-demo-2/src/main/java" charset="UTF-8" />
     <file url="file://$PROJECT_DIR$/eureka-service-demo/src/main/java" charset="UTF-8" />
     <file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
     <file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />

+ 1 - 0
springcloud-demo/.idea/misc.xml

@@ -12,6 +12,7 @@
         <option value="$PROJECT_DIR$/eureka-client-demo-1/pom.xml" />
         <option value="$PROJECT_DIR$/eureka-client-demo-2/pom.xml" />
         <option value="$PROJECT_DIR$/eureka-service-demo-1/pom.xml" />
+        <option value="$PROJECT_DIR$/eureka-service-demo-2/pom.xml" />
       </list>
     </option>
     <option name="ignoredFiles">

+ 25 - 8
springcloud-demo/.idea/workspace.xml

@@ -4,10 +4,9 @@
     <option name="autoReloadType" value="SELECTIVE" />
   </component>
   <component name="ChangeListManager">
-    <list default="true" id="4074a161-1a6c-49f9-bbc5-f70fc0666d46" name="Changes" comment="0410 cloud eureka">
+    <list default="true" id="4074a161-1a6c-49f9-bbc5-f70fc0666d46" name="Changes" comment="0414 cloud eureka">
       <change afterPath="$PROJECT_DIR$/../.idea/VIPJAVA.iml" afterDir="false" />
       <change afterPath="$PROJECT_DIR$/../gn_oa_vip27/.idea/google-java-format.xml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/client-demo-1/src/main/java/com/sf/TestHashMap.java" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/../02_JavaWeb/day06_mybatis/src/main/java/com/lc/mapper/UserMapper.java" beforeDir="false" afterPath="$PROJECT_DIR$/../02_JavaWeb/day06_mybatis/src/main/java/com/lc/mapper/UserMapper.java" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/../02_JavaWeb/day06_mybatis/src/main/resources/com/lc/mapper/UserMapper.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../02_JavaWeb/day06_mybatis/src/main/resources/com/lc/mapper/UserMapper.xml" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/../02_JavaWeb/day07_mybatis/src/main/resources/com/lc/mapper/UserMapper.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../02_JavaWeb/day07_mybatis/src/main/resources/com/lc/mapper/UserMapper.xml" afterDir="false" />
@@ -20,11 +19,8 @@
       <change beforePath="$PROJECT_DIR$/.idea/encodings.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/eureka-client-demo-1/src/main/java/com/sf/eurekaclientdemo1/config/RestConfig.java" beforeDir="false" afterPath="$PROJECT_DIR$/eureka-client-demo-1/src/main/java/com/sf/eurekaclientdemo1/config/RestConfig.java" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/eureka-client-demo-1/src/main/java/com/sf/eurekaclientdemo1/controller/ClientController.java" beforeDir="false" afterPath="$PROJECT_DIR$/eureka-client-demo-1/src/main/java/com/sf/eurekaclientdemo1/controller/ClientController.java" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/eureka-client-demo-1/src/main/resources/application.properties" beforeDir="false" afterPath="$PROJECT_DIR$/eureka-client-demo-1/src/main/resources/application.properties" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/eureka-client-demo-2/src/main/java/com/sf/eurekaclientdemo2/controller/ClientController.java" beforeDir="false" afterPath="$PROJECT_DIR$/eureka-client-demo-2/src/main/java/com/sf/eurekaclientdemo2/controller/ClientController.java" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/eureka-service-demo/src/main/resources/application.properties" beforeDir="false" afterPath="$PROJECT_DIR$/eureka-service-demo/src/main/resources/application.properties" afterDir="false" />
     </list>
     <option name="SHOW_DIALOG" value="false" />
     <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -77,8 +73,10 @@
     "Spring Boot.ClientDemo2Application.executor": "Run",
     "Spring Boot.EurekaClientDemo1Application.executor": "Run",
     "Spring Boot.EurekaClientDemo2Application (1).executor": "Run",
+    "Spring Boot.EurekaClientDemo2Application (2).executor": "Run",
     "Spring Boot.EurekaClientDemo2Application.executor": "Run",
     "Spring Boot.EurekaServiceDemo1Application.executor": "Run",
+    "Spring Boot.EurekaServiceDemo2Application.executor": "Run",
     "Spring Boot.EurekaServiceDemoApplication.executor": "Run",
     "git-widget-placeholder": "master",
     "jdk.selected.JAVA_MODULE": "corretto-17",
@@ -89,6 +87,9 @@
     "node.js.selected.package.eslint": "(autodetect)",
     "node.js.selected.package.tslint": "(autodetect)",
     "nodejs_package_manager_path": "npm",
+    "project.structure.last.edited": "Modules",
+    "project.structure.proportion": "0.0",
+    "project.structure.side.proportion": "0.0",
     "settings.editor.selected.configurable": "MavenSettings",
     "vue.rearranger.settings.migration": "true"
   }
@@ -166,6 +167,13 @@
         <option name="Make" enabled="true" />
       </method>
     </configuration>
+    <configuration name="EurekaServiceDemo2Application" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" nameIsGenerated="true">
+      <module name="eureka-service-demo-2" />
+      <option name="SPRING_BOOT_MAIN_CLASS" value="com.sf.eurekaservicedemo2.EurekaServiceDemo2Application" />
+      <method v="2">
+        <option name="Make" enabled="true" />
+      </method>
+    </configuration>
     <configuration name="EurekaServiceDemoApplication" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" nameIsGenerated="true">
       <module name="eureka-service-demo" />
       <option name="SPRING_BOOT_MAIN_CLASS" value="com.sf.EurekaServiceDemoApplication" />
@@ -188,7 +196,7 @@
       <option name="presentableId" value="Default" />
       <updated>1712747536992</updated>
       <workItem from="1712747538100" duration="4625000" />
-      <workItem from="1713064649248" duration="12782000" />
+      <workItem from="1713064649248" duration="15859000" />
     </task>
     <task id="LOCAL-00001" summary="0410 cloud eureka">
       <option name="closed" value="true" />
@@ -198,7 +206,15 @@
       <option name="project" value="LOCAL" />
       <updated>1712753113007</updated>
     </task>
-    <option name="localTasksCounter" value="2" />
+    <task id="LOCAL-00002" summary="0414 cloud eureka">
+      <option name="closed" value="true" />
+      <created>1713082688554</created>
+      <option name="number" value="00002" />
+      <option name="presentableId" value="LOCAL-00002" />
+      <option name="project" value="LOCAL" />
+      <updated>1713082688554</updated>
+    </task>
+    <option name="localTasksCounter" value="3" />
     <servers />
   </component>
   <component name="TypeScriptGeneratedFilesManager">
@@ -242,6 +258,7 @@
   </component>
   <component name="VcsManagerConfiguration">
     <MESSAGE value="0410 cloud eureka" />
-    <option name="LAST_COMMIT_MESSAGE" value="0410 cloud eureka" />
+    <MESSAGE value="0414 cloud eureka" />
+    <option name="LAST_COMMIT_MESSAGE" value="0414 cloud eureka" />
   </component>
 </project>

+ 23 - 0
springcloud-demo/eureka-client-demo-1/src/main/java/com/sf/eurekaclientdemo1/controller/ClientController.java

@@ -1,6 +1,8 @@
 package com.sf.eurekaclientdemo1.controller;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cloud.client.ServiceInstance;
+import org.springframework.cloud.client.discovery.DiscoveryClient;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.client.RestTemplate;
@@ -10,6 +12,7 @@ import java.net.http.HttpClient;
 import java.net.http.HttpRequest;
 import java.net.http.HttpResponse;
 import java.time.Duration;
+import java.util.List;
 
 @RestController
 public class ClientController {
@@ -17,6 +20,9 @@ public class ClientController {
     @Autowired
     private RestTemplate restTemplate;
 
+    @Autowired
+    private DiscoveryClient discoveryClient;
+
     // http://localhost:18091/clientDemo1
     @GetMapping("/clientDemo1")
     public String handler() throws Exception {
@@ -24,6 +30,23 @@ public class ClientController {
 //        String result = restTemplate.getForObject("http://localhost:18092/clientDemo2", String.class);
         // 通过服务名 完成服务调用
         String result = restTemplate.getForObject("http://eureka-client-demo-2/clientDemo2", String.class);
+
+        // eureka-client-demo-2 localhost:18092
+        // eureka-client-demo-2 localhost:18093
+        List<String> services = discoveryClient.getServices();
+        for (String service : services) {
+            System.out.println(service);
+        }
+
+        // 实例  集群中的实例 一个可以被访问的进程  192.168.1.10:18091  192.168.1.11:18091
+        List<ServiceInstance> instances = discoveryClient.getInstances("eureka-client-demo-2");
+        for (ServiceInstance instance : instances) {
+            System.out.println(instance.getServiceId());
+            System.out.println(instance.getHost());
+            System.out.println(instance.getPort());
+            System.out.println(instance.getUri());
+        }
+
         String data = "client demo 1 success";
         return data + " " + result;
     }

+ 1 - 0
springcloud-demo/eureka-client-demo-2/src/main/java/com/sf/eurekaclientdemo2/controller/ClientController.java

@@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.RestController;
 public class ClientController {
 
     // http://localhost:18092/clientDemo2
+    // http://localhost:18093/clientDemo2
     @GetMapping("/clientDemo2")
     public String handler() throws Exception {
         String data = "client demo 2 success";