美文网首页
4.Feign改造项目结构

4.Feign改造项目结构

作者: 溅十三 | 来源:发表于2020-05-17 13:10 被阅读0次
    image.png

    1.创建公共接口层

    • 使用了@FeignClient("feign-client")注解,那么下游也要用到feign组件
    • 如果怕影响下游,那么直接提供接口即可,不使用@FeignClient注解和 @PostMapping;一般会打两个2包,一个支持springCloud(FeignClient),另一个直接使用接口
      pom:
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>spring-cloud-demo</artifactId>
            <groupId>org.example</groupId>
            <version>1.0-SNAPSHOT</version>
            <relativePath>../../pom.xml</relativePath>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>feign-client-intf</artifactId>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
        </dependencies>
    
    </project>
    

    IService :

    package com.imooc.springcloud;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestParam;
    
    /**
     * Created by 半仙.
     */
    @FeignClient("feign-client")
    public interface IService {
    
        @GetMapping("/sayHi")
        public String sayHi();
    
        @PostMapping("/sayHi")
        public Friend sayHiPost(@RequestBody Friend friend);
    
        @GetMapping("/retry")
        public String retry(@RequestParam(name = "timeout") int timeout);
    
        @GetMapping("/error")
        public String error();
    }
    

    Friend:

    package com.imooc.springcloud;
    
    import lombok.Data;
    
    /**
     * Created by 半仙.
     */
    @Data
    public class Friend {
    
        private String name;
    
        private String port;
    
    }
    

    Product:

    package com.imooc.springcloud;
    
    import lombok.Builder;
    import lombok.Data;
    
    /**
     * Created by 半仙.
     */
    @Data
    @Builder
    public class Product {
    
        private Long productId;
    
        private String description;
    
        private Long stock;
    
    }
    

    2.创建服务提供者feign-client

    pom:
    去除了<artifactId>spring-cloud-starter-openfeign</artifactId>的引用?

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>spring-cloud-demo</artifactId>
            <groupId>org.example</groupId>
            <version>1.0-SNAPSHOT</version>
            <relativePath>../../pom.xml</relativePath>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>feign-client</artifactId>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
            <dependency>
                <groupId>com.imooc</groupId>
                <artifactId>feign-client-intf</artifactId>
                <version>${project.version}</version>
            </dependency>
    
        </dependencies>
    
    </project>
    

    FeignClientApplication启动类:
    去除了feign的注解?client不需要调用feign接口

    package com.imooc.springcloud;
    
    import org.springframework.boot.WebApplicationType;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    /**
     * Created by 半仙.
     */
    @EnableDiscoveryClient
    @SpringBootApplication
    public class FeignClientApplication {
    
        public static void main(String[] args) {
            new SpringApplicationBuilder(FeignClientApplication.class)
                    .web(WebApplicationType.SERVLET)
                    .run(args);
        }
    
    }
    
    

    sonacheck的代码重复率检查?
    Controller类:

    • 需要implements IService
    package com.imooc.springcloud;
    
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.logging.Log;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * Created by 半仙.
     */
    @RestController
    @Slf4j
    public class Controller implements IService {
    
        @Value("${server.port}")
        private String port;
    
    
        @GetMapping("/sayHi2")
        public String sayHi2() {
            return "This is " + port;
        }
    
        @Override
        public String sayHi() {
            return "This is " + port;
        }
    
        @Override
        public Friend sayHiPost(@RequestBody Friend friend) {
            log.info("You are " + friend.getName());
            friend.setPort(port);
            return friend;
        }
    
        @Override
        public String retry(@RequestParam(name = "timeout") int timeout) {
            while (--timeout >= 0) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }
            log.info("retry " + port);
            return port;
        }
    
        @Override
        public String error() {
            throw new RuntimeException("black sheep");
        }
    }
    

    3.创建消费者

    如果自己pom引用的包,和下游的冲突了怎么办?使用,tomcat禁用的方式重新引入
    pom:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>spring-cloud-demo</artifactId>
            <groupId>org.example</groupId>
            <version>1.0-SNAPSHOT</version>
            <relativePath>../../pom.xml</relativePath>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>feign-consumer-advanced</artifactId>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
            <dependency>
                <groupId>com.imooc</groupId>
                <artifactId>feign-client-intf</artifactId>
                <version>1.0.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    
    </project>
    

    FeingConsumerApp启动类:
    Feign默认的扫包路径?当main方法的扫包路径
    接口处层是另外一个公司提供的怎么办?2种方法
    1.@EnableFeignClients配置上扫包路径
    2.添加注解方式(推荐,更为灵活)


    image.png

    最新的G版不被允许,会有2个FeignClient上下文,使用配置文件的方式处理
    spring.main.allow-bean-definition-overriding=true

    package com.imooc.springcloud;
    
    import org.springframework.boot.WebApplicationType;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    import org.springframework.cloud.openfeign.FeignClient;
    
    /**
     * Created by 半仙.
     */
    @EnableDiscoveryClient
    @SpringBootApplication
    @EnableFeignClients
    public class FeingConsumerApp {
    
        public static void main(String[] args) {
            new SpringApplicationBuilder(FeingConsumerApp.class)
                    .web(WebApplicationType.SERVLET)
                    .run(args);
        }
    
    }
    

    Controller类:

    package com.imooc.springcloud;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * Created by 半仙.
     */
    @RestController
    @Slf4j
    public class Controller {
    
        @Autowired
        private IService service;
    
        @GetMapping("/sayHi")
        public String sayHi() {
            return service.sayHi();
        }
    
    //    @PostMapping("/sayHi")
    //    public Friend sayHi2() {
    //        Friend friend = new Friend();
    //        friend.setName("test");
    //        return service.sayHiPost(friend);
    //    }
    //
    //    @GetMapping("/retry")
    //    public String retry(Integer timeout) {
    //        return service.retry(timeout);
    //    }
    
    }
    

    相关文章

      网友评论

          本文标题:4.Feign改造项目结构

          本文链接:https://www.haomeiwen.com/subject/ipzjohtx.html