• 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.测试。

  • 相关阅读:
    自我介绍
    java web 学习计划
    团队-团队编程项目中国象棋-代码设计规范
    团队-中国象棋游戏-设计文档
    团队-象棋游戏-开发环境搭建过程
    结对-贪吃蛇游戏-开发环境搭建过程
    结对-结对编项目贪吃蛇-设计文档
    20170912-构建之法:现代软件工程-阅读笔记
    课后作业-阅读任务-阅读提问-1
    团队-团队编程项目中国象棋-成员简介及分工
  • 原文地址:https://www.cnblogs.com/ibcdwx/p/14427708.html
Copyright © 2020-2023  润新知