springcloud(九):熔斷器Hystrix和Feign的全套應用案例(二)

 

1、. 建立Eureka-Server 服務中心項目

1. 建立Eureka-Server 服務中心項目架構以下html

 

2. pom.xmljava

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
</dependencies>

 

3.配置文件application.propertiesmysql

 1 #給當前服務起名  2 spring.application.name=eureka-server  3 
 4 #給當前服務指定端口號  5 server.port=8761
 6 
 7 #register-with-eureka :表示是將本身註冊到Eureka Server,默認爲true。  8 #由於當前應用就是Eureka Server,因此將其設置位false  9 #★當前服務時eureka的服務端仍是客戶端,當前是服務端所以false 10 eureka.client.register-with-eureka=false
11 
12 #fetch-registry :表示是否從Eureka Server獲取註冊信息,默認爲true。不須要同步數據就將其設爲false 13 #★是否從eureka服務中心獲取信息,由於當前是服務端所以不須要在服務端獲取信息 14 eureka.client.fetch-registry=false
15 
16 #defaultZone :設置與Eureka Server交互的地址, 17 #查詢服務和註冊服務都須要依賴這個地址。默認是http://localhost:8761/eureka ;
18 #多個地址可以使用 , 分隔。 19 #將本eureka服務的地址公開暴露給全部的客戶端,由於只有全部的客戶端知道eureka服務的地址,才能將信息 20 #註冊到eureka服務中心 21 eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
屬性文件

 

 

4.啓動類web

 1 package cn.kgc;  2 
 3 import org.springframework.boot.SpringApplication;  4 import org.springframework.boot.autoconfigure.SpringBootApplication;  5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;  6 
 7 //開啓eureka的服務端
 8 @EnableEurekaServer  9 @SpringBootApplication 10 public class EurekaServerApplication { 11 
12     public static void main(String[] args) { 13         SpringApplication.run(EurekaServerApplication.class, args); 14  } 15 
16 }
啓動類

 

 2、建立一方提供者eureka-client-provider-findcla

1. 項目結構以下:spring

 

 2.pom.xml文件內容sql

 1     <dependencies>
 2         <!--引用公共組件-->
 3         <dependency>
 4             <groupId>cn.kgc</groupId>
 5             <artifactId>eureka-common-clastu</artifactId>
 6             <version>1.0-SNAPSHOT</version>
 7         </dependency>
 8 
 9         <dependency>
10             <groupId>org.springframework.boot</groupId>
11             <artifactId>spring-boot-starter-jdbc</artifactId>
12         </dependency>
13         <dependency>
14             <groupId>org.springframework.boot</groupId>
15             <artifactId>spring-boot-starter-web</artifactId>
16         </dependency>
17         <dependency>
18             <groupId>org.mybatis.spring.boot</groupId>
19             <artifactId>mybatis-spring-boot-starter</artifactId>
20             <version>2.1.0</version>
21         </dependency>
22         <dependency>
23             <groupId>org.springframework.cloud</groupId>
24             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
25         </dependency>
26 
27         <!--修改版本號-->
28         <dependency>
29             <groupId>mysql</groupId>
30             <artifactId>mysql-connector-java</artifactId>
31             <version>5.1.38</version>
32         </dependency>
33 
34         <dependency>
35             <groupId>org.springframework.boot</groupId>
36             <artifactId>spring-boot-starter-test</artifactId>
37             <scope>test</scope>
38         </dependency>
39     </dependencies>
pom.xml

 

3.application.properties屬性文件apache

 1 #給當前服務起名字  2 spring.application.name=eureka-client-provider-findcla  3 
 4 #給當前服務設置端口號  5 server.port=8762  6 
 7 #指定eureka-server的服務地址  8 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/  9 
