一、搭建注册中心

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. 性能工具 stream 最新版本5.10 The STREAM benchmark

    官网下载最新性能工具 stream 最新版本5.10 https://github.com/jeffhammond/STREAM 官网下载最新性能工具 stream 最新版本5.10   http:/ ...

  2. ipmi配置方法-20200328

    ipmi配置错误-20200328[root@localhost home]# ipmitool lan set 1 ipsrc staticCould not open device at /dev ...

  3. shell初学之PHP

    初次接触脚本,写了一个通过Apache实现PHP动态网站的脚本: #!/bin/bash yum -y install php rm -rf /etc/httpd/conf.d/welcome.con ...

  4. Linux中级之keepalived配置

    hacmp: ibm的高可用集群软件,并且是商业的(收费),一般用于非x86架构机器当中 AIX,Unix 去IOE:ibm,oracle,emckeepalived: 一款高可用集群软件,利用vrr ...

  5. 第六章 XaaS和IT服务标准

    从云计算(Cloud Computing)谈起 云计算是一种按使用量付费的模式,这种模式提供可用的.便捷的.按需的网络访问,进入可配置的计算资源共享池(资源包括网络,服务器,存储,应用软件,服务),这 ...

  6. XShell本地上传文件到Ubuntu上及从Ubuntu下载文件到本地

    使用XShell本地上传文件到Ubuntu上及从Ubuntu下载文件到本地. 1.第一种方法是最常用的 :如果下载了Xshell和Xftp,Ctrl+Alt+F就可以选择文件的互传了!(虚拟机/云服务 ...

  7. 『动善时』JMeter基础 — 31、JMeter中BeanShell断言详解

    目录 1.BeanShell简介 2.Beanshell的内置变量和方法 3.BeanShell断言界面详解 4.BeanShell断言的使用 (1)测试计划内包含的元件 (2)登陆接口请求界面内容 ...

  8. Vue的常用特性

    Vue的常用特性 一.表单基本操作 都是通过v-model 单选框 1. 两个单选框需要同时通过v-model 双向绑定 一个值 2. 每一个单选框必须要有value属性 且value值不能一样 3. ...

  9. CVPR2020行人重识别算法论文解读

    CVPR2020行人重识别算法论文解读 Cross-modalityPersonre-identificationwithShared-SpecificFeatureTransfer 具有特定共享特征变换 ...

  10. deeplearning模型量化实战

    deeplearning模型量化实战 MegEngine 提供从训练到部署完整的量化支持,包括量化感知训练以及训练后量化,凭借"训练推理一体"的特性,MegEngine更能保证量化 ...