Skip to content

Commit

Permalink
Springboot demo multi protocol (#264)
Browse files Browse the repository at this point in the history
* springboot demo multi protocol
  • Loading branch information
Reason94 committed Oct 11, 2022
1 parent c6d8e01 commit 891346d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
10 changes: 9 additions & 1 deletion examples/demo-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,13 @@
</parent>

<artifactId>demo-api</artifactId>


<dependencies>
<dependency>
<groupId>com.baidu.cloud</groupId>
<artifactId>spring-cloud-baidu-thirdparty-commons</artifactId>
<version>2022.2.0-SNAPSHOT</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,23 @@
package com.baidu.cloud.demo.api;

import com.baidu.cloud.demo.api.model.User;
import com.baidu.cloud.thirdparty.springframework.web.bind.annotation.DeleteMapping;
import com.baidu.cloud.thirdparty.springframework.web.bind.annotation.GetMapping;
import com.baidu.cloud.thirdparty.springframework.web.bind.annotation.PathVariable;
import com.baidu.cloud.thirdparty.springframework.web.bind.annotation.PostMapping;
import com.baidu.cloud.thirdparty.springframework.web.bind.annotation.PutMapping;
import com.baidu.cloud.thirdparty.springframework.web.bind.annotation.RequestBody;
import com.baidu.cloud.thirdparty.springframework.web.bind.annotation.RequestMapping;
import com.baidu.cloud.thirdparty.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
* 默认情况无需增加SpringMVC注解,
* 服务端对外提供Rest服务时才需要添加SpringMVC注解
*/
@RequestMapping("/user")
public interface UserService {

/**
Expand All @@ -29,22 +42,27 @@ public interface UserService {
* @param userId
* @return
*/
User getUser(Long userId);
@GetMapping
User getUser(@RequestParam("userId") Long userId);

User updateUser(User user);
@PutMapping
User updateUser(@RequestBody User user);

/**
* 返回值void
*
* @param userId
*/
void deleteUser(Long userId);
@DeleteMapping
void deleteUser(@RequestParam("userId")Long userId);

Long saveUser(User user);
@PostMapping
Long saveUser(@RequestBody User user);

/**
* 请求和响应参数均为空
*/
@GetMapping("/connect")
void connect();

/**
Expand All @@ -55,17 +73,20 @@ public interface UserService {
* @param user
* @return
*/
User multiParams(Long userId, User user);
@PostMapping("/multiparams")
User multiParams(@RequestParam("userId") Long userId, @RequestBody User user);

/**
* 返回List集合类 客户端需配置使用pb2-java序列化模式 - java api:ServiceConfig.setSerializeMode("pb2-java"); - spring boot 配置:
* starlight.client.config.default.serializeMode=pb2-java
*
* @return
*/
@GetMapping("/list")
List<User> allUsers();

String userName(Long userId);
@GetMapping("/name/{userId}")
String userName(@PathVariable("userId") Long userId);

/**
* 返回map集合类 客户端需配置使用pb2-java序列化模式 - java api:ServiceConfig.setSerializeMode("pb2-java"); - spring boot 配置:
Expand All @@ -75,13 +96,15 @@ public interface UserService {
* @param user
* @return
*/
Map<Long, User> userMap(Long userId, User user);
@PostMapping("/map")
Map<Long, User> userMap(@RequestParam("userId") Long userId, @RequestBody User user);

/**
* 方法抛出异常
*
* @param userId
* @return
*/
@GetMapping("/exception")
User userExp(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,18 @@ public class UserRestServiceImpl implements UserRestService {
/**
* 不依赖注册发现,直连方式调用
*/
@RpcProxy(remoteUrl = "brpc://localhost:8777")
@RpcProxy(remoteUrl = "localhost:8777", protocol = "brpc")
private UserService userService;

/**
* 不依赖注册发现,直连方式调用
*/
@RpcProxy(remoteUrl = "localhost:8777", protocol = "springrest")
private UserService restUserService;

@Override
public User getUser(Long userId) {
System.out.println("springrest result: " + restUserService.getUser(userId));
return userService.getUser(userId);
}

Expand Down

0 comments on commit 891346d

Please sign in to comment.