SpringBoot整合dubbo

SpringBoot整合dubbo

1.Dubbo是一种微服务框架,更多请访问apachedubbo github:https://github.com/apache/dubbo
2.环境,jdk1.8,zookpeer注册中心, springboot2.1.6; dubbo-spring-boot-starter0.2.0
3.统一的pom.xml

<?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>
  <packaging>pom</packaging>
  <modules>
    <module>api-service</module>
    <module>consumer01</module>
  </modules>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.ls</groupId>
  <artifactId>dubbo01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>dubbo01</name>
  <description>springboot整合dubbo</description>

  <properties>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.alibaba.boot</groupId>
      <artifactId>dubbo-spring-boot-starter</artifactId>
      <version>0.2.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

4.dubbo的提供者
4.1启动类ProvidersApplication

package com.ls;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@EnableDubbo
@SpringBootApplication
public class ProvidersApplication {

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

}

4.2实现类HelloServiceImpl

package com.ls.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ls.service.HelloService;


@Service //注意是dubbo的service依赖
public class HelloServiceImpl  implements HelloService {

    @Override
    public String sayHello(String name) {
        return name+"我是提供者";
    }
}

4.3application.yml

spring:
  application:
    name: provider
server:
  port: 9001
dubbo:
  application:
    name: provider
  protocol:
    name: dubbo
    port: 20889
  registry:
    address: zookeeper://192.168.157.130:2181
    timeout: 9000
  provider:
    timeout: 1000

5.消费者
5.1pom.xml

<?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">
  <parent>
    <artifactId>dubbo01</artifactId>
    <groupId>com.ls</groupId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <artifactId>consumer01</artifactId>
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>com.ls</groupId>
    <artifactId>api-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </dependency>
</dependencies>
</project>

5.2消费者启动类

@EnableDubbo
@SpringBootApplication
public class ConsumerApplication {

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

}

5.4.HelloController

package com.ls.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ls.service.HelloService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {
    @Reference
    private HelloService helloService;
    @GetMapping("/test")
    public String test(String name){
        String s = helloService.sayHello(name);
        return s;
    }
}

5.5application.yml

spring:
  application:
    name: consumer
server:
  port: 9002
dubbo:
  application:
    name: consumer
  registry:
    address: zookeeper://192.168.157.130:2181
    timeout: 9000

6.先启动zookpeer,zookpeer的安装自己去往搜索一下,启动提供者,然后启动消费者
在这里插入图片描述
在这里插入图片描述
7.访问展示http://localhost:9002/test?name=helllo
在这里插入图片描述
dubbo和springboot暂时写到这里源码:github:https://github.com/smileLs66/springboot/tree/master/dubbo01

参考:https://blog.csdn.net/qq_29102545/article/details/87368620

posted @ 2019-07-17 11:54  smileLS66  阅读(64)  评论(0编辑  收藏  举报