10 #數據源的配置 11 spring.datasource.driver-class-name=com.mysql.jdbc.Driver 12 spring.datasource.url=jdbc:mysql://192.168.117.145:3306/kh66 13 spring.datasource.username=root 14 spring.datasource.password=ok 15 
16 #別名配置 17 mybatis.type-aliases-package=cn.kgc.vo
application.properties

 

4.ClassesMapper.javajson

 1 package cn.kgc.mapper;  2 
 3 import cn.kgc.vo.Classes;  4 import org.apache.ibatis.annotations.Select;  5 
 6 import java.util.List;  7 
 8 /**
 9  * Created by Administrator on 2019/8/19. 10  */
11 public interface ClassesMapper { 12     @Select("select * from classes") 13     List<Classes> optionData(); 14 }
ClassesMapper.java

 

5.ClassesService.java服務器

 1 package cn.kgc.service;  2 
 3 import cn.kgc.vo.Classes;  4 import org.apache.ibatis.annotations.Select;  5 
 6 import java.util.List;  7 
 8 /**
 9  * Created by Administrator on 2019/8/19. 10  */
11 public interface ClassesService { 12 
13     List<Classes> optionData(); 14 }
ClassesService.java

 

6.ClassesServiceImpl.javamybatis

 1 package cn.kgc.service;  2 
 3 import cn.kgc.mapper.ClassesMapper;  4 import cn.kgc.vo.Classes;  5 import org.springframework.beans.factory.annotation.Autowired;  6 import org.springframework.stereotype.Service;  7 
 8 import java.util.List;  9 
10 @Service 11 public class ClassesServiceImpl implements ClassesService{ 12  @Autowired 13     private ClassesMapper classesMapper; 14 
15     public List<Classes> optionData() { 16         return classesMapper.optionData(); 17  } 18 }
ClassesServiceImpl.java

 

7.CenterController.java

 1 package cn.kgc.controller;  2 
 3 import cn.kgc.service.ClassesService;  4 import cn.kgc.vo.Classes;  5 import org.springframework.beans.factory.annotation.Autowired;  6 import org.springframework.web.bind.annotation.RequestMapping;  7 import org.springframework.web.bind.annotation.RestController;  8 
 9 import java.util.List; 10 
11 @RestController 12 public class CenterController { 13  @Autowired 14     private ClassesService classesService; 15     
16     @RequestMapping(value = "/option.do") 17     public List<Classes> optionData() { 18         return classesService.optionData(); 19  } 20 }
CenterController.java

 

8.啓動類

 1 package cn.kgc;  2 
 3 import org.mybatis.spring.annotation.MapperScan;  4 import org.springframework.boot.SpringApplication;  5 import org.springframework.boot.autoconfigure.SpringBootApplication;  6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;  7 
 8 
 9 @MapperScan("cn.kgc.mapper") 10 @EnableEurekaClient 11 @SpringBootApplication 12 public class EurekaClientProviderFindclaApplication { 13 
14     public static void main(String[] args) { 15         SpringApplication.run(EurekaClientProviderFindclaApplication.class, args); 16  } 17 
18 }
啓動類

 

 

3、建立多方提供者eureka-client-provider-findstu

1.項目結構以下:

 

2.pom.xml文件以下

 1 <dependencies>
 2         <!--引入公共組件-->
 3         <dependency>
 4             <groupId>cn.kgc</groupId>
 5             <artifactId>eureka-common-clastu</artifactId>
 6             <version>1.0-SNAPSHOT</version>
 7         </dependency>
 8 
 9         <dependency>
