SpringBoot+SpringCloud+Eureka+OpenFeign(融合版) |
您所在的位置:网站首页 › openfeign教程 › SpringBoot+SpringCloud+Eureka+OpenFeign(融合版) |
版本:SpringBoot 2.6.5 SpringCloud 2021.0.3 Eureka,OpenFeign无需设置版本 (要设置的话,选择3.1.3) 我的整个项目分享链接: 链接:https://pan.baidu.com/s/185Tb_hEcCmQKzawL4BiEhw 提取码:ljl6 有三个模块:eureka-server-demo(注册中心),orderservice(服务消费方),userservice(服务提供方) pom.xml 4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.5 com.practice eureka-server-demo 0.0.1-SNAPSHOT eureka-server-demo 1.8 2.6.5 2021.0.3 org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-pluginyaml文件 server: port: 8761 spring: application: name: eureka-server eureka: client: service-url: defaultZone: http://127.0.0.1:8761/eureka/ fetch-registry: false register-with-eureka: false启动类: 项目结构: UserMapper(接口) package com.practice.mapper; import com.practice.model.User; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface UserMapper { /** *@Param: @param id: *@return: User *@Description: 根据ID查找用户 */ User findUserById(Integer id); /** *@Param: @param null: *@return: List *@Description: 查找所有用户 */ List findAllUser(); /** *@Param: @param id: *@return: User *@Description: 根据订单查找用户 */ User findByOrder(Integer order_id); }UserMapper(xml) u.id,u.username,u.address SELECT FROM tb_user AS u WHERE u.id=#{id,jdbcType=INTEGER} SELECT FROM tb_user AS u SELECT FROM tb_user AS u LEFT JOIN tb_order AS o ON u.id=o.user_id WHERE o.id=#{order_id,jdbcType=BIGINT}yaml文件 server: port: 8081 spring: application: name: UserServiceProvider datasource: druid: url: jdbc:mysql://127.0.0.1:3306/spring-cloud-demo?serverTimezone=GMT%2B8 driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 1234 max-active: 100 max-wait: 60000 mvc: pathmatch: matching-strategy: ant_path_matcher mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.practice.entity configuration: map-underscore-to-camel-case: true pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: false params: count=countSql eureka: client: service-url: defaultZone: http://127.0.0.1:8761/eureka/UserController package com.practice.web; import com.practice.model.User; import com.practice.service.UserService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @author:ljl * @create: 2022-07-28 09:22 * @Description: 用户控制类 * @Version: 1.0 */ @RestController("/userServer") @Api(tags = "用户管理") public class UserController { @Autowired private UserService userService; @ApiOperation("根据ID查找用户") @GetMapping("/findById") public User getById(Integer id){ User user = userService.findUserById(id); return user; } @ApiOperation("查找所有用户") @GetMapping("/findAll") public List get(){ return userService.findAllUser(); } @ApiOperation("根据订单查找用户") @GetMapping("/findByOrder") public User getByOrder(Integer order_id){ return userService.findByOrder(order_id); } }启动类: 项目结构: 这个部分主要有userClient 和 User 和 OrderController 和 SwagerConfig就足够了, 因为此时的order是服务消费方 UserClient(重点) package com.practice.client; import com.practice.model.User; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(name = "UserServiceProvider",contextId = "userServer") public interface UserClient { /** *@Param: @param id: *@return: User *@Description: 根据订单查找用户 */ @GetMapping("/findByOrder") User findUserByOrder(@RequestParam("order_id")Integer order_id); }配置要点: 1.name的取值在服务提供方,也就是这个项目的user中的yaml配置的名称 2.contexId的值取user控制类(RestController) 3.接口上一定要加对应的注解比如@GetMapping 4.接口名称一定要和User提供的一致(findUserByOrder) 5.参数使用@RequestParam
启动类: 8081服务提供方 文章最后一些可能出现的错误提示: 小提示:如果在参考过程中发现了错误或者建议都可以私信我,一起成长。 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |