SpringCloud微服務(基於Eureka+Feign+Hystrix+Zuul)

zsq_fengchen發表於2021-07-07

一、搭建註冊中心

 1.1、建立一個cloud-service專案

   

 1.2:POM檔案依賴

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4     <modelVersion>4.0.0</modelVersion>
  5 
  6     <groupId>com.tiandy</groupId>
  7     <artifactId>cloud-service</artifactId>
  8     <version>0.0.1-SNAPSHOT</version>
  9     <packaging>jar</packaging>
 10 
 11     <name>cloud-service</name>
 12     <description>Demo project for Spring Boot</description>
 13 
 14     <parent>
 15         <groupId>org.springframework.boot</groupId>
 16         <artifactId>spring-boot-starter-parent</artifactId>
 17         <version>1.5.9.RELEASE</version>
 18         <relativePath/> <!-- lookup parent from repository -->
 19     </parent>
 20 
 21     <properties>
 22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 24         <java.version>1.8</java.version>
 25         <spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
 26     </properties>
 27 
 28     <dependencies>
 29 
 30         <dependency>
 31             <groupId>org.springframework.boot</groupId>
 32             <artifactId>spring-boot-starter-web</artifactId>
 33         </dependency>
 34 
 35         <dependency>
 36             <groupId>org.springframework.cloud</groupId>
 37             <artifactId>spring-cloud-starter-eureka</artifactId>
 38         </dependency>
 39         <!-- @HystrixCommand註解 -->
 40         <dependency>
 41             <groupId>com.netflix.hystrix</groupId>
 42             <artifactId>hystrix-javanica</artifactId>
 43         </dependency>
 44         <dependency>
 45             <groupId>org.springframework.cloud</groupId>
 46             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
 47         </dependency>
 48         <!-- 宣告呼叫 -->
 49         <dependency>
 50             <groupId>org.springframework.cloud</groupId>
 51             <artifactId>spring-cloud-starter-openfeign</artifactId>
 52         </dependency>
 53         <!-- 服務容錯  -->
 54         <dependency>
 55             <groupId>org.springframework.cloud</groupId>
 56             <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
 57         </dependency>
 58 
 59         <!--閘道器zuul-->
 60         <dependency>
 61             <groupId>org.springframework.cloud</groupId>
 62             <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
 63         </dependency>
 64 
 65         <!--實體中的Date註解,不用get set-->
 66         <dependency>
 67             <groupId>org.projectlombok</groupId>
 68             <artifactId>lombok</artifactId>
 69         </dependency>
 70 
 71 
 72         <dependency>
 73             <groupId>org.springframework.boot</groupId>
 74             <artifactId>spring-boot-starter-test</artifactId>
 75             <scope>test</scope>
 76         </dependency>
 77 
 78 
 79     </dependencies>
 80 
 81     <dependencyManagement>
 82         <dependencies>
 83             <dependency>
 84                 <groupId>org.springframework.cloud</groupId>
 85                 <artifactId>spring-cloud-dependencies</artifactId>
 86                 <version>${spring-cloud.version}</version>
 87                 <type>pom</type>
 88                 <scope>import</scope>
 89             </dependency>
 90         </dependencies>
 91     </dependencyManagement>
 92 
 93     <build>
 94         <plugins>
 95             <plugin>
 96                 <groupId>org.springframework.boot</groupId>
 97                 <artifactId>spring-boot-maven-plugin</artifactId>
 98             </plugin>
 99         </plugins>
100     </build>
101 
102 </project>

  1.3:application.yml配置檔案

  

  1.4:啟動類CloudServiceApplication

 1 package com.tiandy.myclient;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 6 import org.springframework.cloud.netflix.hystrix.EnableHystrix;
 7 
 8 @EnableEurekaClient
 9 @EnableHystrix
10 @SpringBootApplication
11 public class MyClientApplication {
12 
13     public static void main(String[] args) {
14         SpringApplication.run(MyClientApplication.class, args);
15     }
16 }

  說明@EnableEurekaClient是開啟Eureka服務註冊中心功能註解,@EnableHystrix是開始Hystrix功能註解

   1.5:啟動MyClientApplication 服務

    服務啟動成功後,訪問http://127.0.0.1:8761/

 

