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);
    }

}

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

发布了31 篇原创文章 · 获赞 15 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/dongdingzhuo/article/details/96997857
今日推荐