SpringCloud系列教程记录06-客户端

1.客户端简介

客户端主要是通过Gateway调用前面的那些微服务,对接前端项目,中间主要涉及到Hystrix熔断,Feign负载均衡,Eureka客户端,详细代码参考我的码云项目:https://gitee.com/dongdingzhuo/cloud.git
前端项目:https://gitee.com/dongdingzhuo/api-test.git

2.maven需要的包

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
            <version>3.8.0</version>
        </dependency>

        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>3.8.0</version>
        </dependency>


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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

3.配置文件(application.yml)

server:
  port: 9999
spring:
  application:
    name: client
  profiles:
    active: dev

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds:5000
        timeout:
          enabled: true
feign:
  hystrix:
    enabled: true

4.配置文件(application-dev.yml)

eureka:
  instance:
    prefer-ip-address: true
    instance-id: auth
  client:
    service-url:
      defaultZone: http://localhost:1000/eureka/
gateway:
  url: http://localhost:2000

5.注解(@EnableEurekaClient,@EnableFeignClients,@EnableHystrix)

在这里插入图片描述

6.文件服务层(FileService)

@FeignClient(name = "service-gateway-file", url = "${gateway.url}", path = "/service-file", fallback = FileFallback.class, configuration = FileService.FileConfig.class)
public interface FileService {

    /**
     * 文件上传
     *
     * @param map
     * @param file
     * @return
     */
    @PostMapping(value = "/file/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    Object fileUpload(@RequestParam Map<String, Object> map, @RequestPart("file") MultipartFile file);

    /**
     * 文件分页列表
     *
     * @param map
     * @return
     */
    @PostMapping("/file/page")
    Object filePage(@RequestParam Map<String, Object> map);


    class FileConfig {
        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder();
        }
    }
}

7.文件服务熔断

@Service
public class FileFallback implements FileService {
    /**
     * 文件上传
     *
     * @param map
     * @param file
     * @return
     */
    @Override
    public Object fileUpload(Map<String, Object> map, MultipartFile file) {
        return AjaxResult.failResult("文件上传失败,服务忙...请稍后重试!");
    }

    /**
     * 文件分页列表
     *
     * @param map
     * @return
     */
    @Override
    public Object filePage(Map<String, Object> map) {
        return AjaxResult.failResult("获取文件列表失败,服务忙...请稍后重试!");
    }
}

8.文件服务调用

@RestController
public class FileController {

    @Autowired
    FileService service;

    /**
     * 上传文件
     *
     * @param map
     * @param file
     * @return
     */
    @PostMapping(value = "/fileUpload")
    public Object fileUpload(@RequestParam Map<String, Object> map, @RequestParam("file") MultipartFile file) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        String system = MapUtil.getString(map, "system");
        if (system == null) {
            return AjaxResult.failResult("系统代码不能为空");
        }
        String type = MapUtil.getString(map, "type");
        if (type == null) {
            return AjaxResult.failResult("文件类型不能为空");
        }
        return service.fileUpload(map, file);
    }

    /**
     * 文件信息分页列表
     *
     * @param map
     * @return
     */
    @PostMapping("/filePage")
    public Object filePage(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        return service.filePage(map);
    }

    public static void main(String[] args) {
        MapPro mapPro = new MapPro();
        mapPro.put("a",1111).put("b","abc222");
        HashMap map = mapPro.getMap();
        System.out.println(map);
    }

}

以上我以文件服务为例,其他微的调用类似,详细代码参考我码云项目源码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值