您现在的位置是:首页 > 学无止境 > Spring Cloud

spring cloud alibaba(4)基础-创建消费者(feign)

贾继涛 2019-03-11 2090人围观

相关文章

 spring cloud alibaba(1)基础注册中心nacos

 spring cloud alibaba(2)基础-服务提供者

 spring cloud alibaba(3)基础-创建消费者

 spring cloud alibaba(4)基础-创建消费者(feign)

 spring cloud alibaba(5)基础-熔断器防止雪崩sentinel

 spring cloud alibaba(6)基础-Sentinel 控制台搭建

 spring cloud alibaba(7)基础-Spring Cloud Gateway网关

 spring cloud alibaba(8)基础-geteway路由全局过滤

 spring cloud alibaba(9)基础-nacos云配置服务-nacos-config

 spring cloud alibaba(10)基础-Nacos Config多环境配置

项目结构结构

 png

  1. 创建文件夹hello-spring-cloud-alibaba-nacos-consumer-feign 及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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.jjt</groupId>
        <artifactId>hello-spring-cloud-alibaba-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../hello-spring-cloud-alibaba-dependencies/pom.xml</relativePath>
    </parent>
    <artifactId>hello-spring-cloud-alibaba-nacos-consumer-feign</artifactId>
    <packaging>jar</packaging>
    <name>hello-spring-cloud-alibaba-nacos-consumer-feign</name>
    <url>http://www.jiajitao.cn</url>
    <inceptionYear>2018-Now</inceptionYear>
    <dependencies>
        <!-- Spring Boot Begin -->
        <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-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot End -->
        <!-- Spring Cloud Begin -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- Spring Cloud End -->
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.jjt.hello.spring.cloud.alibaba.nacos.consumer.feign.NacosConsumerFeignApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2.springbootApplication

package com.jjt.hello.spring.cloud.alibaba.nacos.consumer.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosConsumerFeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerFeignApplication.class,args);
    }
}


3.service

package com.jjt.hello.spring.cloud.alibaba.nacos.consumer.feign.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(value = "nacos-provider")
public interface HelloService {
    @GetMapping("/h/{msg}")
    public String hello(@PathVariable("msg") String msg);
}

4.controller

package com.jjt.hello.spring.cloud.alibaba.nacos.consumer.feign.controller;
import com.jjt.hello.spring.cloud.alibaba.nacos.consumer.feign.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class NacosConsumerFeignController {
    @Autowired
    private HelloService helloService;
    //http://127.0.0.1:9092/c/hi
    @GetMapping("/c/hi")
    public String hell(){
       return helloService.hello(":我是feign消费者调用的");
    }
}


5.application.yml

spring:
  application:
    name: nacos-consumer-feign
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.102.130:8848
server:
  port: 9092
management:
  endpoints:
    web:
      exposure:
        include: "*"

源码地址

 https://gitee.com/jiajitao/Spring-Cloud-Ali


您的支持