springcloud的fegin简单实例

第一步:修改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.0modelVersion>

    <groupId>com.ck.springcloudgroupId>
    <artifactId>feign-consumerartifactId>
    <version>1.0-SNAPSHOTversion>


    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.3.7.RELEASEversion>
        <relativePath/>
    parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-feignartifactId>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Brixton.SR5version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>

    dependencyManagement>


project>

第二步:resources目录下增加配置文件appilcation.yml了

spring:
  application:
    name: feign-consumer
eureka:
  client:
    serviceUrl:
        defaultZone: http://localhost:1111/eureka/
server:
  port: 9001


第三步:增加启动类:

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

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {

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

第四步:编写调用服务提供者的service

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.*;
//value是eureka服务注册中心上服务提供者的服务名称,HelloServiceFallback是服务降级的处理类,可以去了
@FeignClient(value = "HELLO-SERVICE", fallback = HelloServiceFallback.class)
public interface HelloService {

    @RequestMapping("/hello")
    String hello();

//RequestParamRequestHeader注解的名字必须写
    @RequestMapping(value = "/hello1", method = RequestMethod.GET )
     String hello(@RequestParam("name") String name);

    @RequestMapping(value = "/hello2", method = RequestMethod.GET )
     User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age);

    @RequestMapping(value = "/hello3", method = RequestMethod.POST)
     String hello(@RequestBody User user);
}

第五步:编写测试controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConsumerController {

    @Autowired
    HelloService helloService;

    @RequestMapping(value = "feign-consumer2", method = RequestMethod.GET)
    public String helloConsumer2(){
        StringBuffer  sb = new StringBuffer();
        sb.append(helloService.hello()).append("\n");
        sb .append(helloService.hello("DIDI")).append("\n");
        sb .append(helloService.hello("DIDI",20)).append("\n");
        sb .append(helloService.hello(new User("DIDI",20)));
        return  sb.toString();
    }
}

你可能感兴趣的:(spring)