一、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;
}
}