2018-09-04 10:57:52.0|分类: spring cloud|浏览量: 5036
|
OrderApp.java package com.cookqq.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
/**
* 订单服务
*
*/
@SpringBootApplication
@EnableFeignClients
public class OrderApp
{
public static void main( String[] args )
{
SpringApplication.run(OrderApp.class, args);
}
}@EnableFeignClients注解,声明使用Feign user.java package com.cookqq.consumer.bean;
import java.math.BigDecimal;
public class User {
private Long id;
private String username;
private String name;
private Integer age;
private BigDecimal balance;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public BigDecimal getBalance() {
return balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}UserFeignClient.java package com.cookqq.consumer.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.cookqq.consumer.bean.User;
/**
* provider-user项目微服务名称
* @author conca
*
*/
@FeignClient(name="provider-user")
public interface UserFeignClient {
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public User findUserById(@PathVariable("id") Long id);
}findUserById(@PathVariable Long id)方法如果去掉@PathVariable,findUserById(Long id)默认@RequestBody注解。RequestBody一定是包含在请求体中的,GET方式无法包含。Feign在GET请求包含RequestBody时强制转成了POST请求,而不是报错。 OrderController.java package com.cookqq.consumer.controller;
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.cookqq.consumer.bean.User;
import com.cookqq.consumer.feign.UserFeignClient;
@RestController
public class OrderController {
@Autowired
private UserFeignClient userFeignClient;
@GetMapping("/user/{id}")
public User findUserById(@PathVariable Long id){
// return this.restTemplate.getForObject("http://127.0.0.1:8000/"+id, User.class);
// List<ServiceInstance> listInfo = discoveryClient.getInstances("provider-user");
// if(listInfo.size()>0){
// return this.restTemplate.getForObject(
// listInfo.get(new Random().nextInt(listInfo.size())).getUri()+"/"+id,
// User.class);
// }
// return this.restTemplate.getForObject("http://provider-user/"+id, User.class);
return userFeignClient.findUserById(id);
}
}RequestMapping代表映射的路径,使用GET,POST,PUT,DELETE方式都可以映射到该端点。GetMapping代表get方式。 SpringMVC中常用的请求参数注解有(@RequestParam,@RequestBody,@PathVariable)等。name被默认当做@RequestParam。形参String name由框架使用字节码技术获取name这个名称,自动检测请求参数中key值为name的参数,也可以使用@RequestParam(“name”)覆盖变量本身的名称。当我们在url中携带name参数或者form表单中携带name参数时,会被获取到。 application.yml server: port: 8080 spring: application: name: consumer-order eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cookqq</groupId> <artifactId>consumer-order-feign</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>consumer-order-feign</name> <url>http://maven.apache.org</url> <!-- 引入spring boot的依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <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> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <!-- 引入spring cloud的依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project> feign依赖内容 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> 启动euraka 启动provider-user(多个) 启动consumer-order-feign 多次访问,查看provider-user访问日志
|