二、spring cloud建立服務提供者

    2.1、建立一個my-client專案

    

    2.1:POM檔案依賴

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4     <modelVersion>4.0.0</modelVersion>
  5 
  6     <groupId>com.tiandy</groupId>
  7     <artifactId>my-client</artifactId>
  8     <version>0.0.1-SNAPSHOT</version>
  9     <packaging>jar</packaging>
 10 
 11     <name>sbc-providers</name>
 12     <description>Demo project for Spring Boot</description>
 13 
 14     <parent>
 15         <groupId>org.springframework.boot</groupId>
 16         <artifactId>spring-boot-starter-parent</artifactId>
 17         <version>1.5.9.RELEASE</version>
 18         <relativePath/> <!-- lookup parent from repository -->
 19     </parent>
 20 
 21     <properties>
 22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 24         <java.version>1.8</java.version>
 25         <spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
 26     </properties>
 27 
 28     <dependencies>
 29 
 30         <dependency>
 31             <groupId>org.springframework.boot</groupId>
 32             <artifactId>spring-boot-starter-web</artifactId>
 33         </dependency>
 34         <dependency>
 35             <groupId>org.springframework.cloud</groupId>
 36             <artifactId>spring-cloud-starter-eureka</artifactId>
 37         </dependency>
 38         <!-- @HystrixCommand註解 -->
 39         <dependency>
 40             <groupId>com.netflix.hystrix</groupId>
 41             <artifactId>hystrix-javanica</artifactId>
 42         </dependency>
 43         <dependency>
 44             <groupId>org.springframework.cloud</groupId>
 45             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
 46         </dependency>
 47         <!-- 宣告呼叫 -->
 48         <dependency>
 49             <groupId>org.springframework.cloud</groupId>
 50             <artifactId>spring-cloud-starter-openfeign</artifactId>
 51         </dependency>
 52         <!-- 服務容錯  -->
 53         <dependency>
 54             <groupId>org.springframework.cloud</groupId>
 55             <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
 56         </dependency>
 57 
 58         <!--閘道器zuul-->
 59         <dependency>
 60             <groupId>org.springframework.cloud</groupId>
 61             <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
 62         </dependency>
 63 
 64         <!--實體中的Date註解,不用get set-->
 65         <dependency>
 66             <groupId>org.projectlombok</groupId>
 67             <artifactId>lombok</artifactId>
 68         </dependency>
 69 
 70 
 71         <dependency>
 72             <groupId>org.springframework.boot</groupId>
 73             <artifactId>spring-boot-starter-test</artifactId>
 74             <scope>test</scope>
 75         </dependency>
 76 
 77 
 78     </dependencies>
 79 
 80     <dependencyManagement>
 81         <dependencies>
 82             <dependency>
 83                 <groupId>org.springframework.cloud</groupId>
 84                 <artifactId>spring-cloud-dependencies</artifactId>
 85                 <version>${spring-cloud.version}</version>
 86                 <type>pom</type>
 87                 <scope>import</scope>
 88             </dependency>
 89         </dependencies>
 90     </dependencyManagement>
 91 
 92     <build>
 93         <plugins>
 94             <plugin>
 95                 <groupId>org.springframework.boot</groupId>
 96                 <artifactId>spring-boot-maven-plugin</artifactId>
 97             </plugin>
 98         </plugins>
 99     </build>
100 
101 </project>

   2.3:application.yml配置檔案

 1 server:
 2   port: 8800
 3 spring:
 4   application:
 5     name: product-client #為你的應用起個名字,該名字將註冊到eureka註冊中心
 6 eureka:
 7   client:
 8     serviceUrl:
 9       defaultZone: http://localhost:8761/eureka/ #去哪裡註冊,eureka服務地址
