一、介绍
前端的所有请求都会映射到此微服务上,然后由该服务和其他服务间配合,相互调用,完成业务处理
二、搭建
2.1、选择相应组件
2.2、主程序配置
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class HbuLiveBackedApplication {
public static void main(String[] args) {
SpringApplication.run(HbuLiveBackedApplication.class, args);
}
}
2.3、配置文件
server.port=1110
spring.application.name=hbu-live-backed
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
三、feign方式调用
3.1 service接口
@FeignClient 注解,名称为user模块的服务名称
@FeignClient("hbu-live-user")
public interface UserService {
@RequestMapping(path = "/users", method = RequestMethod.GET)
String getUser(@RequestParam("userId") int userId);
}
3.2 controller
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
UserService userService;
@RequestMapping( method = RequestMethod.GET)
public ResponseEntity testUser(@RequestParam("userId") int userId){
return ResponseEntity.ok().body(userService.getUser(userId));
}
}
四、访问
http://127.0.0.1:1110/users?userId=1
数据库有数据,返回json串