zoukankan      html  css  js  c++  java
  • eureka和feign的使用

      1 eureka和feign的简介(copy来的)

      eureka:Eureka是Netflix开发的服务发现组件,本身是一个基于REST的服务。Spring Cloud将它集成在其子项目spring-cloud-netflix中,以实现Spring Cloud的服务发现功能。

      feign:Feign是一个声明式的Web服务客户端,使用Feign可使得Web服务客户端的写入更加方便。 它具有可插拔注释支持,包括Feign注解和JAX-RS注解、Feign还支持可插拔     编码器和解码器、Spring Cloud增加了对Spring MVC注释的支持,并HttpMessageConverters在Spring Web中使用了默认使用的相同方式。Spring Cloud集成了Ribbon        和Eureka,在使用Feign时提供负载平衡的http客户端。

    2 eureka的服务端

      2.1 pom.xml中导入相应的依赖

    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 
    </dependency>

      2.2 在配置文件中编码相关配置

    #指定端口
    server:
      port: 7001
    #创建eureka实例
    eureka:
      instance:
        hostname: localhost
      client:
        #不允许服务端向自己注册
        register-with-eureka: false
        fetch-registry: false
        #客户端注册时需要使用的地址
        service-url:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

      2.3 在主类上添加相关注解

    3 服务提供者(feign)

      3.1 pom.xml中导入相关依赖

           <!--eureka的客户端依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
              <!--feign的依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>        

      3.2 在配置文件中编码相关配置

    #配置端口
    server:
      port: 7003
    #注册到eureka客户端的名字
    spring:
      application:
        name: feign-client
    #eureka相关实例
    eureka:
      instance:
        lease-expiration-duration-in-seconds: 30
        lease-renewal-interval-in-seconds: 10
        #在eureka中显示ip地址
        prefer-ip-address: true
        ip-address: 127.0.0.1
      client:
        registerWithEureka: true
        fetchRegistry: true
        #eureka服务端的地址
        serviceUrl:
          defaultZone: http://localhost:7001/eureka/

      3.3 在主类上添加相关依赖

      

      3.4 需要被服务消费者调用的方法(例)

      

    @RestController
    public class UserController {
        @GetMapping("/user")
        public String user(@PathVariable("id") Integer id){
            return "测试成功啦啦啦啦啦啦"+id;
        }
    }

    4 服务消费者(feign)

      4.1 在pom.xml 中添加相关依赖

           <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>    

      4.2 在配置文件中编码相关配置

    server:
      port: 7002
    ribbon:
      ReadTimeout: 60000
      ConnectTimeout: 60000
    eureka:
      instance:
        lease-expiration-duration-in-seconds: 30
        lease-renewal-interval-in-seconds: 10
        prefer-ip-address: true
        ip-address: 127.0.0.1
      client:
        registerWithEureka: true
        fetchRegistry: true
        serviceUrl:
          defaultZone: http://localhost:7001/eureka/
    spring:
      application:
        name: eureka_client

      4.3 在主类上添加相关注解

      4.4 服务消费者的feign端(即为一个接口,通过此接口与另一个服务建立连接)

    @Component
    @FeignClient(value = "FEIGN-CLIENT",name = "FEIGN-CLIENT")
    public interface Feign {
    
        @RequestMapping(value = "/user",method = RequestMethod.GET)
        public String user(@PathVariable("id") Integer id);
    
    }

      4.5 服务消费端调用此接口的方法

    @RestController
    public class UserController {
        @Autowired
        private Feign feign;
    
        @GetMapping("/user/{id}")
        public String user(@PathVariable("id") Integer id){
            return feign.user(id);
        }
    }
  • 相关阅读:
    centos7使用163 yum源
    Package has no installation candidate解决方法
    HTML 5 canvas相关
    git rebase 使用总结
    Redis不是只有get set那么简单
    k8s---pod常用操作
    Navigator.sendBeacon 无阻塞发送统计数据
    如何在页面关闭或跳转时优雅的发送Ajax请求
    mysql的触发器、视图、索引,受益匪浅
    杂集:centos7中利用logrotate工具切割tomcat日志
  • 原文地址:https://www.cnblogs.com/cl-rr/p/9644815.html
Copyright © 2011-2022 走看看