SpringCloud项目之Feign搭建

从零搭建一个SpringCloud项目之Feign搭建

工程简述

SpringCloud项目之Feign搭建_第1张图片目的:实现trade服务通过feign调用user服务的功能。因为trade服务会用到user里的一些类和接口,所以抽出了其他服务需要的东西到user-api工程。避免trade直接依赖user导致依赖多余的东西。

一、创建User接口工程user-api

  1. 新建一个module,取名为study-user-api
  2. 引入pom文件
<dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-openfeignartifactId> dependency> dependencies>
  1. 创建一个User实体类
  2. 编写一个UserApi接口,value里的study-user对应后面user实现工程的spring.application.name的值
@FeignClient(value = "study-user")
public interface UserApi { @RequestMapping(value = "/getUserById",method = RequestMethod.GET) User getUserById(Integer id); }

二、创建User实现工程

  1. 创建一个module用于对user接口提供实现,项目名为study-user
  2. 加入pom依赖
<dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency>  <dependency> <artifactId>study-user-apiartifactId> <groupId>com.mmcgroupId> <version>1.0-SNAPSHOTversion> dependency> dependencies>
  1. 配置文件
server.port=8003
#注册到eureka服务端的微服务名称
spring.application.name=study-user
#注册到eureka服务端的地址
#eureka.client.service-url.defaultZone=http://localhost:9000/eureka/
security.login.username=root
security.login.pass=123456789
eureka.client.service-url.defaultZone=http://${security.login.username}:${security.login.pass}@localhost:9000/eureka/ #点击具体的微服务,是否显示ip eureka.instance.prefer-ip-address=true eureka.instance.instance-id=study-user-8003
  1. 写user接口实现,一个UserController
@RestController
@RequestMapping("/user")
public class UserController { @RequestMapping("/getUserById") public User getUserById(Integer id){ User user = new User(); user.setId(1); user.setName("小明"); return user; } }

三、创建交易工程

  1. 新建一个module,项目名为study-trade
  2. 引入pom依赖
<dependency>
            <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-openfeignartifactId> dependency>  <dependency> <artifactId>study-user-apiartifactId> <groupId>com.mmcgroupId> <version>1.0-SNAPSHOTversion> dependency>
  1. 编写配置文件
server.port=8005
#注册到eureka服务端的微服务名称
spring.application.name=study-trade
#注册到eureka服务端的地址
#eureka.client.service-url.defaultZone=http://localhost:9000/eureka/
security.login.username=root
security.login.pass=123456789
eureka.client.service-url.defaultZone=http://${security.login.username}:${security.login.pass}@localhost:9000/eureka/ #点击具体的微服务,是否显示ip eureka.instance.prefer-ip-address=true eureka.instance.instance-id=study-trade-8005
  1. 写controller调用user服务
/**
 * @description:
 * @author: mmc
 * @create: 2020-04-05 11:41
 **/
@RestController
@RequestMapping("/trade") public class TradeController { @Autowired private UserApi userApi; @RequestMapping(value = "/testTrade/{userid}",method = RequestMethod.GET) public String testTrade(@PathVariable Integer userid){ User user= userApi.getUserById(userid); String name = user.getName(); System.out.println("用户"+name+"下单成功...."); return "用户"+name+"下单成功"; } }
  1. 启动类配置启用feign注解
@SpringBootApplication
@EnableFeignClients(basePackages = "com.mmc.user")
public class TradeApplication {

    public static void main(String[] args) { SpringApplication.run(TradeApplication.class); } }
  1. 测试

依次启动eureka Server,user工程,trade工程。输入网址测试

http://localhost:8005/trade/testTrade/1

你可能感兴趣的:(SpringCloud项目之Feign搭建)