SpringCloud项目之Feign搭建
从零搭建一个SpringCloud项目之Feign搭建
工程简述
目的:实现trade服务通过feign调用user服务的功能。因为trade服务会用到user里的一些类和接口,所以抽出了其他服务需要的东西到user-api工程。避免trade直接依赖user导致依赖多余的东西。
一、创建User接口工程user-api
- 新建一个module,取名为study-user-api
- 引入pom文件
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
- 创建一个User实体类
- 编写一个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实现工程
- 创建一个module用于对user接口提供实现,项目名为study-user
- 加入pom依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 本地依赖-->
<dependency>
<artifactId>study-user-api</artifactId>
<groupId>com.mmc</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
- 配置文件
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
- 写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;
}
}
三、创建交易工程
- 新建一个module,项目名为study-trade
- 引入pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- 本地依赖-->
<dependency>
<artifactId>study-user-api</artifactId>
<groupId>com.mmc</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
- 编写配置文件
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
- 写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+"下单成功";
}
}
- 启动类配置启用feign注解
@SpringBootApplication
@EnableFeignClients(basePackages = "com.mmc.user")
public class TradeApplication {
public static void main(String[] args) {
SpringApplication.run(TradeApplication.class);
}
}
- 测试
依次启动eureka Server,user工程,trade工程。输入网址测试