Feign实现了Ribbon不用拼接url通过调用接口直接调用方法传入参数一样的,可以通过官方文档自己学习
复制一份order
修改不要的只留一个主配置类,application.yml
eureka: client: serviceUrl: defaultZone: http://localhost:1000/eureka/#注册中心地址 instance: ip-address: true #使用ip配置 instance-id: order-server2 #指定服务的id server: port: 3001 spring: application: name: order-server2
这里不是做集群所以我们需要更改
spring:
application:
name: order-server2
1.导包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
3.需要在主配置类上打入标签
package cn.jiedada; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableFeignClients(basePackages = "cn.jiedada.Feign") public class EurekaOrderService3001 { public static void main(String[] args) { new SpringApplicationBuilder(EurekaOrderService3001.class).web(true).run(args); } }
4.自己写一个接口打入标签
package cn.jiedada.Feign; import cn.jiedada.domain.User; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; //添加集群application的名称 @FeignClient("USER-SERVER") public interface UserFeginClient { //添加路劲 @RequestMapping("/provider/user/{id}") public User getUserById(@PathVariable("id")Long id); }
4.Controller
package cn.jiedada.web.controller; import cn.jiedada.Feign.UserFeginClient; import cn.jiedada.domain.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @RequestMapping("/customer") public class OrderController { @Autowired private UserFeginClient userFeginClient; @RequestMapping("/user/{id}") public User getUserById(@PathVariable("id")Long id){ return userFeginClient.getUserById(id); } }