springCloud 学习第四篇——构建feign进行服务调用

简单粗暴的复制之前构建好的spring-boot项目,依旧是替换pom.xml文件;很多原理性东西在这里我就不拿出来写了,直接上代码,让你可以直接构建项目进行测试,其他理论原理性知识,请自行查阅一下其他资料。

项目结构:

springCloud 学习第四篇——构建feign进行服务调用_第1张图片

一:pom.xml:



    4.0.0

    com.example
    md-feign
    0.0.1-SNAPSHOT
    jar

    md-feign
    Demo project for Spring Boot

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

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
            org.springframework.cloud
            spring-cloud-starter-feign
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-hystrix
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Dalston.RC1
                pom
                import
            
        
    

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

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



二:MdFeignApplication:

package com.example.mdfeign;

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;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
public class MdFeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(MdFeignApplication.class, args);
    }
}

三:application.yml

server:
  port: 8780
spring:
  application:
    name: service-feign
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

四:RibAndHixController、RibbonService

package com.example.mdfeign.controller;

import com.example.mdfeign.service.RibbonService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
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 RibAndHixController {
    @Autowired
    RibbonService rb;
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    @HystrixCommand(fallbackMethod = "hiError")
    public String sayHi(@RequestParam String name){
        return rb.ribbonMethod();
    }
    public String hiError(String name) {
        return "hi," + name + ",sorry,error!";
    }
}
package com.example.mdfeign.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(value="md-service-find")
public interface RibbonService {
    @RequestMapping(value = "/service", method = RequestMethod.GET)
    String  ribbonMethod();
}

 

你可能感兴趣的:(springCloud)