zoukankan      html  css  js  c++  java
  • springcloud-OpenFeign服务调用

      1.创建模块

      2.引入依赖

    <dependencies>
            <dependency>
                <groupId>cn.aib.springcloud</groupId>
                <artifactId>springclud-api-common</artifactId>
                <version>1.0-SNAPSHOT</version>
            </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>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!--   引入eureka客户端     -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <!--  open feign      -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
    
        </dependencies>

      3.改配置

    server:
      port: 80
    
    
    eureka:
      client:
        register-with-eureka: false   #是否将自己注册到注册中心,集群必须设置为true配合ribbon
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka

      由于Feign是webService客户端,不需要到注册中心上注册。但如果是直接的Ribbon+Eureka的话,调用方是需要到注册中心注册的。

      4.主启动

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

      记得开启Feign

      5.写业务

    @Component
    @FeignClient("cloud-payment-service")
    public interface PaymentFeignService {
    
        @GetMapping("/payment/get/{id}")
        public CommonResult selectPayment(@PathVariable("id") Long id);
    }
    @RestController
    public class FeignController {
        @Resource
        private PaymentFeignService paymentFeignService;
    
        @GetMapping(value = "/consumer/payment/get/{id}")
        public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
    
            return paymentFeignService.selectPayment(id);
        }
    
    }

      6.测试。

  • 相关阅读:
    datatable删除一行方法
    jquery绑定事件的坑,重复绑定问题
    jquery表单重置
    koa中上传文件到阿里云oss实现点击在线预览和下载
    koa2使用阿里云oss的nodejs sdk实现上传图片
    input元素默认选中设置
    koa使用koa-passport实现路由进入前登录验证
    jquery获取select多选框选中的值
    jquery方法.serializeArray()获取name和value并转为json数组
    jquery获取表单数据方法$.serializeArray()获取不到disabled的值
  • 原文地址:https://www.cnblogs.com/ibcdwx/p/14427708.html
Copyright © 2011-2022 走看看