目的:实现trade服务通过feign调用user服务的功能。因为trade服务会用到user里的一些类和接口,所以抽出了其他服务需要的东西到user-api工程。避免trade直接依赖user导致依赖多余的东西。
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies>
@FeignClient(value = "study-user") public interface UserApi { @RequestMapping(value = "/getUserById",method = RequestMethod.GET) User getUserById(Integer id); }
<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
@RestController @RequestMapping("/user") public class UserController { @RequestMapping("/getUserById") public User getUserById(Integer id){ User user = new User(); user.setId(1); user.setName("小明"); return user; } }
<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
/** * @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+"下单成功"; } }
@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
github地址: https://github.com/mmcLine/spring-cloud-study