1、下载zookeeper
什么是zookeeper:https://www.cnblogs.com/Bernard94/p/17495775.html
下载地址:https://dlcdn.apache.org/zookeeper/zookeeper-3.7.2/
下载好解压后进入conf目录下,把‘zoo_sample.cfg’复制并改名为‘zoo.cfg’:
修改配置文件的日志地址,修改到自己指定位置(非必要操作):
进入bin目录,找到‘zkServer.cmd’双击启动zookeeper:
2、新建项目
1、建父类项目
根据自己配置修改Maven地址和JDK版本:
修改Pom文件,新增以下配置:
<properties> <dubbo.version>3.2.0-beta.4</dubbo.version> <spring-boot.version>2.7.8</spring-boot.version> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencyManagement> <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Dubbo --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-bom</artifactId> <version>${dubbo.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper-curator5</artifactId> <version>${dubbo.version}</version> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <build> <pluginManagement> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> </plugin> </plugins> </pluginManagement> </build>
2、API模块
new-Module-Next
编写接口:HelloWorld
public interface HelloWorld { public String sayHello(); }
3、provider模块(新建步骤如上)
修改pom文件,新增以下代码
<dependencies> <!--dubbo统一接口--> <dependency> <groupId>com.code</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- dubbo --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <!-- zookeeper --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper-curator5</artifactId> <type>pom</type> <exclusions> <exclusion> <artifactId>slf4j-reload4j</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <!-- spring boot starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies>
新增配置文件:application.yml
dubbo: application: name: dubbo-provider protocol: name: dubbo port: -1 registry: address: zookeeper://127.0.0.1:2181 server: port: 8080
新增启动类:ProviderApplication
@SpringBootApplication @EnableDubbo public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
新增实现类:HelloWorldImpl
@DubboService public class HelloWorldImpl implements HelloWorld { @Override public String sayHello() { return "hello,world!"; } }
结构如下:
4、consumer模块(新建步骤如上)
修改pom文件,新增以下代码:
<dependencies> <!--dubbo统一接口--> <dependency> <groupId>com.code</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- dubbo --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <!-- zookeeper --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper-curator5</artifactId> <type>pom</type> <exclusions> <exclusion> <artifactId>slf4j-reload4j</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <!-- spring boot starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- spring boot web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.7.8</version> </dependency> </dependencies>
新增配置文件:application.yml
dubbo: application: name: dubbo-consumer protocol: name: dubbo port: -1 registry: address: zookeeper://127.0.0.1:2181 server: port: 8081
新增启动类:ConsumerApplication
@SpringBootApplication @EnableDubbo public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
新增测试类controller:
@RestController public class IndexController { @DubboReference public HelloWorld helloWorld; @RequestMapping("/hello") public String sayHello() { return helloWorld.sayHello(); } }
结构如下:
3、启动项目
1、启动zookeeper
2、启动provider
3、启动consumer
4、测试
浏览器输入路径:http://localhost:8081/hello
标签:dubbo,Springboot,spring,zookeeper,boot,org,public From: https://www.cnblogs.com/Bernard94/p/18193761