Spring Cloud Alibaba基础教程:Feign

搭建步骤:
1、新建工程 demo_feign_provider
maven 依赖:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>

新建控制器类:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author zhangyugu
 * @Date 2020/7/9 6:56 上午
 * @Version 1.0
 */
@RestController
public class TestController {

    @GetMapping("/hello")
    public String hello(@RequestParam("name") String name){
        return "hello " + name;
    }
}

新建启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @Author zhangyugu
 * @Date 2020/7/9 6:54 上午
 * @Version 1.0
 */
@SpringBootApplication
@EnableDiscoveryClient
//@EnableFeignClients
public class FeignProviderApplication {

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

application.yml 配置:

server:
  port: 8854

spring:
  application:
    name: demo-feign-provider
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

2、新建工程 demo_feign_consumer
maven依赖:

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

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

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
    </dependencies>

新建feign接口及控制器实现类:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author zhangyugu
 * @Date 2020/7/7 7:03 下午
 * @Version 1.0
 */
@RestController
public class TestController {

    @Autowired
    Client client;
    
    @GetMapping("/hello")
    @ResponseBody
    public String hello(@RequestParam("name") String name) {
        return client.hello(name);
    }
}

@FeignClient("demo-feign-provider")
interface Client {

    @GetMapping("hello")
    String hello(@RequestParam(name = "name") String name);
}

新建启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @Author zhangyugu
 * @Date 2020/7/7 6:53 下午
 * @Version 1.0
 */
@SpringBootApplication
@EnableFeignClients
public class SwaggerApplication {

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

新建配置文件application.yml:

server:
  port: 8823

spring:
  application:
    name: demo-swagger
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

3、在已经启动nacos的前提下,先后启动 demo_feign_provider、demo_feign_consumer
测试:http://localhost:8823/hello?name=guzy
返回 hello guzy

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值