SpringBoot2.1.1 Eureka Feign服务调用(Greenwich版本)

一、pom加入依赖

    
        Greenwich.RELEASE
    
 
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    
    
    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
    

二、yml文件配置

server: 
    port: 80
eureka: 
    client: 
        serviceUrl: #注册中心的注册地址
            defaultZone: http://localhost:8081/eureka/ #注册中心地址
spring:
    application:
        name: service-customer #注册中心服务名称

三、新建feign接口

package com.mytest.spring.feign;

import java.util.List;
import java.util.Map;

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

import com.mytest.entity.UserVO;

@FeignClient("service-project") // 注册中心提供服务的服务名称
public interface RemoteService {

    @GetMapping(value = "/getUser") //参数少
    public UserVO getUser(@RequestParam("id") String id);

    @GetMapping(value = "/getUserList") //参数多可使用map,注意加注解@RequestParam
    public List getUserList(@RequestParam Map map);

    @PostMapping(value = "/saveUser") //post请求实体接收(map接收也行),注意加注解@RequestBody。
    public boolean saveUser(@RequestBody UserVO user);

}

四、修改启动类

package com.mytest.application;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.client.RestTemplate;
 
@SpringBootApplication
@EnableFeignClients("com.mytest.spring.feign") //注意:这里必须指定feign接口所在的包
@EnableDiscoveryClient
@ComponentScan(basePackages="com.mytest")
public class SpringBootWebApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }
}

五、配置完毕,使用:

package com.mytest.controller;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
 
import com.mytest.entity.UserVO;
import com.mytest.spring.feign.RemoteService;
 
@RestController
public class UserController {
 
    @Autowired
    RemoteService remoteService;
 
    /**
     * 根据id查询
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/getUser/{id}")
    public Object getUser(@PathVariable String id) throws Exception {
        UserVO user = remoteService.getUser(id);
        return user;
    }

    /**
     * 根据姓名和电话查询list
     * @param name
     * @param phone
     * @return
     * @throws Exception
     */
    @GetMapping("/getUserList/{name}/{phone}")
    public Object getUserList(@PathVariable String name,@PathVariable String phone) throws Exception {
        Map map = new HashMap();
        map.put("name", name);
        map.put("phone", phone);
        List userList = remoteService.getUserList(map);
        return userList;
    }

}

 

你可能感兴趣的:(springboot)