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

Spring cloud入门基础(4)客户端Feign实现

贾继涛 2019-01-22 2207人围观


1.首先看下我整理的项目目录结构如下

 png

2. 创建File->New->Directory创建文件夹hello-spring-cl-sweb-feign目录结构类似如下

png

3.pom.xml文件

<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.cloud</groupId>
        <artifactId>hello-spring-cl-den</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../hello-spring-cl-den/pom.xml</relativePath>
    </parent>
    <artifactId>hello-spring-cl-sweb-feign</artifactId>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--eureka server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <!--<artifactId>spring-cloud-starter-eureka</artifactId>-->
        </dependency>
        <!-- 引入feign 依赖 ,用来实现负载均衡,我们这里只是使用,先不作其他介绍-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>
   <build>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
                 <configuration>
                     <mainClass>com.jjt.hello.hello.SwebFeignApplication</mainClass>
                 </configuration>
             </plugin>
         </plugins>
     </build>
</project>

4.启动main



package com.jjt.hello.hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableEurekaClient
//@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class SwebFeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(SwebFeignApplication.class, args);
    }
}


5.service



package com.jjt.hello.hello.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "hello-spring-cl-service")
public interface HelloService {
    @RequestMapping(method = RequestMethod.GET, value = "hi")
    public String sayHi(@RequestParam(value = "msg") String msg);
}


6.controller


package com.jjt.hello.hello.controller;


import com.jjt.hello.hello.service.HelloService;

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.RequestParam;

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


@RestController

public class ConsumerFeignController {


    @Autowired

    private HelloService helloService;


    @RequestMapping(value = "/hello-consumer",method = RequestMethod.GET)

    public String helloConsumer(@RequestParam String msg) {

        return helloService.sayHi(msg);

    }

}



7.配置application.yml

spring:
  application:
        name: hello-spring-cl-sweb-feign
server:
    port: 8765
eureka:
    client:
        serviceUrl:
            defaultZone: http://127.0.0.1:8761/eureka/

8.启动通客户端Ribbon实现 访问体质 http://127.0.0.1:8765/hello-consumer?msg=11111111111111


我的代码公开在码云 其中包括pom中 父依赖



https://gitee.com/jiajitao/springCloudFirst



您的支持