zoukankan      html  css  js  c++  java
  • 负载均衡二Feign

    Feign实现了Ribbon不用拼接url通过调用接口直接调用方法传入参数一样的,可以通过官方文档自己学习

    复制一份order

    修改不要的只留一个主配置类,application.yml

    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:1000/eureka/#注册中心地址
      instance:
        ip-address: true #使用ip配置
        instance-id: order-server2 #指定服务的id
    server:
      port: 3001
    spring:
      application:
        name: order-server2

    这里不是做集群所以我们需要更改

    spring:
      application:
        name: order-server2

    1.导包

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

    3.需要在主配置类上打入标签

    package cn.jiedada;
    
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients(basePackages = "cn.jiedada.Feign")
    public class EurekaOrderService3001 {
        public static void main(String[] args) {
            new SpringApplicationBuilder(EurekaOrderService3001.class).web(true).run(args);
        }
    
    }

    4.自己写一个接口打入标签

    package cn.jiedada.Feign;
    
    import cn.jiedada.domain.User;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    //添加集群application的名称
    @FeignClient("USER-SERVER")
    public interface UserFeginClient {
        //添加路劲
        @RequestMapping("/provider/user/{id}")
        public User getUserById(@PathVariable("id")Long id);
    }

     4.Controller

    package cn.jiedada.web.controller;
    
    import cn.jiedada.Feign.UserFeginClient;
    import cn.jiedada.domain.User;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    @RequestMapping("/customer")
    public class OrderController {
        @Autowired
        private UserFeginClient userFeginClient;
    
        @RequestMapping("/user/{id}")
        public User getUserById(@PathVariable("id")Long id){
            return userFeginClient.getUserById(id);
        }
    }
  • 相关阅读:
    Android 编译命令 make j8 2>&1 | tee build.log 解释
    Linux时间函数之gettimeofday()函数之使用方法
    转:RSA算法原理说明
    转: 各个密码算法的实现(未验证)
    转:修改Android签名证书keystore的密码、别名alias以及别名密码
    转:Eclipse ADT的Custom debug keystore所需证书规格
    转:如何转换Android打包用jks格式keystore证书为Air用pkcs12格式p12证书
    转: 关于流量控制与令牌桶介绍
    转:sublime2 官方网址
    转:Mac OS X下Sublime Text (V2.0.1)破解
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/11926636.html
Copyright © 2011-2022 走看看