SpringCloud

【SpringCloud】使用Feign进行远程调用、实现断路器

2019-08-09  本文已影响0人  扮鬼之梦

一、启动注册中心Eureka

参考:https://www.jianshu.com/p/35f7e452a539

二、创建服务的提供者demo-member

1.目录结构

2.pom添加feign依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

3.启动类添加开启Feign的注解@EnableFeignClients

@SpringBootApplication
@MapperScan("com.thy.dao")
@EnableFeignClients  //启用Feign
public class DemoMemberApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoMemberApplication.class, args);
    }
}

4.yml配置,开启断路器

server:
  port: 8080
spring:
  application:
    name: demo-member #项目名
  datasource: 
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/activity?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2b8
    username: root
    password: 123456
mybatis:  
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.thy.pojo
eureka:
  client: 
    enabled: true #该客户端是否可用
    service-url: 
      defaultZone: http://localhost:8761/eureka #注册中心地址
    register-with-eureka: true #注册该服务,默认为true
    fetch-registry: true #获取服务列表,默认为true
feign:
  hystrix:
    enabled: true #是否开启断路器    

5.Controller中提供一个用户查询接口

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.thy.common.ReturnData;
import com.thy.service.UserService;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    
    
    @GetMapping("/query")
    public ReturnData query(Long userId) {
        return ReturnData.success(userService.selectById(userId));
    }
    
}

6.访问该接口

三、创建服务的消费者demo-product

1.目录结构

2.pom添加feign依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

3.启动类添加开启Feign的注解@EnableFeignClients

@SpringBootApplication
@MapperScan("com.thy.dao")
@EnableFeignClients  //启用Feign
public class DemoProductApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoProductApplication.class, args);
    }
}

4.yml配置,开启断路器

server: 
  port: 8090
spring:
  application:
    name: demo-product #项目名
  datasource: 
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/activity?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2b8
    username: root
    password: 123456
mybatis:  
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.thy.pojo
eureka:
  client:
    enabled: true #该客户端是否可用
    service-url: 
      defaultZone: http://localhost:8761/eureka #注册中心地址
    register-with-eureka: true #注册该服务,默认为true
    fetch-registry: true #获取服务列表,默认为true
feign:
  hystrix:
    enabled: true #是否开启断路器  

5.创建FeignClient接口UserClient

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.thy.common.BaseResponse;
import com.thy.feign.bean.User;
import com.thy.feign.fallback.UserFallback;

//不使用断路器,只需要填写name属性
//@FeignClient("demo-member")
//使用断路器,需要填写fallback属性
@FeignClient(name="demo-member",fallback=UserFallback.class)
public interface UserClient {
    @GetMapping("/user/query")
    BaseResponse<User> findMember(@RequestParam("userId") Long userId);
}

6.创建断路器UserFallback

import org.springframework.stereotype.Component;
import com.thy.common.BaseResponse;
import com.thy.feign.UserClient;
import com.thy.feign.bean.User;
@Component
public class UserFallback implements UserClient{

    @Override
    public BaseResponse<User> findMember(Long userId) {
        return new BaseResponse<User>("调用会员服务失败");
    }

}

7.在Service或者Controller中进行服务的调用

用法比较像Controller调用Service或Service调用Mapper,使用@Autowired自动装配UserClient ,然后就可以远程调用用户查询的接口

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.thy.common.BaseResponse;
import com.thy.feign.UserClient;
import com.thy.feign.bean.User;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private UserClient userClient;
    
    @GetMapping("/queryUser")
    public BaseResponse<User> queryUser(Long userId) {
        return userClient.findMember(userId);
    }
    

}

8.访问demo-product服务远程调用会员查询接口

返回结果同直接访问demo-member服务查询会员


9.若远程调用失败,则返回UserFallback中的结果。这里将demo-member服务关闭,模拟访问失败

10.若不需要断路器,可以将demo-product的配置文件中的断路器关闭

11.再次访问,则会抛出访问失败的异常

四、负载均衡

1.为了能看到负载均衡的效果,在demo-member中再添加一个接口,返回值中标记访问的是在哪个端口下的服务。启动一个demo-member服务。

    @GetMapping("/loadBalance")
    public ReturnData loadBalance() {
        return ReturnData.success("loadBalance:8080");
    }

2.修改demo-member的访问端口为8081,新增的接口返回信息也改为8081,再启动一个demo-member服务。


3.查看Eureka,可以看到启动了两个demo-member服务,分别运行在8080和8081端口上

4.使用同查询会员接口的的方法创建测试负载均衡的接口



5.通过demo-product远程调用demo-member的接口

多次发送请求会发现,demo-product服务会交替着远程调用8080端口和8081端口上的demo-member服务,实现了负载均衡。



五、git地址

https://gitee.com/gnliscream/eureka-feign-demo

上一篇下一篇

猜你喜欢

热点阅读