10             <groupId>org.springframework.boot</groupId>
11             <artifactId>spring-boot-starter-jdbc</artifactId>
12         </dependency>
13         <dependency>
14             <groupId>org.springframework.boot</groupId>
15             <artifactId>spring-boot-starter-web</artifactId>
16         </dependency>
17         <dependency>
18             <groupId>org.mybatis.spring.boot</groupId>
19             <artifactId>mybatis-spring-boot-starter</artifactId>
20             <version>2.1.0</version>
21         </dependency>
22         <dependency>
23             <groupId>org.springframework.cloud</groupId>
24             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
25         </dependency>
26        <!--添加版本號-->
27         <dependency>
28             <groupId>mysql</groupId>
29             <artifactId>mysql-connector-java</artifactId>
30             <version>5.1.38</version>
31         </dependency>
32         <dependency>
33             <groupId>org.springframework.boot</groupId>
34             <artifactId>spring-boot-starter-test</artifactId>
35             <scope>test</scope>
36         </dependency>
37 
38         <!--<dependency> 39  <groupId>org.apache.httpcomponents</groupId> 40  <artifactId>httpclient</artifactId> 41  <version>4.5.3</version> 42  </dependency> 43  <dependency> 44  <groupId>com.netflix.feign</groupId> 45  <artifactId>feign-httpclient</artifactId> 46  <version>8.16.2</version> 47  </dependency>-->
48     </dependencies>
pom.xml

 

3..StudentMapper.java

 1 package cn.kgc.mapper;  2 
 3 import cn.kgc.vo.Student;  4 import org.apache.ibatis.annotations.Delete;  5 import org.apache.ibatis.annotations.Insert;  6 import org.apache.ibatis.annotations.Update;  7 
 8 import java.util.List;  9 import java.util.Map; 10 
11 /**
12  * Created by Administrator on 2019/8/19. 13  */
14 public interface StudentMapper { 15     List<Map<String,Object>> showData(Student student); 16 
17     @Insert("INSERT INTO kh66.student (sname, password, subject, result, cid) VALUES(#{sname},#{password},#{subject},#{result},#{cid}) ") 18     int addStu(Student student); 19 
20     @Update("UPDATE kh66.student SET sname =#{sname}, password =#{password}, subject = #{subject}, result =#{result}, cid =#{cid} WHERE sid =#{sid} ") 21     int editStu(Student student); 22 
23     @Delete("delete from student where sid=#{sid}") 24     int delStu(Integer sid); 25 }
StudentMapper.java

 

4.StudentMapper.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper  3  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  4  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="cn.kgc.mapper.StudentMapper">
 6     <select id="showData" parameterType="Student" resultType="map">
 7  select s.*,c.cname from student s,classes c where s.cid=c.cid  8          <if test="sid!=null">
 9  and s.sid=#{sid} 10          </if>
