第3篇:创建总后端微服务

一、介绍

前端的所有请求都会映射到此微服务上,然后由该服务和其他服务间配合,相互调用,完成业务处理
这里写图片描述

二、搭建

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串

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值