美文网首页
Feign简单记录

Feign简单记录

作者: dwwl | 来源:发表于2019-04-26 20:13 被阅读0次

    ribbon还没测试,还有配置文件中具体是什么也还没搞太清楚

    使用feign的注意事项:

    1. feignClient接口中方法的定义,
    2. feignClient返回值为复杂对象时,器类型必须有午餐构造函数,dubbo对返回的要求是必须实现序列化接口,

    简单Demo记录

    三个模块:

    • govern_center : 注册中心
    • eurekaClient : 服务提供模块
    • eureka_consumer : 服务消费模块

    govern_center

    pom.xml

    eureka-server这个依赖依赖了web-starter,并且在java9的环境下,需要额外引入jaxb-api

    <dependencies>
            <!-- 导入Eureka服务的依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
                <version>2.0.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>javax.xml.bind</groupId>
                <artifactId>jaxb-api</artifactId>
                <version>2.4.0-b180725.0427</version>
            </dependency>
        </dependencies>
    

    application.yml

    server:
      port: 50101 #服务端口
    spring:
      application:
        name: xc-govern-center #指定服务名
    eureka:
      client:
        registerWithEureka: false #服务注册,是否将自己注册到Eureka服务中
        fetchRegistry: false #服务发现,是否从Eureka中获取注册信息
        serviceUrl: #Eureka客户端与Eureka服务端的交互地址,高可用状态配置对方的地址,单机状态配置自己(如果不配置则默认本机8761端口)
          defaultZone: http://localhost:50101/eureka/
      server:
        enable-self-preservation: false #是否开启自我保护模式
        eviction-interval-timer-in-ms: 60000 #服务注册表清理间隔(单位毫秒,默认是60*1000)
    
    

    Application.java

    @EnableEurekaServer
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class,args);
        }
    }
    

    eurekaClient

    pom.xml

    eureka-client不含web-starter,需要额外引入

     <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
                <version>2.0.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.0.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>javax.xml.bind</groupId>
                <artifactId>jaxb-api</artifactId>
                <version>2.4.0-b180725.0427</version>
            </dependency>
        </dependencies>
    

    application.yml

    eureka:
      client:
        registerWithEureka: true #服务注册开关
        fetchRegistry: true #服务发现开关
        serviceUrl: #Eureka客户端与Eureka服务端进行交互的地址,多个中间用逗号分隔
          defaultZone: http://localhost:50101/eureka/
      instance:
        prefer-ip-address:  true  #将自己的ip地址注册到Eureka服务中
        ip-address: 127.0.0.1
        instance-id: myFeign:5678 #指定实例id
    server:
      port: 5678
    spring:
      application:
        name: myFeign
    

    Application.java

    @EnableDiscoveryClient
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class);
        }
    }
    

    FeignTest.java

    @RestController
    public class FeignTest {
    
        @GetMapping("/my/feign/{id}")
        public String myFeign(@PathVariable("id") String id) {
    
            return "this is a feign Test id:" + id;
        }
    }
    

    eureka_consumer

    pom.xml

    <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
                <version>2.0.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.0.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>javax.xml.bind</groupId>
                <artifactId>jaxb-api</artifactId>
                <version>2.3.0</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
                <version>2.0.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>com.netflix.feign</groupId>
                <artifactId>feign-okhttp</artifactId>
                <version>8.18.0</version>
            </dependency>
        </dependencies>
    

    application.yml

    eureka:
      client:
        registerWithEureka: true #服务注册开关
        fetchRegistry: true #服务发现开关
        serviceUrl: #Eureka客户端与Eureka服务端进行交互的地址,多个中间用逗号分隔
          defaultZone: http://localhost:50101/eureka/
      instance:
        prefer-ip-address:  true  #将自己的ip地址注册到Eureka服务中
        ip-address: 127.0.0.1
        instance-id: xc_huang:5679 #指定实例id
    server:
      port: 5679
    spring:
      application:
        name: xc_huang
    

    Appliaction.java

    @EnableFeignClients
    @EnableDiscoveryClient
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class);
        }
    }
    
    

    MyFeignConsumer.java

    @FeignClient(value = "MYFEIGN")
    public interface MyFeignConsumer {
        @GetMapping("/my/feign/{id}")
        String myFeign(@PathVariable("id") String id);
    }
    
    

    FeignController.java用于测试

    @RestController
    public class FeignController {
    
        @Autowired
        private MyFeifnConsumer consumer;
    
        @RequestMapping("/test/{id}")
        public String feignTest(@PathVariable("id") String id) {
            String s = consumer.myFeign(id);
            System.out.println(s);
            return s;
        }
    }
    

    结果:

    1556280533488.png

    相关文章

      网友评论

          本文标题:Feign简单记录

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