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 其中是cloud-service项目中zuul配置的网关和路由(perfix和path)
至此,以上便是一个简单完成的springcloud微服务架构了!
上一篇: 软件开发打败了 80 %的程序员
推荐阅读
-
基于腾讯云服务器部署微信小程序后台服务(Python+Django)
-
基于腾讯云服务器部署微信小程序后台服务(Python+Django)
-
024.微服务之服务注册与发现(基于kubernetes / SpringCloud)
-
基于zookeeper实现springcloud微服务(服务提供者和服务消费者)
-
SpringCloud微服务(基于Eureka+Feign+Hystrix+Zuul)
-
Kite的学习历程SpringCloud之基于Nacos的服务提供者的创建
-
Kite的学习历程SpringCloud之基于Nacos的服务消费者的创建
-
PHP实例教程(2):构建基于PHP的微博客服务
-
PHP实例教程(4):构建基于PHP的微博客服务
-
PHP实例教程(1):构建基于PHP的微博客服务