微服务笔记之Spring Cloud 中使用Feign调用接口服务(Finchley)

此篇和上篇只是spring-boot和spring-cloud的版本不同,所带来的配置及代码变化,项目配置及代码参考如下。

创建maven项目servicefeign_f;

项目目录结构:

微服务笔记之Spring Cloud 中使用Feign调用接口服务(Finchley)_第1张图片

1. pom.xml文件配置:




    4.0.0
    com.springcloud.lcl.fetgn
    servicefeign_f
    jar
    1.0-SNAPSHOT
    servicefeign_f
    http://www.myorganization.org

    
        servicefeign_f
    

    
        UTF-8
        1.7
        1.7
        Finchley.RELEASE
    

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.3.RELEASE
        
    

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

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

        
    


 2. 代码示例  

 项目主启动类:

package com.springcloud.lcl;

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

/**
 * @author lcl
 * @version 0.1
 * @date 2018/11/8
 **/
@SpringBootApplication
@EnableFeignClients
public class ServiceFeignFinchleyApplication {

    public static void main(String[] args){

        SpringApplication.run(ServiceFeignFinchleyApplication.class,args);
    }

}
服务接口 IFeignService代码:
package com.springcloud.lcl.feign.service;

import com.springcloud.lcl.feign.service.impl.FeignServiceImpl;
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 = "service-hi",fallback = FeignServiceImpl.class)
public interface IFeignService {

    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    String getHiService(@RequestParam(value = "name") String name);
}

服务接口实现类FeignServiceImpl:

package com.springcloud.lcl.feign.service.impl;

import com.springcloud.lcl.feign.service.IFeignService;
import org.springframework.stereotype.Component;

/**
 * @author lcl
 * @version 0.1
 * @date 2018/11/8
 **/
@Component
public class FeignServiceImpl implements IFeignService {
    @Override
    public String getHiService(String name) {
        return "Hii "+name+": Sorry,Error...";
    }
}

api接口定义类:FeignController

package com.springcloud.lcl.feign;

import com.springcloud.lcl.feign.service.IFeignService;
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;

/**
 * @author lcl
 * @version 0.1
 * @date 2018/11/8
 **/
@RestController
public class FeignController {

    @Autowired
    private IFeignService feignService;

    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    public String hiService(@RequestParam String name){
        return feignService.getHiService(name);
    }

}

3.  application.properties 配置: 

spring.application.name=service-feign-fin

server.port=8790

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

4. 启动及验证同上篇。

你可能感兴趣的:(分布式服务)