Ribbon负载均衡及Feign消费者调用服务
微服务调用Ribbon
简介
Ribbon是Netflix发布的负载均衡器,它有助于控制HTTP和TCP的客户端的行为。为Ribbon配置服务提供者地址后,Ribbon就可基于某种负载均衡算法,自动地帮助服务消费者去请求。Ribbon默认为我们提供了很多负载均衡算法,例如轮询、随机等。当然,我们也可为Ribbon实现自定义的负载均衡算法。
在Spring Cloud中,当Ribbon与Eureka配合使用时,Ribbon可自动从Eureka Server获取服务提供者地址列表,并基于负载均衡算法,请求其中一个服务提供者实例。展示了Ribbon与Eureka配合使用时的架构。
初步应用
Ribbon是客户端负载均衡,所以肯定集成再消费端,也就是consumer端我们修改microservice-student-consumer-80
pom.xml 加入 ribbon相关依赖
1 <!--ribbon相关依赖--> 2 <dependency> 3 <groupId>org.springframework.cloud</groupId> 4 <artifactId>spring-cloud-starter-eureka</artifactId> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.cloud</groupId> 8 <artifactId>spring-cloud-starter-ribbon</artifactId> 9 </dependency> 10 <dependency> 11 <groupId>org.springframework.cloud</groupId> 12 <artifactId>spring-cloud-starter-config</artifactId> 13 </dependency>
添加application.yml配置
1 server: 2 port: 81 3 context-path: / 4 eureka: 5 client: 6 service-url: 7 defaultZone: http://eureka2001.javaqi.com:2001/eureka/,http://eureka2002.javaqi.com:2002/eureka/,http://eureka2003.javaqi.com:2003/eureka/ 8 register-with-eureka: false
SpringCloudConfig加个负载均衡配置
@LoadBalanced
1 package com.javaqi.microservicestudentconsumer80.config; 2 3 import com.netflix.loadbalancer.IRule; 4 import com.netflix.loadbalancer.RetryRule; 5 import org.springframework.cloud.client.loadbalancer.LoadBalanced; 6 import org.springframework.context.annotation.Bean; 7 import org.springframework.context.annotation.Configuration; 8 import org.springframework.web.client.RestTemplate; 9 10 @Configuration 11 public class SpringCloudConfig { 12 13 @LoadBalanced // 引入ribbon负载均衡 14 @Bean 15 public RestTemplate getRestTemplate(){ 16 return new RestTemplate(); 17 } 18 19 }
因为和eureka整合,所以启动类StudentConsumerApplication_80 加个注解
@EnableEurekaClient
修改下StudentConsumerController的PRE_HOST,改成指定的微服务应用名称;
在服务提供者microservice-student-provider-1001的application.yml加下配置,指定下应用名称:
application:
name: microservice-student
所以服务调用者这边的控制器里PRE_HOST改成 http://MICROSERVICE-STUDENT即可;MICROSERVICE-STUDENT为Eureka注册中心的应用名称
先启动三个eureka,然后再启动服务提供者,再启动服务消费者;
Ribbon负载均衡
按照microservice-student-provider-1001建立一个microservice-student-provider子项目,然后将microservice-student-provider-1001这个子项目干掉;
前面搭建了初步例子,但是还没实现真正负载均衡,我们这里要先搞三个服务提供者集群,然后才能演示负载均衡,以及负载均衡策略;
新建项目microservice-student-provider-1002,microservice-student-provider-1003
pom.xml,application.yml,以及java类都复制一份,启动类名称对应的改下;
yml配置文件有两处要对应的改下,port端口改下,以及服务实例名称改下;
pom.xml
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 <parent> 6 <groupId>com.javaqi</groupId> 7 <artifactId>qimicroservice</artifactId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <artifactId>microservice-student-provider</artifactId> 11 12 <properties> 13 <java.version>1.8</java.version> 14 </properties> 15 16 <dependencies> 17 <dependency> 18 <groupId>com.javaqi</groupId> 19 <artifactId>microservice-common</artifactId> 20 </dependency> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-web</artifactId> 24 </dependency> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-test</artifactId> 28 <scope>test</scope> 29 </dependency> 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-data-jpa</artifactId> 33 </dependency> 34 <dependency> 35 <groupId>mysql</groupId> 36 <artifactId>mysql-connector-java</artifactId> 37 </dependency> 38 <dependency> 39 <groupId>org.springframework.boot</groupId> 40 <artifactId>spring-boot-starter-tomcat</artifactId> 41 </dependency> 42 <dependency> 43 <groupId>com.alibaba</groupId> 44 <artifactId>druid-spring-boot-starter</artifactId> 45 </dependency> 46 <!-- 修改后立即生效,热部署 --> 47 <dependency> 48 <groupId>org.springframework</groupId> 49 <artifactId>springloaded</artifactId> 50 </dependency> 51 <dependency> 52 <groupId>org.springframework.boot</groupId> 53 <artifactId>spring-boot-devtools</artifactId> 54 </dependency> 55 56 <!--添加注册中心Eureka相关配置--> 57 <dependency> 58 <groupId>org.springframework.cloud</groupId> 59 <artifactId>spring-cloud-starter-eureka</artifactId> 60 </dependency> 61 <dependency> 62 <groupId>org.springframework.cloud</groupId> 63 <artifactId>spring-cloud-starter-config</artifactId> 64 </dependency> 65 <dependency> 66 <groupId>com.javaqi</groupId> 67 <artifactId>microservice-common</artifactId> 68 <version>1.0-SNAPSHOT</version> 69 <scope>compile</scope> 70 </dependency> 71 72 <!-- actuator监控引入 --> 73 <dependency> 74 <groupId>org.springframework.boot</groupId> 75 <artifactId>spring-boot-starter-actuator</artifactId> 76 </dependency> 77 <dependency> 78 <groupId>com.javaqi</groupId> 79 <artifactId>microservice-student-provider-1001</artifactId> 80 <version>1.0-SNAPSHOT</version> 81 <scope>compile</scope> 82 </dependency> 83 </dependencies> 84 85 <build> 86 <plugins> 87 <plugin> 88 <groupId>org.springframework.boot</groupId> 89 <artifactId>spring-boot-maven-plugin</artifactId> 90 </plugin> 91 </plugins> 92 </build> 93 </project>
application.yml
1 server: 2 port: 1001 3 context-path: / 4 spring: 5 datasource: 6 type: com.alibaba.druid.pool.DruidDataSource 7 driver-class-name: com.mysql.jdbc.Driver 8 url: jdbc:mysql://localhost:3306/xufanqi?useUnicode=true&characterEncoding=utf8 9 username: root 10 password: 123 11 jpa: 12 hibernate: 13 ddl-auto: update 14 show-sql: true 15 application: 16 name: microservice-student 17 profiles: provider-1001 18 19 eureka: 20 instance: 21 hostname: localhost 22 appname: microservice-student 23 instance-id: microservice-student:1001 24 prefer-ip-address: true 25 client: 26 service-url: 27 defaultZone: http://eureka2001.javaqi.com:2001/eureka/,http://eureka2002.javaqi.com:2002/eureka/,http://eureka2003.javaqi.com:2003/eureka/ 28 29 info: 30 groupId: com.javaqi.testSpringcloud 31 artifactId: microservice-student-provider-1001 32 version: 1.0-SNAPSHOT 33 userName: http://javaqi.com 34 phone: 123456 35 36 --- 37 server: 38 port: 1002 39 context-path: / 40 spring: 41 datasource: 42 type: com.alibaba.druid.pool.DruidDataSource 43 driver-class-name: com.mysql.jdbc.Driver 44 url: jdbc:mysql://localhost:3306/xufanqi?useUnicode=true&characterEncoding=utf8 45 username: root 46 password: 123 47 jpa: 48 hibernate: 49 ddl-auto: update 50 show-sql: true 51 application: 52 name: microservice-student 53 profiles: provider-1002 54 55 eureka: 56 instance: 57 hostname: localhost 58 appname: microservice-student 59 instance-id: microservice-student:1002 60 prefer-ip-address: true 61 client: 62 service-url: 63 defaultZone: http://eureka2001.javaqi.com:2001/eureka/,http://eureka2002.javaqi.com:2002/eureka/,http://eureka2003.javaqi.com:2003/eureka/ 64 65 info: 66 groupId: com.javaxl.testSpringcloud 67 artifactId: microservice-student-provider-1002 68 version: 1.0-SNAPSHOT 69 userName: http://javaqi.com 70 phone: 123456 71 72 --- 73 server: 74 port: 1003 75 context-path: / 76 spring: 77 datasource: 78 type: com.alibaba.druid.pool.DruidDataSource 79 driver-class-name: com.mysql.jdbc.Driver 80 url: jdbc:mysql://localhost:3306/xufanqi?useUnicode=true&characterEncoding=utf8 81 username: root 82 password: 123 83 jpa: 84 hibernate: 85 ddl-auto: update 86 show-sql: true 87 application: 88 name: microservice-student 89 profiles: provider-1003 90 91 eureka: 92 instance: 93 hostname: localhost 94 appname: microservice-student 95 instance-id: microservice-student:1003 96 prefer-ip-address: true 97 client: 98 service-url: 99 defaultZone: http://eureka2001.javaqi.com:2001/eureka/,http://eureka2002.javaqi.com:2002/eureka/,http://eureka2003.javaqi.com:2003/eureka/ 100 101 info: 102 groupId: com.javaqi.testSpringcloud 103 artifactId: microservice-student-provider-1003 104 version: 1.0-SNAPSHOT 105 userName: http://javaqi.com 106 phone: 123456
启动类MicroserviceStudentProviderApplication
1 package com.javaqi.microservicestudentprovider; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.boot.autoconfigure.domain.EntityScan; 6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 7 8 @EntityScan("com.javaqi.*.*") 9 @EnableEurekaClient 10 @SpringBootApplication 11 public class MicroserviceStudentProviderApplication { 12 13 public static void main(String[] args) { 14 SpringApplication.run(MicroserviceStudentProviderApplication.class, args); 15 } 16 17 }
StudentProviderController
1 package com.javaqi.microservicestudentprovider.controller; 2 3 import com.javaqi.microservicecommon.entity.Student; 4 import com.javaqi.microservicestudentprovider.service.StudentService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.beans.factory.annotation.Value; 7 import org.springframework.web.bind.annotation.*; 8 9 import java.util.List; 10 11 @RestController 12 @RequestMapping("/student") 13 public class StudentProviderController { 14 15 @Autowired 16 private StudentService studentService; 17 18 @Value("${server.port}") 19 private String port; 20 21 @PostMapping(value="/save") 22 public boolean save(Student student){ 23 try{ 24 studentService.save(student); 25 return true; 26 }catch(Exception e){ 27 return false; 28 } 29 } 30 31 @GetMapping(value="/list") 32 public List<Student> list(){ 33 return studentService.list(); 34 } 35 36 @GetMapping(value="/get/{id}") 37 public Student get(@PathVariable("id") Integer id){ 38 return studentService.findById(id); 39 } 40 41 @GetMapping(value="/delete/{id}") 42 public boolean delete(@PathVariable("id") Integer id){ 43 try{ 44 studentService.delete(id); 45 return true; 46 }catch(Exception e){ 47 return false; 48 } 49 } 50 51 @RequestMapping("/ribbon") 52 public String ribbon(){ 53 return "工号【"+port+"】正在为您服务"; 54 } 55 }
StudentConsumerController
1 package com.javaqi.microservicestudentconsumer80.controller; 2 3 import com.javaqi.microservicecommon.entity.Student; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.web.bind.annotation.*; 6 import org.springframework.web.client.RestTemplate; 7 8 import java.util.List; 9 10 @RestController 11 @RequestMapping("/student") 12 public class StudentConsumerController { 13 14 private final static String SERVER_IP_PORT = "http://MICROSERVICE-STUDENT"; 15 16 @Autowired 17 private RestTemplate restTemplate; 18 19 @PostMapping(value="/save") 20 private boolean save(Student student){ 21 return restTemplate.postForObject(SERVER_IP_PORT+"/student/save", student, Boolean.class); 22 } 23 24 @GetMapping(value="/list") 25 public List<Student> list(){ 26 return restTemplate.getForObject(SERVER_IP_PORT+"/student/list", List.class); 27 } 28 29 @GetMapping(value="/get/{id}") 30 public Student get(@PathVariable("id") Integer id){ 31 return restTemplate.getForObject(SERVER_IP_PORT+"/student/get/"+id, Student.class); 32 } 33 34 @GetMapping(value="/delete/{id}") 35 public boolean delete(@PathVariable("id") Integer id){ 36 try{ 37 restTemplate.getForObject(SERVER_IP_PORT+"/student/delete/"+id, Boolean.class); 38 return true; 39 }catch(Exception e){ 40 return false; 41 } 42 } 43 @RequestMapping("/ribbon") 44 public String ribbon() { 45 return restTemplate.getForObject(SERVER_IP_PORT + "/student/ribbon", String.class); 46 } 47 48 }
配置
访问结果
停掉1003
Feign简介及应用
简介
声明式服务调用Feign简单介绍下;
Feign是一个声明式的Web Service客户端,它使得编写Web Serivce客户端变得更加简单。
我们只需要使用Feign来创建一个接口并用注解来配置它既可完成。它具备可插拔的注解支持,包括Feign注解和JAX-RS注解。
Feign也支持可插拔的编码器和解码器。Spring Cloud为Feign增加了对Spring MVC注解的支持,还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。
这段话看起来比较懵逼,这里说下实际使用,前面Ribbon调用服务提供者,我们通过restTemplate调用,
缺点是,多个地方调用,同一个请求要写多次,不方便统一维护,这时候Feign来了,就直接把请求统一搞一个service作为FeignClient,
然后其他调用Controller需要用到的,直接注入service,直接调用service方法即可;同时Feign整合了Ribbon和Eureka,所以要配置负载均衡的话,直接配置Ribbon即可,无其他特殊地方;当然Fiegn也整合了服务容错保护,断路器Hystrix,后面再说。
应用
1、在common项目里建一个service(实际项目肯定是多个service)作为Feign客户端,用Feign客户端来调用服务器提供者,当然可以配置负载均衡;Feign客户端定义的目的,就是为了方便给其他项目调用;
修改 microservice-common
pom.xml引入Feign依赖:
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 <parent> 6 <groupId>com.javaqi</groupId> 7 <artifactId>qimicroservice</artifactId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <artifactId>microservice-common</artifactId> 11 12 13 <properties> 14 <java.version>1.8</java.version> 15 </properties> 16 17 <dependencies> 18 <dependency> 19 <groupId>org.springframework.boot</groupId> 20 <artifactId>spring-boot-starter-web</artifactId> 21 </dependency> 22 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-test</artifactId> 26 <scope>test</scope> 27 </dependency> 28 29 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-data-jpa</artifactId> 33 </dependency> 34 35 <dependency> 36 <groupId>mysql</groupId> 37 <artifactId>mysql-connector-java</artifactId> 38 </dependency> 39 40 <dependency> 41 <groupId>org.springframework.cloud</groupId> 42 <artifactId>spring-cloud-starter-feign</artifactId> 43 </dependency> 44 </dependencies> 45 46 <build> 47 <plugins> 48 <plugin> 49 <groupId>org.springframework.boot</groupId> 50 <artifactId>spring-boot-maven-plugin</artifactId> 51 </plugin> 52 </plugins> 53 </build> 54 55 </project>
建StudentClientService接口;
1 package com.javaqi.microservicecommon.service; 2 3 import com.javaqi.microservicecommon.entity.Student; 4 import org.springframework.cloud.netflix.feign.FeignClient; 5 import org.springframework.web.bind.annotation.GetMapping; 6 import org.springframework.web.bind.annotation.PathVariable; 7 import org.springframework.web.bind.annotation.PostMapping; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 10 import java.util.List; 11 12 /** 13 * Student Feign接口客户端 14 * @author Administrator 15 * 16 */ 17 @FeignClient(value="MICROSERVICE-STUDENT") 18 public interface StudentClientService { 19 20 /** 21 * 根据id查询学生信息 22 * @param id 23 * @return 24 */ 25 @GetMapping(value="/student/get/{id}") 26 public Student get(@PathVariable("id") Integer id); 27 28 /** 29 * 查询学生信息 30 * @return 31 */ 32 @GetMapping(value="/student/list") 33 public List<Student> list(); 34 35 /** 36 * 添加或者修改学生信息 37 * @param student 38 * @return 39 */ 40 @PostMapping(value="/student/save") 41 public boolean save(Student student); 42 43 /** 44 * 根据id删除学生信息 45 * @return 46 */ 47 @GetMapping(value="/student/delete/{id}") 48 public boolean delete(@PathVariable("id") Integer id); 49 50 @RequestMapping("/student/ribbon") 51 public String ribbon(); 52 }
2、新建一个Feign消费者项目;
参考microservice-student-consumer-80建一个microservice-student-consumer-feign-80
代码都复制一份,包括pom.xml
pom.xml
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 <parent> 6 <groupId>com.javaqi</groupId> 7 <artifactId>qimicroservice</artifactId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <artifactId>microservice-student-consumer-feign-80</artifactId> 11 12 <properties> 13 <java.version>1.8</java.version> 14 </properties> 15 <dependencies> 16 <dependency> 17 <groupId>com.javaqi</groupId> 18 <artifactId>microservice-common</artifactId> 19 </dependency> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-web</artifactId> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-test</artifactId> 27 <scope>test</scope> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-data-jpa</artifactId> 32 </dependency> 33 <dependency> 34 <groupId>mysql</groupId> 35 <artifactId>mysql-connector-java</artifactId> 36 </dependency> 37 <dependency> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-starter-tomcat</artifactId> 40 </dependency> 41 <!-- 修改后立即生效,热部署 --> 42 <dependency> 43 <groupId>org.springframework</groupId> 44 <artifactId>springloaded</artifactId> 45 </dependency> 46 <dependency> 47 <groupId>org.springframework.boot</groupId> 48 <artifactId>spring-boot-devtools</artifactId> 49 </dependency> 50 <dependency> 51 <groupId>com.javaqi</groupId> 52 <artifactId>microservice-common</artifactId> 53 <version>1.0-SNAPSHOT</version> 54 <scope>compile</scope> 55 </dependency> 56 57 <!--ribbon相关依赖--> 58 <dependency> 59 <groupId>org.springframework.cloud</groupId> 60 <artifactId>spring-cloud-starter-eureka</artifactId> 61 </dependency> 62 <dependency> 63 <groupId>org.springframework.cloud</groupId> 64 <artifactId>spring-cloud-starter-ribbon</artifactId> 65 </dependency> 66 <dependency> 67 <groupId>org.springframework.cloud</groupId> 68 <artifactId>spring-cloud-starter-config</artifactId> 69 </dependency> 70 71 <!--引入Feign依赖--> 72 <dependency> 73 <groupId>org.springframework.cloud</groupId> 74 <artifactId>spring-cloud-starter-feign</artifactId> 75 </dependency> 76 </dependencies> 77 <build> 78 <plugins> 79 <plugin> 80 <groupId>org.springframework.boot</groupId> 81 <artifactId>spring-boot-maven-plugin</artifactId> 82 </plugin> 83 </plugins> 84 </build> 85 </project>
SpringCloudConfig.java
1 package com.javaqi.microservicestudentconsumerfeign80.config; 2 3 import com.netflix.loadbalancer.IRule; 4 import com.netflix.loadbalancer.RetryRule; 5 import org.springframework.cloud.client.loadbalancer.LoadBalanced; 6 import org.springframework.context.annotation.Bean; 7 import org.springframework.context.annotation.Configuration; 8 import org.springframework.web.client.RestTemplate; 9 10 @Configuration 11 public class SpringCloudConfig { 12 13 @LoadBalanced // 引入ribbon负载均衡 14 @Bean 15 public RestTemplate getRestTemplate(){ 16 return new RestTemplate(); 17 } 18 19 /** 20 * 自定义调用规则(服务提供者掉线后不再调用,解决轮询问题) 21 * @return 22 */ 23 @Bean 24 public IRule myRule(){ 25 return new RetryRule(); 26 // return new RandomRule(); 27 } 28 }
yml文件配置
1 server: 2 port: 80 3 context-path: / 4 eureka: 5 client: 6 service-url: 7 defaultZone: http://eureka2001.javaqi.com:2001/eureka/,http://eureka2002.javaqi.com:2002/eureka/,http://eureka2003.javaqi.com:2003/eureka/ 8 register-with-eureka: false
3、修改启动类名称,和加注解
启动类名称改下,改成StudentConsumerFeignApplication_80,同时加个注解@EnableFeignClients
1 package com.javaqi.microservicestudentconsumerfeign80; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 6 import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; 7 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 8 import org.springframework.cloud.netflix.feign.EnableFeignClients; 9 10 @EnableEurekaClient 11 @EnableFeignClients(value = "com.javaqi.*.*") 12 @SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) 13 public class MicroserviceStudentConsumerFeign80Application { 14 15 public static void main(String[] args) { 16 SpringApplication.run(MicroserviceStudentConsumerFeign80Application.class, args); 17 } 18 19 }
StudentConsumerController.java
1 package com.javaqi.microservicestudentconsumerfeign80.controller; 2 3 import com.javaqi.microservicecommon.entity.Student; 4 import com.javaqi.microservicecommon.service.StudentClientService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.*; 7 import org.springframework.web.client.RestTemplate; 8 9 import java.util.List; 10 11 @RestController 12 @RequestMapping("/student") 13 public class StudentConsumerController { 14 15 @Autowired 16 private StudentClientService studentClientService; 17 18 @Autowired 19 private RestTemplate restTemplate; 20 21 @PostMapping(value = "/save") 22 private boolean save(Student student) { 23 return studentClientService.save(student); 24 } 25 26 @GetMapping(value = "/list") 27 public List<Student> list() { 28 return studentClientService.list(); 29 } 30 31 @GetMapping(value = "/get/{id}") 32 public Student get(@PathVariable("id") Integer id) { 33 return studentClientService.get(id); 34 } 35 36 @GetMapping(value = "/delete/{id}") 37 public boolean delete(@PathVariable("id") Integer id) { 38 try { 39 studentClientService.delete(id); 40 return true; 41 } catch (Exception e) { 42 return false; 43 } 44 } 45 46 @RequestMapping("/ribbon") 47 public String ribbon(){ 48 return studentClientService.ribbon(); 49 } 50 }
运行效果没问题,可以试着停到1001服务,当访问1001时它不会服务,重启1001可以继续服务