2 Star 5 Fork 0

沉默的注视 / y-rpc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 6.95 KB
一键复制 编辑 原始数据 按行查看 历史
沉默的注视 提交于 2023-04-19 14:17 . :memo:更新教程-setting.xml

使用教程

写在前面

目前仅支持nacos作为服务发现

1. 引入依赖


<dependencies>
    <!--服务端依赖-->
    <dependency>
        <groupId>io.gitee.yswysw</groupId>
        <artifactId>y-rpc-server-spring-boot-starter</artifactId>
        <version>${y-rpc.version}</version>
    </dependency>
    <!--客户端依赖-->
    <dependency>
        <groupId>io.gitee.yswysw</groupId>
        <artifactId>y-rpc-client-spring-boot-starter</artifactId>
        <version>${y-rpc.version}</version>
    </dependency>
</dependencies>

2. 定义服务端


@RpcService
public class TestServiceImpl implements TestService {
    @Override
    public String sayHello(String name) {
        return name + ": hello";
    }
}

3. 客户端调用


@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

4.1.2 填写项目信息

4.1.2

4.1.3 修改pom文件

4.1.3

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ysw.rpc</groupId>
    <artifactId>create-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <spring-boot.version>2.7.10</spring-boot.version>
        <y-rpc.version>1.0.0</y-rpc.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>io.gitee.yswysw</groupId>
            <artifactId>y-rpc-client-spring-boot-starter</artifactId>
            <version>${y-rpc.version}</version>
        </dependency>
        <dependency>
            <groupId>io.gitee.yswysw</groupId>
            <artifactId>y-rpc-server-spring-boot-starter</artifactId>
            <version>${y-rpc.version}</version>
        </dependency>
    </dependencies>

</project>

4.1.4 创建测试接口

public interface TestService {
    String sayHello(String name);
}

4.1.5 创建测试接口实现类


@RpcService
public class TestServiceImpl implements TestService {
    @Override
    public String sayHello(String name) {
        return name + ": hello";
    }
}

4.1.6 创建启动类


@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}

4.1.7 添加配置文件application.yml

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

这里直接改造了启动类


@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

4.1.10 检查是否已经成功注册到nacos

可以看到已经注册成功 4.1.10

4.1.11 使用接口调试工具进行调试

这里使用的是 ApiPost7 进行调试 4.1.11

请求成功!!教程到此结束,最后附上目录结构

└─main
    ├─java
    │  └─com
    │      └─ysw
    │          └─rpc
    │              └─create_demo
    │                      Application.java
    │                      TestService.java
    │                      TestServiceImpl.java

    └─resources
            application.yml

5. 注意

需要配置中央仓库才可以拉到依赖…… 下面是setting.xml

<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

    <localRepository>...</localRepository>

    <pluginGroups></pluginGroups>

    <proxies></proxies>

    <servers></servers>

    <mirrors>
    </mirrors>

    <profiles>
        <profile>
            <id>aliyun</id>
            <repositories>
                <repository>
                    <id>ali</id>
                    <url>https://maven.aliyun.com/repository/public</url>
                    <releases>
                        <enable>true</enable>
                    </releases>
                    <snapshots>
                        <enable>true</enable>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
        <profile>
            <id>repo1</id>
            <repositories>
                <repository>
                    <id>repo1</id>
                    <url>https://repo1.maven.org/maven2/</url>
                    <releases>
                        <enable>true</enable>
                    </releases>
                    <snapshots>
                        <enable>true</enable>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>aliyun</activeProfile>
        <activeProfile>repo1</activeProfile>
    </activeProfiles>
</settings>
Java
1
https://gitee.com/yswysw/y-rpc.git
git@gitee.com:yswysw/y-rpc.git
yswysw
y-rpc
y-rpc
master

搜索帮助