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

  • 相关阅读:
    IntelliJ Idea 常用快捷键列表
    IPv6地址存储
    一文看懂java的IO流
    AchartEngine的柱状图属性设置
    绘制图表改变其大小
    在Android上用AChartEngine轻松绘制图表
    Android 图表绘制 achartengine 示例解析
    封装一个类搞定90%安卓客户端与服务器端交互
    安卓图表引擎AChartEngine(三)
    AchartEngine 的学习
  • 原文地址:https://www.cnblogs.com/ibcdwx/p/14427708.html
Copyright © 2020-2023  润新知