feign最简单调用和被调用的demo
carl 10/25/2023 feignjavaspringCloud
feign最简单调用和被调用的demo
以下是一个简单示例,展示了如何使用 Feign 进行服务之间的调用。
首先,假设有一个名为 user-service
的微服务,它提供了对用户信息的增、删、改、查操作。
被调用的服务端(user-service)示例:
- 添加依赖:在
pom.xml
文件中添加以下依赖项:
<dependencies>
<!-- 其他依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
1
2
3
4
5
6
7
2
3
4
5
6
7
- 启用 Feign:在启动类上添加
@EnableFeignClients
注解,以启用 Feign 客户端:
@SpringBootApplication
@EnableFeignClients
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
- 创建接口:创建一个用于定义远程调用的接口
UserClient
:
@FeignClient(name = "user-client", url = "${userservice.baseurl}")
public interface UserClient {
@GetMapping("/users/{id}")
UserDto getUserById(@PathVariable("id") Long id);
@PostMapping("/users")
UserDto createUser(@RequestBody UserDto userDto);
...
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
在上述代码中,我们使用了 @FeignClient
注解来声明一个 Feign 客户端,并指定了远程服务的名称和 URL。
- 使用接口:在需要调用服务的地方注入
UserClient
接口,并使用其方法进行调用:
@Service
public class UserService {
private final UserClient userClient;
public UserService(UserClient userClient) {
this.userClient = userClient;
}
public UserDto getUserById(Long id) {
return userClient.getUserById(id);
}
public UserDto createUser(UserDto userDto) {
return userClient.createUser(userDto);
}
...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
在上述代码中,我们通过构造函数注入了 UserClient
接口,并在需要的地方调用其中的方法。
这样,被调用的服务端就完成了。接下来,我们看一下如何编写调用方的代码。
调用方示例:
- 添加依赖:在
pom.xml
文件中添加以下依赖项:
<dependencies>
<!-- 其他依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
1
2
3
4
5
6
7
2
3
4
5
6
7
- 启用 Feign:在启动类上添加
@EnableFeignClients
注解,以启用 Feign 客户端:
@SpringBootApplication
@EnableFeignClients
public class MyAppApplication {
public static void main(String[] args) {
SpringApplication.run(MyAppApplication.class, args);
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
- 创建接口:创建一个用于调用远程服务的接口
UserServiceClient
:
@FeignClient(name = "user-service")
public interface UserServiceClient {
@GetMapping("/users/{id}")
UserDto getUserById(@PathVariable("id") Long id);
@PostMapping("/users")
UserDto createUser(@RequestBody UserDto userDto);
...
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
在上述代码中,我们使用了 @FeignClient
注解来声明一个 Feign 客户端,并指定了要调用的远程服务名称。
- 使用接口:在需要调用服务的地方注入
UserServiceClient
接口,并使用其方法进行调用:
@RestController
public class UserController {
private final UserServiceClient userServiceClient;
public UserController(UserServiceClient userServiceClient) {
this.userServiceClient = userServiceClient;
}
@GetMapping("/users/{id}")
public UserDto getUserById(@PathVariable Long id) {
return userServiceClient.getUserById(id);
}
@PostMapping("/users")
public UserDto createUser(@RequestBody UserDto userDto) {
return userServiceClient.createUser(userDto);
}
...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
在上述代码中,我们通过构造函数注入了 UserServiceClient
接口,并在需要的地方调用其中的方法。