10 
11 hystrix:
12   command:
13     default:
14       execution:
15         isolation:
16           thread:
17             timeoutInMilliseconds:10000  #超時時間

   2.4:例項類VO-User

 1 package com.tiandy.myclient.vo;
 2 
 3 import lombok.Data;
 4 import java.io.Serializable;
 5 
 6 @Data
 7 public class User implements Serializable {
 8 
 9     private String id;
10 
11     private  String name;
12 
13     private   Integer age;
14 
15     @Override
16     public String toString() {
17         return "User{" +
18                 "id='" + id + '\'' +
19                 ", name='" + name + '\'' +
20                 ", age=" + age +
21                 '}';
22     }
23 }

  2.5:HelloController業務控制類

 1 package com.tiandy.myclient.controller;
 2 import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
 3 import com.tiandy.myclient.vo.User;
 4 import org.springframework.web.bind.annotation.PathVariable;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RestController;
 7 @RestController
 8 public class HelloController {
 9 
10     @RequestMapping("/hello/{fallback}")
11     @HystrixCommand(fallbackMethod="fallbackMethod")/*呼叫方式失敗後呼叫helloFallbackMethod*/
12     public String hello(@PathVariable("fallback") String fallback){
13         if("1".equals(fallback)){
14             throw new RuntimeException("...");
15         }
16         return "走閘道器了: hello zuul !";
17     }
18 
19     public String fallbackMethod(String fallback){
20         return "【觸發了Hystrix熔斷機制,呼叫了fallbackMethod方法...】";
21     }
22 
23     @RequestMapping("/getUserById/{fallback}")
24     public String getUserById(@PathVariable("fallback") String fallback){
25         User user=new User();
26         user.setId("101");
27         user.setAge(32);
28         user.setName("司藤");
29         if(!fallback.equals("101")){
30             throw new RuntimeException("...");
31         }
32         String userInfo=user.toString();
33         System.out.println(userInfo);
34         return userInfo;
35     }
36 }

 2.6:服務啟動類MyClientApplication

 1 package com.tiandy.myclient;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 6 import org.springframework.cloud.netflix.hystrix.EnableHystrix;
 7 
 8 @EnableEurekaClient
 9 @EnableHystrix
10 @SpringBootApplication
11 public class MyClientApplication {
12 
13     public static void main(String[] args) {
14         SpringApplication.run(MyClientApplication.class, args);
15     }
16 }

2.7:啟動服務,看服務product-client是否註冊到了註冊中心

  啟動成功後,訪問http://127.0.0.1:8761/

 

三、spring cloud建立服務消費者

    3.1、建立一個my-consumert專案

   

    3.2:POM檔案依賴

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4     <modelVersion>4.0.0</modelVersion>
  5 
  6     <groupId>com.tiandy</groupId>
  7     <artifactId>my-consumer</artifactId>
  8     <version>0.0.1-SNAPSHOT</version>
  9     <packaging>jar</packaging>
 10 
 11     <name>my-consumer</name>
 12     <description>Demo project for Spring Boot</description>
 13 
 14     <parent>
 15         <groupId>org.springframework.boot</groupId>
 16         <artifactId>spring-boot-starter-parent</artifactId>
 17         <version>1.5.9.RELEASE</version>
 18         <relativePath/> <!-- lookup parent from repository -->
 19     </parent>
 20 
 21     <properties>
 22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 24         <java.version>1.8</java.version>
 25         <spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
 26     </properties>
 27 
 28     <dependencies>
 29 
 30         <dependency>
 31             <groupId>org.springframework.boot</groupId>
 32             <artifactId>spring-boot-starter-web</artifactId>
 33         </dependency>
 34         <dependency>
 35             <groupId>org.springframework.cloud</groupId>
 36             <artifactId>spring-cloud-starter-eureka</artifactId>
 37         </dependency>
 38         <!-- @HystrixCommand註解 -->
 39         <dependency>
 40             <groupId>com.netflix.hystrix</groupId>
 41             <artifactId>hystrix-javanica</artifactId>
 42         </dependency>
 43         <dependency>
 44             <groupId>org.springframework.cloud</groupId>
 45             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
 46         </dependency>
 47         <!-- 宣告呼叫 -->
 48         <dependency>
 49             <groupId>org.springframework.cloud</groupId>
 50             <artifactId>spring-cloud-starter-openfeign</artifactId>
 51         </dependency>
 52         <!-- 服務容錯  -->
 53         <dependency>
 54             <groupId>org.springframework.cloud</groupId>
 55             <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
 56         </dependency>
 57 
 58         <!--閘道器zuul-->
 59         <dependency>
 60             <groupId>org.springframework.cloud</groupId>
 61             <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
 62         </dependency>
 63 
 64         <!--實體中的Date註解,不用get set-->
 65         <dependency>
 66             <groupId>org.projectlombok</groupId>
 67             <artifactId>lombok</artifactId>
 68         </dependency>
 69 
 70 
 71         <dependency>
 72             <groupId>org.springframework.boot</groupId>
 73             <artifactId>spring-boot-starter-test</artifactId>
 74             <scope>test</scope>
 75         </dependency>
 76 
 77 
 78     </dependencies>
 79 
 80     <dependencyManagement>
 81         <dependencies>
 82             <dependency>
 83                 <groupId>org.springframework.cloud</groupId>
 84                 <artifactId>spring-cloud-dependencies</artifactId>
 85                 <version>${spring-cloud.version}</version>
 86                 <type>pom</type>
 87                 <scope>import</scope>
 88             </dependency>
 89         </dependencies>
 90     </dependencyManagement>
 91 
 92     <build>
 93         <plugins>
 94             <plugin>
 95                 <groupId>org.springframework.boot</groupId>
 96                 <artifactId>spring-boot-maven-plugin</artifactId>
 97             </plugin>
 98         </plugins>
 99     </build>
