# y-rpc **Repository Path**: yswysw/y-rpc ## Basic Information - **Project Name**: y-rpc - **Description**: 一个简单易用的java rpc - **Primary Language**: Java - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 6 - **Forks**: 0 - **Created**: 2023-04-13 - **Last Updated**: 2024-12-20 ## Categories & Tags **Categories**: rpc **Tags**: Java, rpc, SpringBoot ## README # 使用教程 ## 写在前面 目前仅支持nacos作为服务发现 ## 1. 引入依赖 ```xml io.gitee.yswysw y-rpc-server-spring-boot-starter ${y-rpc.version} io.gitee.yswysw y-rpc-client-spring-boot-starter ${y-rpc.version} ``` ## 2. 定义服务端 ```java @RpcService public class TestServiceImpl implements TestService { @Override public String sayHello(String name) { return name + ": hello"; } } ``` ## 3. 客户端调用 ```java @RestController public class ClientController { @RpcClient private TestService service; @GetMapping("/{test}") public String test(@PathVariable String test) { return service.sayHello(test); } } ``` ## 4. 0代码使用教程 ### 4.1 新建项目 #### 4.1.1 `file -> new -> project` ![4.1.1](tutorial-resource/4.1.1.png) #### 4.1.2 填写项目信息 ![4.1.2](tutorial-resource/4.1.2.png) #### 4.1.3 修改pom文件 ![4.1.3](tutorial-resource/4.1.3.png) ```xml 4.0.0 com.ysw.rpc create-demo 1.0-SNAPSHOT 8 8 UTF-8 2.7.10 1.0.0 org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web io.gitee.yswysw y-rpc-client-spring-boot-starter ${y-rpc.version} io.gitee.yswysw y-rpc-server-spring-boot-starter ${y-rpc.version} ``` #### 4.1.4 创建测试接口 ```java public interface TestService { String sayHello(String name); } ``` #### 4.1.5 创建测试接口实现类 ```java @RpcService public class TestServiceImpl implements TestService { @Override public String sayHello(String name) { return name + ": hello"; } } ``` #### 4.1.6 创建启动类 ```java @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class); } } ``` #### 4.1.7 添加配置文件`application.yml` ```yaml server: port: 7878 spring: application: name: rpc-create-demo cloud: nacos: discovery: namespace: d2380f0c-b0bf-40bd-a4ea-b43d66576a1e server-addr: localhost:8848 ``` #### 4.1.8 添加测试Controller > 这里直接改造了启动类 ```java @RestController @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class); } @RpcClient private TestService service; @GetMapping("test") public String test(String name) { return service.sayHello(name); } } ``` #### 4.1.9 启动! 下图划线的就是启动成功的日志,因为这里是一个项目既做服务端又做客户端的原因是两条 ![4.1.9](tutorial-resource/4.1.9.png) #### 4.1.10 检查是否已经成功注册到nacos 可以看到已经注册成功 ![4.1.10](tutorial-resource/4.1.10.png) #### 4.1.11 使用接口调试工具进行调试 这里使用的是 ApiPost7 进行调试 ![4.1.11](tutorial-resource/4.1.11.png) 请求成功!!教程到此结束,最后附上目录结构 ```text └─main ├─java │ └─com │ └─ysw │ └─rpc │ └─create_demo │ Application.java │ TestService.java │ TestServiceImpl.java │ └─resources application.yml ``` ## 5. 注意 需要配置中央仓库才可以拉到依赖…… 下面是`setting.xml` ```xml ... aliyun ali https://maven.aliyun.com/repository/public true true always repo1 repo1 https://repo1.maven.org/maven2/ true true always aliyun repo1 ```