先给上百度云链接,分别是dubbo-admin-2.6.0,2.6.0往后的版本不再提供注册中心可视化界面
还有一个是zookeeper的压缩包
dubbo-admin-2.6.0:
链接:https://pan.baidu.com/s/1EJjmCxipi1EIjWOOemCM3w 密码:jn0s
zookeeper:
链接:https://pan.baidu.com/s/1CoOC9NwwMp6wNZqv0qOm9A 密码:2o5r
先进行给代码吧,我做的是自己既是提供者,自己又是消费者。这样自己既可以消费自己提供的服务,也可
以消费别人提供的服务。
项目目录:
1、首先需要导入依赖
<!--dubbo 依赖 -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- zookeeper client依赖 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
<!--排除这个slf4j-log4j12-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除这个slf4j-log4j12-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
注意是为了排除日志重复,不要重复导入了spring-boot-starter-web。仔细看清楚
2、写入配置文件
server.port=9998
dubbo.application.name = back-stage
dubbo.registry.address = zookeeper://192.168.126.1:2181
dubbo.scan.base-package = com.pang.back_stage.Service
dubbo.protocol.port = 20888
dubbo.protocol.port是设置端口,默认的为20880,只是防止提供者暴露服务的时候端口号重复会出错
3、BackStageApplication
package com.pang.back_stage;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@DubboComponentScan
@EnableDubbo
public class BackStageApplication {
public static void main(String[] args) {
SpringApplication.run(BackStageApplication.class, args);
}
}
注意一定要写入@DubboComponentScan还有@EnableDubbo这两个注解,不然发布服务失败
4、TestService
package com.pang.front_stage.Service.service;
public interface TestService {
String test(String src);
}
这个是其他地方发布的服务,包路径要与发布的路径一样,dubbo端口号为20880
5、BackService
package com.pang.back_stage.Service.service;
public interface BackService {
public String back(String src);
}
这个是自己提供发布的Service,dubbo端口号为20888
6、BackServiceImpl
package com.pang.back_stage.Service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.pang.back_stage.Service.service.BackService;
import org.springframework.stereotype.Component;
@Component
@Service
public class BackServiceImpl implements BackService {
@Override
public String back(String src) {
System.out.println("提供者");
return src;
}
}
@Service注解一定要引入alibaba.dubbo的Service
7、DubboConfig
package com.pang.back_stage.MyConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DubboConfig {
@Bean
public ConsumerConfig consumerConfig() {
ConsumerConfig consumerConfig = new ConsumerConfig();
consumerConfig.setCheck(false);
consumerConfig.setTimeout(40000);
return consumerConfig;
}
}
防止消费者比提供者提前启动,不然会出现Null空指针报错
8、TestController
package com.pang.back_stage.Controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.pang.back_stage.Service.service.BackService;
import com.pang.front_stage.Service.service.TestService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/test")
public class TestController {
@Reference
BackService backService;
@Reference
TestService testService;
@RequestMapping(value = "/test2")
public String back() {
String front_stage = testService.test("前台消费者");
String back_stage = backService.back("后台消费者");
System.out.println(front_stage + "***" + back_stage);
return "成功";
}
}
简单的测试一下
9、打开zookeeper
进入zookeeper的bin目录下,双击打开zkServer.cmd
10、打开tomcat启动dubbo-admin-2.6.0
进入tomcat的bin目录下,双击打开startup.bat
11、查看效果图