springboot+dubbo微服务入门

springboot+dubbo微服务入门

  • dubbo-api
  • dubbo-provider
  • dubbo-consumer

服务提供者依赖和服务消费者依赖:

<dependency>
        <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-dependencies-zookeeper -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.8</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>com.botao</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency

服务提供者配置文件:

server:
  port: 8001
dubbo:
  application:
    name: dubbo-provider
  registry:
    address: zookeeper://127.0.0.1:2181
  protocol:
    name: dubbo
    port: 20880

服务消费者配置文件:

server:
  port: 8080
dubbo:
  application:
    name: dubbo-consumer
  registry:
    address: zookeeper://127.0.0.1:2181

服务提供者业务代码

@DubboService(version = "1.0.0")
public class IUserServiceImpl implements IUserService {
    @Override
    public User getUserById(int id) {
        return new User(id,"admin","123456");
    }
}

服务消费者调用代码

    @DubboReference(version = "1.0.0")
    IUserService iUserService;
    @RequestMapping("/getUserById")
    public User getUserById(int id){
        return iUserService.getUserById(id);
    }

调用:http://127.0.0.1:8080/getUserById?id=15

结果:

// 20210218110942
// http://127.0.0.1:8080/getUserById?id=15

{
  "id": 15,
  "username": "admin",
  "password": "123456"
}
posted @ 2021-02-18 11:18  星时代曹波涛  阅读(162)  评论(0编辑  收藏  举报