SpringBoot集成分布式环境示例

  • zookeeper的搭建,看这里,zookeeper+dubbo搭建
  • 首先创建一个maven项目,父工程的pom依赖,只是放了一个springboot的依赖;
<?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.sunyw.xyz</groupId>
    <artifactId>springbootdubbo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>customer</module>
        <module>provider</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
</project>
  • 接着创建两个maven项目,命名就起个provider/customer吧,首先provider依赖
<?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>springbootdubbo</artifactId>
        <groupId>com.sunyw.xyz</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>
    <artifactId>provider</artifactId>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--spring-boot-starter-dubbo-->
        <dependency>
            <groupId>com.gitee.reger</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.9</version>
        </dependency>
    </dependencies>

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

</project>
  • 然后是customer依赖
<?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>springbootdubbo</artifactId>
        <groupId>com.sunyw.xyz</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>
    <artifactId>customer</artifactId>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--spring-boot-starter-dubbo-->
        <dependency>
            <groupId>com.gitee.reger</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.9</version>
        </dependency>
        <dependency>
            <groupId>com.sunyw.xyz</groupId>
            <artifactId>provider</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

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

</project>
  • 然后provider的配置文件
server.port=9102
#服务治理当前项目的名称
spring.dubbo.application.name=provider
#暴露服务的包路径
spring.dubbo.base-package=com.sunyw.xyz.service
#zookeeper的注册地址
spring.dubbo.registry.address=127.0.0.1
#zookeeper的端口
spring.dubbo.registry.port=2181
#性能调优,balabala.....
spring.dubbo.protocol.name=dubbo
#重试次数,默认为2,这里设置为0
spring.dubbo.provider.retries=0


  • 然后是customer的配置文件
server.port=9100
#服务名
spring.dubbo.application.name=customer
#暴露服务的包路径
spring.dubbo.base-package=com.sunyw.xyz.service
#zookeeper的注册地址
spring.dubbo.registry.address=127.0.0.1
#zookeeper的端口
spring.dubbo.registry.port=2181
#性能调优,balabala.....
spring.dubbo.protocol.name=dubbo
#重试次数,默认为2,这里设置为0
spring.dubbo.provider.retries=0
  • 下面是代码,不涉及到db操作,就是个demo,所以就简单点吧,provider创建一个接口,一个实现类,@Service使用的是dubbo的,别搞错了嗷!
package com.sunyw.xyz.service;

public interface TestService {

    public  String getName(String name);
}

package com.sunyw.xyz.service;

import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

@Service
public class TestServiceImpl implements TestService {
    @Override
    public String getName(String name) {
        return "SUCCESS";
    }
}

  • customer下,写个controller就完事了,记得要把provider的pom坐标导入到customr的pom文件中
package com.sunyw.xyz;

import com.alibaba.dubbo.config.annotation.Reference;
import com.reger.dubbo.annotation.Inject;
import com.sunyw.xyz.service.TestService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {


    @Inject
    private TestService testService;

    @RequestMapping("/get")
    public String getName(){
      return   testService.getName("a");
    }

}

  • 编写两个启动类先启动provider,在启动customer,可以通过dubbo-admin来查看注册的情况
    在这里插入图片描述
  • 然后直接访问
    在这里插入图片描述
  • 完事了嗷!点个赞吧,秋梨膏!
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页