100 
101 </project>

  3.3:application.yml配置檔案

1 server:
2   port: 8082
3 spring:
4   application:
5     name: consumer-client #為你的應用起個名字,該名字將註冊到eureka註冊中心
6 eureka:
7   client:
8     serviceUrl:
9       defaultZone: http://localhost:8761/eureka/ #去哪裡註冊,eureka服務地址

  3.4:遠端介面呼叫HelloService

 1 package com.tiandy.myconsumer.service;
 2 
 3 import org.springframework.cloud.netflix.feign.FeignClient;
 4 import org.springframework.web.bind.annotation.PathVariable;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 
 7 @FeignClient("product-client")  //product-client提供介面工程的服務名
 8 public interface HelloService {
 9 
10     @RequestMapping("/hello/{fallback}")
11     public String hello(@PathVariable("fallback") String fallback);
12 
13     @RequestMapping("/getUserById/{fallback}")
14     public String getUserById(@PathVariable("fallback") String fallback);
15 }

  注意@FeignClient註解就是開啟遠端呼叫的功能,提供的介面必須和product-client工程服務中提供的介面名稱、引數、返回值一樣

   3.5:業務控制類HelloController

 1 package com.tiandy.myconsumer.controller;
 2 
 3 import com.tiandy.myconsumer.service.HelloService;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.web.bind.annotation.PathVariable;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RestController;
 8 
 9 @RestController
10 public class HelloController {
11 
12     @Autowired
13     private HelloService helloServcie;
14 
15     @RequestMapping("/test/{fallback}")
16     public String hello(@PathVariable("fallback") String fallback){
17         String res=helloServcie.hello(fallback);
18         return "結果為:"+res;
19     }
20     
21     @RequestMapping("/getUserById/{fallback}")
22     public String getUserById(@PathVariable("fallback") String fallback){
23          String userString=helloServcie.getUserById(fallback);
24          System.out.println("==結果:==="+userString);
25          return  userString;
26     }
27 }

   3.6:啟動類MyConsumerApplication

 1 package com.tiandy.myconsumer;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 6 import org.springframework.cloud.netflix.feign.EnableFeignClients;
 7 import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
 8 
 9 @SpringBootApplication
10 @EnableEurekaClient
11 @EnableZuulProxy
12 @EnableFeignClients
13 public class MyConsumerApplication {
14 
15     public static void main(String[] args) {
16         SpringApplication.run(MyConsumerApplication.class, args);
17     }
18 }

  注意@EnableZuulProxy是開啟閘道器功能的註解

  3.7:啟動服務,看服務consumer-client是否註冊到了註冊中心

      啟動成功後,訪問http://127.0.0.1:8761/ 

 

 四:啟動服務測試

   把cloud-service、my-client、my-consumer三個服務都啟動成功後,訪問

   http://127.0.0.1:8082/test/1

   http://127.0.0.1:8082/test/2

   http://127.0.0.1:8082/getUserById/101

   http://192.168.100.50:8761/techouse/usersystem/getUserById/101

   

 

 

 

 

 注意:我們可以使用統一入口,呼叫PRODUCT-CLIENT服務:
           訪問的url: http://127.0.0.1:8761/techouse/usersystem/hello/2  其中techouse/usersystem是cloud-service專案中zuul配置的閘道器和路由(perfix和path)

 至此,以上便是一個簡單完成的SpringCloud微服務架構了!

相關文章