Spring Cloud Alibaba OpenFeign

application.xml

server:
  port: 8040
  #应用名称  (nacos 会将该名称当作服务名称)
spring:
  application:
    name: order-openfeign-service
  cloud:
    nacos:
#      server-addr: 127.0.0.1:8848
      server-addr: 192.168.133.128:8847  #集群 nginx 负载均衡访问 nacos
      discovery:
        username: nacos
        password: nacos
        namespace: public

 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloudalibaba</artifactId>
        <groupId>com.wsm.springcloud</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>order-openfeign</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
<!--            <version>2.5.5</version>-->
        </dependency>

        <!-- nacos 服务注册发现  -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!-- 添加 openfeign 依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>


    </dependencies>

</project>

 

 

 

 

package com.wsm.order;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
//@EnableDiscoveryClient 老版本要加,
@EnableFeignClients
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class,args);
    }

//    @Bean
//    @LoadBalanced //启用负载均衡
//    public RestTemplate restTemplate(RestTemplateBuilder builder){
//        RestTemplate restTemplate = builder.build();
//        return restTemplate;
//    }
}

 

 

 

package com.wsm.order.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 添加feign接口的方法
 * name 指定调用rest接口所对应的服务名
 * path 指定调用rest接口所在的StockController指定的@RequestMapping
 */
@FeignClient(name = "stock-service",path="/stock")
public interface StockFeignService {

    //声明需要调用的rest接口对应的方法
    @RequestMapping("/reduct")//与 StockController 中的reduct()方法的@RequestMapping一致
    public String reduct(); //与 StockController 中的reduct()方法对应
}

 

package com.wsm.order.controller;

import com.wsm.order.feign.StockFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.swing.*;

@RestController
@RequestMapping("/order")
public class OrderController {

//    @Autowired
//    RestTemplate restTemplate;
    @Autowired
    StockFeignService stockFeignService;

    @RequestMapping("/add")
    public String add(){
        System.out.println("aaaaaaaaaaaaa");
//        String msg = restTemplate.getForObject("http://localhost:8011/stock/reduct", String.class);
//        String msg = restTemplate.getForObject("http://stock-service/stock/reduct", String.class);
        String msg = stockFeignService.reduct();
        return "hello feign "+msg;
    }
}

 

 

 

 

posted @ 2021-11-07 12:19  残星  阅读(121)  评论(0编辑  收藏  举报