新建ums工程(使用OpenFeign) 作者:马育民 • 2020-06-15 21:55 • 阅读:10092 # 关系图 [![](https://www.malaoshi.top/upload/pic/springcloud/QQ20200615-233115.png)](https://www.malaoshi.top/upload/pic/springcloud/QQ20200615-233115.png) # 创建 module 在父工程中新建 module `ums_openfeign` # 添加依赖 ### 添加 openfeign 依赖 修改pom.xml文件,添加依赖 ``` org.springframework.cloud spring-cloud-starter-openfeign ``` ### 完整依赖 ``` org.springframework.cloud spring-cloud-starter-openfeign org.springframework.cloud spring-cloud-starter-netflix-eureka-client top.malaoshi common 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test ``` # application.yml 从ums工程中,复制`application.yml`粘贴到该工程中 # 主启动类 要加上`@EnableFeignClients` 不需要 `@EnableEurekaClient` ``` @SpringBootApplication @EnableFeignClients public class UMSOpenFeignMain { public static void main(String[] args) { SpringApplication.run(UMSOpenFeignMain.class,args); } } ``` # 创建接口 OpenFeign 通过接口和注解即可实现web service client,简化操作,注解中的url 就是 原Controller 中的url: [![](https://www.malaoshi.top/upload/pic/eureka/QQ20210108152028.png)](https://www.malaoshi.top/upload/pic/eureka/QQ20210108152028.png) ``` package malaoshi.ums.clients; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import top.malaoshi.entities.Notice; import top.malaoshi.json.CommonResult; @Component @FeignClient(value="NOTICE-SERVICE") //notice微服务名称 public interface NoticeClient { //notice服务的url @GetMapping(value="/getNoticeById/{id}") public CommonResult getNoticeById(@PathVariable("id") String id); //notice服务的url @PostMapping(value="/sendNotice") public CommonResult sendNotice(@RequestBody Notice notice); } ``` ##### 解释: - `@FeignClient(value="NOTICE-SERVICE")`:注解中是eureka server中微服务的名字,如下图: [![](https://www.malaoshi.top/upload/pic/springcloud/QQ20200615-200357.png)](https://www.malaoshi.top/upload/pic/springcloud/QQ20200615-200357.png) - 方法名及注解,是从`NOTICE-SERVICE`服务的controller中复制过来的,如下图: [![](https://www.malaoshi.top/upload/pic/springcloud/QQ20200615-225318.png)](https://www.malaoshi.top/upload/pic/springcloud/QQ20200615-225318.png) # 创建controller ``` package malaoshi.ums.controllers; import lombok.extern.slf4j.Slf4j; import malaoshi.ums.clients.NoticeClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import top.malaoshi.entities.Notice; import top.malaoshi.json.CommonResult; import javax.annotation.Resource; @RestController @Slf4j public class FollowController { @Resource private NoticeClient noticeClient; @PostMapping(value="/follow") public CommonResult follow(Notice notice){ return noticeClient.sendNotice(notice); } @GetMapping(value="/getNotice/{id}") public CommonResult getNotice(@PathVariable("id") String id){ log.info("---"+id); return noticeClient.getNoticeById(id); } } ``` 原文出处:http://malaoshi.top/show_1EF5iMiuMztc.html