一、环境准备
准备一个项目,项目中有3个模块,1
1. FeignClient(下图的goods-center)
2. FeignApi(下图的user-api) ,这个模块为其他2个模块的公共依赖模块,相当于2个项目通信的协议
3. FeignServer (下图的user-center)
二、 user-api 模块编写
1. pom.xml 文件关键点
<dependencies>
<!-- 次项目中包含VO对象,为了节约开发成本,引入lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- 因为需要在 api interface中写RequestMapping 故需要引入web包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- openfeign 不用多说了 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- 没有loadbalancer调用会报错 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- 这个工程没有main方法,所以只需要普通打包即可 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
</build>
2. UserCenterFacade
因为目前还有集成注册中心,故只能在@FeignClient 中指定服务端的地址 http://localhost:9521
package com.ychen.usercenter.api;
import com.ychen.usercenter.vo.UserInfoVo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(name = "user-center", url = "http://localhost:9521")
public interface UserCenterFacade {
@RequestMapping(value = "/getUserInfo/ByUserId")
UserInfoVo getUserInfoByUserId(Long userId);
}
3. UserInfoVo
package com.ychen.usercenter.vo;
import lombok.Data;
@Data
public class UserInfoVo {
private Long userId;
private String nickName;
private Integer age;
}
三、user-center 模块编写
1. pom.xml 文件关键点
将user-api 模块依赖进来即可
<dependencies>
<dependency>
<groupId>com.ychen</groupId>
<artifactId>user-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
2. UserCenterController
package com.ychen.usercenter.controllers;
import com.ychen.usercenter.api.UserCenterFacade;
import com.ychen.usercenter.vo.UserInfoVo;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserCenterController implements UserCenterFacade {
@Override
public UserInfoVo getUserInfoByUserId(Long userId) {
UserInfoVo userInfo = new UserInfoVo();
userInfo.setAge(22);
userInfo.setNickName("张三");
userInfo.setUserId(3323323258122L);
return userInfo;
}
}
3. 配置文件application.properties
server.port=9521
spring.application.name=user-center
四、goods-center模块编写
1. GoodsController
package com.ychen.goodscenter.controllers;
import com.ychen.usercenter.api.UserCenterFacade;
import com.ychen.usercenter.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GoodsController {
@Autowired
private UserCenterFacade userCenterFacade;
@GetMapping("/getGoodsDetails")
public String getGoodsDetails(@RequestParam(value = "goodsId") Long goodsId) {
UserInfoVo userInfo = userCenterFacade.getUserInfoByUserId(2222L);
return String.format("Hello %s!", userInfo.toString());
}
}
2. GoodsCenterApplication
package com.ychen.goodscenter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients(basePackages = "com.ychen.**")
public class GoodsCenterApplication {
public static void main(String[] args) {
SpringApplication.run(GoodsCenterApplication.class, args);
}
}
这里要注意@EnableFeignClients(basePackages = "com.ychen.**") ,因为com.ychen.usercenter.api.UserCenterFacade属于引用外部项目,所以这里要指定扫描包路径,否则Spring默认不会扫工程以外的包
五、版本管理
这里要注意一个点,SpringBoot3.1.7 对于的SpringCloud 版本是2022.0.3~2023.0.0 之间,版本错误也会导致项目起不来
以下是主工程的pom.xml 部分配置
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<java.version>17</java.version>
<spring-cloud.version>2022.0.4</spring-cloud.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>