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/ 
四:启动服务测试
把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微服务架构了!
SpringCloud微服务(基于Eureka+Feign+Hystrix+Zuul)的更多相关文章
- springcloud微服务实战:Eureka+Zuul+Feign/Ribbon+Hystrix Turbine+SpringConfig+sleuth+zipkin
相信现在已经有很多小伙伴已经或者准备使用springcloud微服务了,接下来为大家搭建一个微服务框架,后期可以自己进行扩展.会提供一个小案例: 服务提供者和服务消费者 ,消费者会调用提供者的服务,新 ...
- springcloud微服务实战:Eureka+Zuul+Ribbon+Hystrix+SpringConfig
原文地址:http://blog.csdn.net/yp090416/article/details/78017552 springcloud微服务实战:Eureka+Zuul+Ribbon+Hyst ...
- springcloud微服务基于redis集群的单点登录
springcloud微服务基于redis集群的单点登录 yls 2019-9-23 简介 本文介绍微服务架构中如何实现单点登录功能 创建三个服务: 操作redis集群的服务,用于多个服务之间共享数据 ...
- 小D课堂 - 新版本微服务springcloud+Docker教程_3-04 SpringCloud微服务核心组件Eureka介绍和闭源后影响
笔记 4.SpringCloud微服务核心组件Eureka介绍和闭源后影响 简介: SpringCloud体系介绍 官方地址:http://projec ...
- SpringCloud微服务基础 Eureka、Feign、Ribbon、Zuul、Hystrix、配置中心的基础使用
1.单点系统架构 传统项目架构 传统项目分为三层架构,将业务逻辑层.数据库访问层.控制层放入在一个项目中. 优点:适合于个人或者小团队开发,不适合大团队开发. 分布式项目架构 根据业务需求进行拆分成N ...
- SpringCloud微服务的Eureka
一.什么是微服务架构 架构设计概念,各服务间隔离(分布式也是隔离),自治(分布式依赖整体组合)其它特性(单一职责,边界,异步通信,独立部署)是分布式概念的跟严格执行SOA到微服务架构的演进过程 作用: ...
- Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul)
Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul) 1.Eureka Eureka是Netflix的一个子模块,也是核心模块之一.Eureka是 ...
- springCloud进阶(微服务架构&Eureka)
springCloud进阶(微服务架构&Eureka) 1. 微服务集群 1.1 为什么要集群 为了提供并发量,有时同一个服务提供者可以部署多个(商品服务).这个客户端在调用时要根据一定的负责 ...
- 【微服务】之五:轻松搞定SpringCloud微服务-调用远程组件Feign
上一篇文章讲到了负载均衡在Spring Cloud体系中的体现,其实Spring Cloud是提供了多种客户端调用的组件,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使 ...
随机推荐
- 网络安全服务(Network Security Services, NSS
网络安全服务(Network Security Services, NSS)是一套为网络安全服务而设计的库 支持支持安全的客户端和 服务器应用程序.使用NSS构建的应用程序可以支持SSL v2 和v3 ...
- localectl set-locale LANG=en_US.UTF-8
localectl set-locale LANG=en_US.UTF-8
- Nginx——Docker下安装部署
前言 Nginx 是一个高性能的 HTTP 和反向代理 web 服务器,同时也提供了 IMAP/POP3/SMTP 服务 . 一. 环境说明 docker: 18.09.9-ce nginx: 1.1 ...
- Linux进阶之综合练习
综合练习: 1.准备2台centos7系统的服务器,远程互相免密登录,以下所有题目过程中开启防火墙 2.给1号机和2号机使用光盘搭建本地yum源(永久生效) 3.给服务器1添加2块硬盘,1块1GB,1 ...
- linux python3安装whl包时报错解决:is not a supported wheel on this platform
原因1 你下载安装的包不是当前平台所支持的 原因2 你下载的包,不符合你所在的平台的安装whl的名称规范,所以出错.比如当前我要安装的包是:pymssql-2.1.5-cp36-cp36m-manyl ...
- idea 使用Springboot 编译报错
报错信息如下 Argument for @NotNull parameter 'url' of org/jetbrains/jps/model/impl/JpsUrlListImpl.addUrl m ...
- Java枚举类与注解详解——一篇文章读懂枚举类与注解详
目录 一.枚举类 ① 自定义枚举类 ② enum关键字定义枚举类 ③ enum 枚举类的方法 ④ enum 枚举类实现接口 二.注解 ① 生成文档相关注解 ②注解在编译时进行格式检查 ③注解跟踪代码的 ...
- Kali Linux 2021.2 发布 (Kaboxer, Kali-Tweaks, Bleeding-Edge & Privileged Ports)
Kali Linux 简介 Kali Linux 是基于 Debian 的 Linux 发行版,旨在进行高级渗透测试和安全审核.Kali Linux 包含数百种工具,可用于各种信息安全任务,例如渗透测 ...
- 回顾Servlet
1.新建一个Maven工程当做父工程!pom依赖! <!-- 依赖 --> <dependencies> <dependency> <groupId>j ...
- RISC-V与DSA计算机架构
RISC-V与DSA计算机架构 相信所有和计算机体系结构打过交道的朋友们都看过David Patterson与John Hennessy的煌煌巨作,<计算机体系架构:量化研究方法>.两位在 ...