第三篇: 服务消费者(Feign)(Greenwich版本)

一、在前面的工程基础上,增加一个service-feign的子模块。

第三篇: 服务消费者(Feign)(Greenwich版本)_第1张图片

 

修改父工程的pom.xml 把service-feign添加到它的子模块里面去


    
        eureka-server
        eureka-client
        service-ribbon
        service-feign
    

service-feign的pom.xml内容如下:



    4.0.0

    service-feign
    0.0.1-SNAPSHOT
    jar

    service-feign
    Demo project for Spring Boot

    
        com.example
        chapter2
        0.0.1-SNAPSHOT
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            
        
    



application.properties配置如下:

eureka.client.service-url.defaultZone=http://localhost:8001/eureka/
spring.application.name=service-feign
server.port=8005
ServiceFeignApplication配置如下:
package com.example;

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

/**
 * @author xiaobu @EnableFeignClients 开启feign功能
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableEurekaClient
@EnableFeignClients
public class ServiceFeignApplication {

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

feign目录结构如下:

第三篇: 服务消费者(Feign)(Greenwich版本)_第2张图片

定义一个feign接口

package com.example.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author xiaobu
 * @version JDK1.8.0_171
 * @date on  2018/11/6 14:24
 * @description V1.0 定义个feign接口 @FeignClient("服务名") 来确定调哪个服务
 */
@FeignClient("eureka-client")
public interface FeignService {
    /***
     * @author xiaobu
     * @date 2018/11/6 14:34
     * @param name 名字
     * @return java.lang.String
     * @descprition value为test则是调用 eureka-client的test的方法
     * RequestMapping(value="/test",method = RequestMethod.GET)与GetMapping(value="/test")等价
     * RequestParam.value() was empty on parameter 0 第一个参数不能为空
     * @version 1.0
     */
    //@RequestMapping(value="/test",method = RequestMethod.GET)
    @GetMapping(value="/test")
    String testFromClient(@RequestParam(value = "name") String name);
}

 

定义一个controller

package com.example.controller;

import com.example.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xiaobu
 * @version JDK1.8.0_171
 * @date on  2018/11/6 14:22
 * @description V1.0 调用feignService的test方法
 */
@RestController
public class FeginController {
    @Autowired
    FeignService feignService;


    @GetMapping("/test")
    public String test(@RequestParam String name){
        return feignService.testFromClient(name);
    }
}

输入http://localhost:8005/test?name=admin结果如下:

你可能感兴趣的:(SpringCloud)