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

一、pom加入依赖

    <properties>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
    </properties>
 
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <!-- 使用feign这仨缺一不可 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

二、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<UserVO> getUserList(@RequestParam Map<String, Object> 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<String, Object> map = new HashMap<String, Object>();
        map.put("name", name);
        map.put("phone", phone);
        List<UserVO> userList = remoteService.getUserList(map);
        return userList;
    }

}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值