一、搭建注册中心

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)的更多相关文章

  1. springcloud微服务实战:Eureka+Zuul+Feign/Ribbon+Hystrix Turbine+SpringConfig+sleuth+zipkin

    相信现在已经有很多小伙伴已经或者准备使用springcloud微服务了,接下来为大家搭建一个微服务框架,后期可以自己进行扩展.会提供一个小案例: 服务提供者和服务消费者 ,消费者会调用提供者的服务,新 ...

  2. springcloud微服务实战:Eureka+Zuul+Ribbon+Hystrix+SpringConfig

    原文地址:http://blog.csdn.net/yp090416/article/details/78017552 springcloud微服务实战:Eureka+Zuul+Ribbon+Hyst ...

  3. springcloud微服务基于redis集群的单点登录

    springcloud微服务基于redis集群的单点登录 yls 2019-9-23 简介 本文介绍微服务架构中如何实现单点登录功能 创建三个服务: 操作redis集群的服务,用于多个服务之间共享数据 ...

  4. 小D课堂 - 新版本微服务springcloud+Docker教程_3-04 SpringCloud微服务核心组件Eureka介绍和闭源后影响

    笔记 4.SpringCloud微服务核心组件Eureka介绍和闭源后影响     简介:         SpringCloud体系介绍             官方地址:http://projec ...

  5. SpringCloud微服务基础 Eureka、Feign、Ribbon、Zuul、Hystrix、配置中心的基础使用

    1.单点系统架构 传统项目架构 传统项目分为三层架构,将业务逻辑层.数据库访问层.控制层放入在一个项目中. 优点:适合于个人或者小团队开发,不适合大团队开发. 分布式项目架构 根据业务需求进行拆分成N ...

  6. SpringCloud微服务的Eureka

    一.什么是微服务架构 架构设计概念,各服务间隔离(分布式也是隔离),自治(分布式依赖整体组合)其它特性(单一职责,边界,异步通信,独立部署)是分布式概念的跟严格执行SOA到微服务架构的演进过程 作用: ...

  7. Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul)

    Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul) 1.Eureka Eureka是Netflix的一个子模块,也是核心模块之一.Eureka是 ...

  8. springCloud进阶(微服务架构&Eureka)

    springCloud进阶(微服务架构&Eureka) 1. 微服务集群 1.1 为什么要集群 为了提供并发量,有时同一个服务提供者可以部署多个(商品服务).这个客户端在调用时要根据一定的负责 ...

  9. 【微服务】之五:轻松搞定SpringCloud微服务-调用远程组件Feign

    上一篇文章讲到了负载均衡在Spring Cloud体系中的体现,其实Spring Cloud是提供了多种客户端调用的组件,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使 ...

随机推荐

  1. [Linux] Linux命令行与Shell脚本编程大全 Part.3

    Shell Shell 是管理命令行的程序(包在内核外的壳) 不同Shell版本演化关系如下,一般Linux中会自动安装Sh和Bash(Bash比Sh好写,Zsh最好用) .bashrc:Bash 的 ...

  2. [刷题] PTA 7-32 说反话-加强版

    题目描述: 给定一个英语句子,各个单词之间用空格分隔.要求编写程序,将所有单词倒序输出 输入示例: Hello World Here I Come 输出示例: Come I Here World He ...

  3. 6.6 rsync:文件同步工具

    rsync 是一款开源的.快速的.多功能的.可实现全量及增量的本地或远程数据镜像同步备份的优秀工具.rsync适用于Unix/Linux/Windows等多种操作系统平台.   rsync命令有三种常 ...

  4. BUCK BOOST学习总结

    首先对于我这种电源方面的小白来说 关于电源用的最多的就是线性稳压了 开关类的如  TI 的TPS系列  我是只知道应用电路而不知道具体原理的 但是长此以往也不是个办法 于是今天就带打家详细的来讲一下 ...

  5. 大对象数据LOB的应用

    概述 由于无结构的数据往往都是大型的,存储量特别大,而LOB(large object)类型主要用来支持无结构的大型数据. 用户可以利用LOB数据类型来存储大型的无结构数据,特别是文本,图形,视频和音 ...

  6. teprunner测试平台定时任务这次终于稳了

    teprunner测试平台已经有一个多月没有更新了,主要原因是定时任务不够稳定,经过反复试错,找到了解决办法,这次终于稳定了. 本文开发内容 作为测试平台而言,定时任务算是必备要素了,只有跑起来的自动 ...

  7. Python中PyQuery库的使用

    pyquery库是jQuery的Python实现,可以用于解析HTML网页内容,我个人写过的一些抓取网页数据的脚本就是用它来解析html获取数据的. 它的官方文档地址是:http://packages ...

  8. PDF 文件编写器 C# 类库(版本 1.28.0)使用详解

    PDF File Writer 是一个 C# .NET 类库,允许应用程序创建 PDF 文件. PDF File Writer C# 类库使 .NET 应用程序能够生成 PDF 文档.该库使应用程序免 ...

  9. SSTI漏洞-fastapi

    0x00 原理   SSTI漏洞全称服务器模板注入漏洞,服务器模板接收了用户输入的恶意代码,未经过滤便在服务端执行并通过渲染模板返回给用户,使得用户可以通过构造恶意代码在服务端执行命令. 0x01 c ...

  10. AI推理单元

    AI推理单元 推理服务供了一套面向 MLU(Machine Learning Unit,机器学习单元)设备的类似服务器的推理接口(C++11标准),以及模型加载与管理,推理任务调度等功能,极大地简化了 ...