11         <if test="cid!=null and cid!=-1">
12  and s.cid=#{cid} 13         </if>
14         <if test="sname!=null">
15  and sname like concat('%',#{sname},'%') 16         </if>
17     </select>
18 </mapper>
StudentMapper.xml

 

5..StudentService.java

 1 package cn.kgc.service;  2 
 3 import cn.kgc.vo.Student;  4 import org.apache.ibatis.annotations.Delete;  5 import org.apache.ibatis.annotations.Insert;  6 import org.apache.ibatis.annotations.Update;  7 import org.springframework.web.bind.annotation.RequestBody;  8 
 9 import java.util.List; 10 import java.util.Map; 11 
12 /**
13  * Created by Administrator on 2019/8/19. 14  */
15 public interface StudentService { 16     List<Map<String,Object>> showData(Student student); 17 
18     int addStu(Student student); 19 
20     int editStu( Student student); 21 
22     int delStu(Integer sid); 23 }
StudentService.java

 

6.StudentServiceImpl.java

 1 package cn.kgc.service;  2 
 3 import cn.kgc.mapper.StudentMapper;  4 import cn.kgc.vo.Student;  5 import org.springframework.beans.factory.annotation.Autowired;  6 import org.springframework.stereotype.Service;  7 import org.springframework.transaction.annotation.Transactional;  8 import org.springframework.web.bind.annotation.RequestBody;  9 
10 import java.util.List; 11 import java.util.Map; 12 
13 @Service 14 @Transactional 15 public class StudentServiceImpl implements StudentService { 16  @Autowired 17     private StudentMapper studentMapper; 18 
19  @Override 20     public List<Map<String, Object>> showData( Student student) { 21         return studentMapper.showData(student); 22  } 23 
24  @Override 25     public int addStu(Student student) { 26         return studentMapper.addStu(student); 27  } 28 
29  @Override 30     public int editStu(Student student) { 31         return studentMapper.editStu(student); 32  } 33 
34  @Override 35     public int delStu(Integer sid) { 36         return studentMapper.delStu(sid); 37  } 38 }
StudentServiceImpl.java

 

7.CenterController.java

 1 package cn.kgc.controller;  2 
 3 import cn.kgc.mapper.StudentMapper;  4 import cn.kgc.service.StudentService;  5 import cn.kgc.vo.Student;  6 import com.fasterxml.jackson.core.JsonProcessingException;  7 import com.fasterxml.jackson.databind.ObjectMapper;  8 import com.netflix.ribbon.proxy.annotation.Http;  9 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.stereotype.Service; 11 import org.springframework.transaction.annotation.Transactional; 12 import org.springframework.web.bind.annotation.*; 13 
14 import java.util.List; 15 import java.util.Map; 16 
17 @RestController 18 public class CenterController{ 19     ObjectMapper om=new ObjectMapper(); 20 
21  @Autowired 22     private StudentService studentService; 23 
24 
25     @RequestMapping(value = "/data.do") 26     public List<Map<String,Object>> showData(@RequestBody Student student){ 27         System.out.println("stu=showData>>>>>>>>student:"+student.getSid()); 28         return studentService.showData(student); 29  } 30 
31     @RequestMapping(value = "/add.do") 32     public int addStu(@RequestBody Student student){ 33         return studentService.addStu(student); 34  } 35 
36     @RequestMapping(value = "/edit.do") 37     public int editStu(@RequestBody Student student){ 38         return studentService.editStu(student); 39  } 40 
41 
42     @RequestMapping(value = "/del.do") 43     public int delStu(@RequestParam("sid") Integer sid) { 44         return studentService.delStu(sid); 45  } 46 /*
47  @RequestMapping(value = "/data.do",consumes = "application/json") 48  public String showData(@RequestBody Student student) throws Exception{ 49  System.out.println("stu=showData>>>>>>>>student:"+student.getSid()); 50  String str=om.writeValueAsString(studentService.showData(student)); 51 
52  return str; 53  } 54 
55  @RequestMapping(value = "/add.do",consumes = "application/json") 56  public String addStu(@RequestBody Student student) throws Exception{ 57  String str=om.writeValueAsString(studentService.addStu(student)); 58  return str; 59  } 60 
61  @RequestMapping(value = "/edit.do") 62  public String editStu(@RequestBody Student student) throws Exception{ 63  String str=om.writeValueAsString(studentService.editStu(student)); 64  return str; 65  } 66 
67 
68  @RequestMapping(value = "/del.do") 69  public String delStu(@RequestParam("sid") Integer sid) throws Exception{ 70  String str=om.writeValueAsString(studentService.delStu(sid)); 71  return str; 72  }*/
73 }
CenterController.java

 

8.啓動類

 1 package cn.kgc;  2 
 3 import org.mybatis.spring.annotation.MapperScan;  4 import org.springframework.boot.SpringApplication;  5 import org.springframework.boot.autoconfigure.SpringBootApplication;  6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;  7 
 8 @MapperScan("cn.kgc.mapper")  9 @EnableEurekaClient 10 @SpringBootApplication 11 public class EurekaClientProviderFindstuApplication { 12 
13     public static void main(String[] args) { 14         SpringApplication.run(EurekaClientProviderFindstuApplication.class, args); 15  } 16 
17 }
啓動類

 

 4、建立調用者項目eureka-client-consumer-clastu-p

使用feign調用上述2個提供者項目裏的controller拿值,並使用熔斷器,進行錯誤請求處理

1.項目結構以下

 

2.pom.xml文件內容以下

 1     <dependencies>
 2         <dependency>
 3             <groupId>cn.kgc</groupId>
 4             <artifactId>eureka-common-clastu</artifactId>
 5             <version>1.0-SNAPSHOT</version>
 6         </dependency>
 7         <dependency>
 8             <groupId>org.springframework.boot</groupId>
 9             <artifactId>spring-boot-starter-web</artifactId>
10         </dependency>
11         <dependency>
12             <groupId>org.springframework.cloud</groupId>
13             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
14         </dependency>
15         <dependency>
16             <groupId>org.springframework.cloud</groupId>
17             <artifactId>spring-cloud-starter-openfeign</artifactId>
18         </dependency>
19 
20         <dependency>
21             <groupId>org.springframework.boot</groupId>
22             <artifactId>spring-boot-starter-test</artifactId>
23             <scope>test</scope>
24         </dependency>
25 
26         <!--<dependency> 27  <groupId>org.apache.httpcomponents</groupId> 28  <artifactId>httpclient</artifactId> 29  <version>4.5.3</version> 30  </dependency> 31  <dependency> 32  <groupId>com.netflix.feign</groupId> 33  <artifactId>feign-httpclient</artifactId> 34  <version>8.16.2</version> 35  </dependency>-->
36     </dependencies>
pom.xml

 

3.application.properties

1 spring.application.name=eureka-client-consumer-clastu-p 2 
3 server.port=8766 4 
5 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ 6 
7 #將feign上集成的斷路器設置位有效開啓狀態 8 feign.hystrix.enabled=true
application.properties

 

4.一方feign接口,ClassesProviderFeign.java

package cn.kgc.feign; import cn.kgc.vo.Classes; import cn.kgc.vo.Student; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; import java.util.Map; @FeignClient(name = "eureka-client-provider-findcla",fallback =ClassesProviderFeignFallBack.class ) public interface ClassesProviderFeign { @RequestMapping("/option.do") public String optionData(); }
ClassesProviderFeign.java

 

5.一方feign接口的容錯類:ClassesProviderFeignFallBack.java

 1 package cn.kgc.feign;  2 
 3 import cn.kgc.vo.Classes;  4 import org.springframework.cloud.openfeign.FeignClient;  5 import org.springframework.stereotype.Component;  6 import org.springframework.web.bind.annotation.RequestMapping;  7 
 8 import java.util.List;  9 
10 //classes容錯處理類
11 @Component 12 public class ClassesProviderFeignFallBack implements ClassesProviderFeign{ 13 
14  @Override 15     public String optionData() { 16         return "提供者服務器請求異常,請稍後再試...."; 17  } 18 }
ClassesProviderFeignFallBack.java

 

6.多方feign接口,StudentProviderFeign.java

 1 package cn.kgc.feign;  2 
 3 import cn.kgc.vo.Classes;  4 import cn.kgc.vo.Student;  5 import org.springframework.cloud.openfeign.FeignClient;  6 import org.springframework.web.bind.annotation.RequestBody;  7 import org.springframework.web.bind.annotation.RequestMapping;  8 import org.springframework.web.bind.annotation.RequestParam;  9 
10 import java.util.ArrayList; 11 import java.util.List; 12 import java.util.Map; 13 
14 @FeignClient(name = "eureka-client-provider-findstu",fallback = StudentProviderFeignFallBack.class) 15 public interface StudentProviderFeign { 16 
17     @RequestMapping(value = "/data.do") 18     public String showData(@RequestBody Student student); 19 
20     @RequestMapping(value = "/add.do") 21     public String addStu(@RequestBody Student student); 22 
23     @RequestMapping(value = "/edit.do") 24     public String editStu(@RequestBody Student student); 25 
26     @RequestMapping(value = "/del.do") 27     public String delStu(@RequestParam("sid") Integer sid) ; 28 
29 }
StudentProviderFeign.java

 

7.多方feign接口的容錯類:StudentProviderFeignFallBack.java

 1 package cn.kgc.feign;  2 
 3 import cn.kgc.vo.Student;  4 import org.springframework.stereotype.Component;  5 
 6 import java.util.List;  7 import java.util.Map;  8 
 9 @Component 10 public class StudentProviderFeignFallBack implements StudentProviderFeign{ 11 
12     public String showData(Student student) { 13         return "提供服務器數據沒有查到!"; 14  } 15 
16     public String addStu(Student student) { 17         return "提供服務器數據添加失敗!"; 18  } 19 
20     public String editStu(Student student) { 21         return "提供服務器修改數據失敗!"; 22  } 23 
24     public String delStu(Integer sid) { 25         return "刪除數據失敗!"; 26  } 27 }
tudentProviderFeignFallBack.java

 

8.CenterController.java

 1 package cn.kgc.controller;  2 
 3 import cn.kgc.feign.ClassesProviderFeign;  4 import cn.kgc.feign.StudentProviderFeign;  5 import cn.kgc.vo.Classes;  6 import cn.kgc.vo.Student;  7 import feign.Headers;  8 import org.springframework.beans.factory.annotation.Autowired;  9 import org.springframework.web.bind.annotation.RequestBody; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RestController; 12 
13 import java.util.HashMap; 14 import java.util.List; 15 import java.util.Map; 16 
17 @RestController 18 public class CenterController { 19 
20  @Autowired 21     private ClassesProviderFeign classesProviderFeign; 22 
23  @Autowired 24     private StudentProviderFeign studentProviderFeign; 25 
26     @RequestMapping(value = "/option.do",consumes = "application/json" ) 27     public String optionData(){ 28         return classesProviderFeign.optionData(); 29  } 30 
31     @RequestMapping(value = "/data.do",consumes = "application/json") 32     public String showData(@RequestBody Student student){ 33         System.out.println("clastu=showData>>>>>>>>student:"+student.getSid()); 34         return studentProviderFeign.showData(student); 35  } 36 
37 
38     @RequestMapping(value = "/info.do",consumes = "application/json") 39     public Map<String,Object> getInfo(@RequestBody Student student){ 40         Map<String,Object> map=new HashMap<String,Object>(); 41         System.out.println("clastu=getInfo>>>>>>>>student:"+student.getSid()); 42         map.put("stu",studentProviderFeign.showData(student)); 43         map.put("clalist",classesProviderFeign.optionData()); 44         return map; 45  } 46 
47 
48     @RequestMapping(value = "/add.do",consumes = "application/json") 49     public String addStu(Student student){ 50         return studentProviderFeign.addStu(student); 51  } 52 
53     @RequestMapping(value = "/edit.do",consumes = "application/json") 54     public String editStu(Student student){ 55         return studentProviderFeign.editStu(student); 56  } 57 
58     @RequestMapping(value = "/del.do",consumes = "application/json") 59     public String delStu(Integer sid){ 60         return studentProviderFeign.delStu(sid); 61  } 62 }
CenterController.java

 

9.啓動類

 1 package cn.kgc;  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.openfeign.EnableFeignClients;  7 
 8 @EnableFeignClients  9 @EnableEurekaClient 10 @SpringBootApplication 11 public class EurekaClientConsumerClastuPApplication { 12 
13     public static void main(String[] args) { 14         SpringApplication.run(EurekaClientConsumerClastuPApplication.class, args); 15  } 16 
17 }
啓動類

 

10.測試正常流程,

按順序啓動1、2、3、四項目

 

 

11.容錯的測試

停掉三的項目,由於三項目是多方的提供者,停掉則四訪問不到會走容錯

 

 

注:本帖爲「"Holly老師"原創,轉載請註明出處,謝謝!

出處:https://www.cnblogs.com/holly8/p/11415344.html  

 

原文出處:https://www.cnblogs.com/holly8/p/11415344.html

相關文章
相關標籤/搜索