SpringCloud项目之Feign搭建
2021-02-18 00:17
标签:tco ram enable 依次 tty red auto vat frame 目的:实现trade服务通过feign调用user服务的功能。因为trade服务会用到user里的一些类和接口,所以抽出了其他服务需要的东西到user-api工程。避免trade直接依赖user导致依赖多余的东西。从零搭建一个SpringCloud项目之Feign搭建
工程简述
一、创建User接口工程user-api
dependencies>
dependency>
groupId>org.springframework.cloudgroupId>
artifactId>spring-cloud-starter-openfeignartifactId>
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.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>
- 配置文件
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.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>
- 编写配置文件
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工程。输入网址测试
http://localhost:8005/trade/testTrade/1
SpringCloud项目之Feign搭建
标签:tco ram enable 依次 tty red auto vat frame
原文地址:https://www.cnblogs.com/MonsterJ/p/12694207.html
上一篇:python telnet登陆
下一篇:cxf将wsdl生成java代码
文章标题:SpringCloud项目之Feign搭建
文章链接:http://soscw.com/index.php/essay/56798.html