zookeeper+springboot+dubbo简单实现
第一步:在虚拟机中搭建zookeeper。
第二步:本地创建3个maven工程,分别为wxh-dubbo-api(对外暴露的接口),wxh-dubbo-provider(服务提供者,接口的具体实现),wxh-dubbo-consumer(服务的消费方)
//如下工程的类都是在包“com.test.wxh”下创建的
第三步:在wxh-dubbo-api写一个简单的对外暴露的接口
public interface IUserService {
public void getName(String name) ;
}
第四步:① 在wxh-dubbo-provider的pom文件的依赖如下
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
<version>1.16.14</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.test.wxh</groupId>
<artifactId>wxh-dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
②,添加一个类实现上述的接口
@Service
@Component
public class UserService implements IUserService {
@Override
public void getName(String name) {
System.out.println(“HELLO” + name);
}}
**注意此处的service是com.alibaba.dubbo.config.annotation.Service包的**
③,写一个启动类
@SpringBootApplication(scanBasePackages = { “com.test”})//此处写自己工程的包路径
public class Application {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(Application.class);
springApplication.setWebEnvironment(true);
springApplication.run(args);
System.out.println(“启动成功 “);
}}
④,配置一个yml文件
⑤接下来可以运行步骤③的类,这个时候查看zookeeper会发现注册了一个。
如何查看zookeeper上注册了哪些服务
1、进入到zookeeper的bin目录,执行 sh zkCli.sh -server 虚拟机的地址:2181
2、执行ls /dubbo
会发现列表中有一个自己的工程 com.test.wxh.service.IUserService
3、执行ls /dubbo/com.test.wxh.service.IUserService/providers
4,执行ls /dubbo/com.test.wxh.service.IUserService/consumers(在还没有进行第五步的时候此处是一个空的列表,当启动了consumer工程后此处如下图)
第五步:配置consumer工程的pom
①、
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
<version>1.16.14</version>
</dependency>
<dependency>
<groupId>com.test.wxh</groupId>
<artifactId>wxh-dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
②、yml文件同provider工程
③、写具体的consumer类
@Component
public class ConsumerTest {
@Reference// 注意此处是dubbo的
private IUserService userService;
public void name() {
userService.getName(“jane”);
}
}
④、写启动类
@SpringBootApplication(scanBasePackages = { “com.test” })
public class Application {
@Autowired
private ConsumerTest consumerTest;
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(Application.class);
springApplication.setWebEnvironment(true);
springApplication.run(args);
System.out.println(“启动成功-consumer “);
}
@PostConstruct
public void r() {
consumerTest.name();
}
⑤。启动工程之后可以看到provider工程会输出一句话“HELLOjane”;