【SpringCloud】服务调用-Feign(五)

文章目录

  • 前言
  • Note
  • Core-Code(SERVICE-PROVIDER)
    • maven
    • Code
  • Core-code(SERVICE-CONSUMER)
  • GitHub
  • Author

前言

  1. springcloud提供多种方式服务间的调用。restTemplate / loadBalance / feign 等
  2. 比较推荐使用feign
  3. 每个service应当提供一个api的包出去让其他服务能调用

Note

Core-Code(SERVICE-PROVIDER)

maven


    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.6.RELEASEversion>
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        
        <springcloud.version>Greenwich.RELEASEspringcloud.version>
    properties>
<dependencyManagement>
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-dependenciesartifactId>
            <version>${springcloud.version}version>
            <type>pomtype>
            <scope>importscope>
        dependency>
    dependencies>
dependencyManagement>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>

Code

/**
 * ProductAPI
 * 

* Description: *

* Creation Time: 2018/11/28 23:20. * * @author Hu Weihui */ @FeignClient(name = "product-server",path = "/product",fallback = ProductFeignApi.ProductFeignApiFallback.class )//如果产生服务降级就返回NULL public interface ProductFeignApi { /** * fegin获取商品信息. * * @param id the id * @return the mapper * @author HuWeihui * @since hui_project v1 */ @GetMapping("/product/{id}") ResultVO<ProductDTO> get(@PathVariable("id") String id); @PutMapping("/products") ResultVO add(@RequestBody ProductDTO productDTO); @PutMapping("/lcn/tcc/products") ResultVO tccAdd(@RequestBody ProductDTO productDTO); @PutMapping("/lcn/txc/products") ResultVO txcAdd(@RequestBody ProductDTO productDTO); @Component static class ProductFeignApiFallback implements ProductFeignApi{ /** * fegin获取商品信息. * * @param id the id * @return the mapper * @author HuWeihui * @since hui_project v1 */ @Override public ResultVO<ProductDTO> get(String id) { return null; } @Override public ResultVO add(ProductDTO productDTO) { return null; } @Override public ResultVO tccAdd(ProductDTO productDTO) { DTXUserControls.rollbackGroup(TracingContext.tracing().groupId()); return null; } @Override public ResultVO txcAdd(ProductDTO productDTO) { return null; } } }

Core-code(SERVICE-CONSUMER)


       <dependency>
            <groupId>com.hui.base.springcloudgroupId>
            <artifactId>hui-base-springcloud-product-apiartifactId>
            <version>${project.version}version>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>
    @Autowired
    private ProductFeignApi productFeignApi;


    /**
     * fegin获取.
     *
     * @param id the id
     * @return the response entity
     * @author Hu weihui
     * @since hui_project 1.0.0
     */
    @GetMapping("/order/product4/{id}")
    public ResultVO<ProductDTO> getProduct4(@PathVariable("id") String id) {
        ProductDTO data = productFeignApi.get(id).getData();
        log.info(data.toString());
        return ResultMapper.ok(data);
    }
    

GitHub

不同版本对应不同分支

springcloud-greenwich采用了consul-config,不需要下方的hui-base-springcloud-config-repo

【SpringCloud】完整工程 https://github.com/ithuhui/hui-base-springcloud

【Config-Server】配置中心配置文件:https://github.com/ithuhui/hui-base-springcloud-config-repo

Author

 作者:HuHui
 转载:欢迎一起讨论web和大数据问题,转载请注明作者和原文链接,感谢

你可能感兴趣的:(JavaWeb)