SpringCloud微服务(基于Eureka+Feign+Hystrix+Zuul)

一、搭建注册中心

 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/ 

    注意: DESKTOP-J7C9TIF是电脑的主机名  ,consumer-client是服务名,8082是端口

 四:启动服务测试

   把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/getUserById/101  其中techouse/usersystem是cloud-service项目中zuul配置的网关和路由(perfix和path)

 至此,以上便是一个简单完成的SpringCloud微服务架构了!

posted @ 2021-07-07 09:07  zsq_fengchen  阅读(330)  评论(2编辑  收藏  举报