“看看星空,会觉得自己很渺小,可能我们在宇宙中从来就是一个偶然。所以,无论什么事情,仔细想一想,都没有什么大不了的。这能帮助自己在遇到挫折时稳定心态,想得更开。”– 《腾讯传》
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket
本文跟着我学习的脚步,进行一步一步的探索。
一、下载zookeeper服务注册管理器
- 下载ZooKeeper地址:http://www.apache.org/dyn/closer.cgi/zookeeper
liuxin@KK-MINI ~ cd zookeeper-3.5.2-alpha\ 2
liuxin@KK-MINI ~/zookeeper-3.5.2-alpha 2 ls
CHANGES.txt README_packaging.txt contrib ivysettings.xml src zookeeper-3.5.2-alpha.jar.md5
LICENSE.txt bin dist-maven lib zookeeper zookeeper-3.5.2-alpha.jar.sha1
NOTICE.txt build.xml docs logs zookeeper-3.5.2-alpha.jar
README.txt conf ivy.xml recipes zookeeper-3.5.2-alpha.jar.asc
liuxin@KK-MINI ~/zookeeper-3.5.2-alpha 2 cd conf
liuxin@KK-MINI ~/zookeeper-3.5.2-alpha 2/conf ls
configuration.xsl log4j.properties zoo.cfg zoo_sample.cfg
liuxin@KK-MINI ~/zookeeper-3.5.2-alpha 2/conf
在conf中创建输入zoo.cfg
tickTime=2000
#这个地址自定义
dataDir=/javaee/zookeeper/data
#这个地址自定义
dataLogDir=/javaee/zookeeper/log
clientPort=2181
然后在bin目录中./zkServer.sh start启动
二、创建生产者
- 在pom文件中引入dubbo
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>${dubbo-spring-boot}</version>
</dependency>
- application.properties
## Dubbo 服务提供者配置
spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=org.spring.springboot.dubbo
server.port=8082
- 向原来创建SpringBoot项目中一样,不过注意一个注解@Service使用dubbo提供的
import com.alibaba.dubbo.config.annotation.Service;
@Service(version = "1.0.1")
public class UserDubboServiceImpl implements UserDubboService {
@Override
public User getUserByName(String name) {
return new User(name,"shanghai");
}
}
当项目启动的时候,会把这个服务注册到zookeeper中。等到消费
三、创建消费者
- pom文件中和上面引入的一样。
- application.properties
## 避免和 server 工程端口冲突
server.port=8081
## Dubbo 服务消费者配置
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=org.spring.springboot.dubbo
- 创建在服务提供中一样的JavaBean对象
User
和UserDubboService
- 使用
@Reference(version = "1.0.1")
注解引入服务
@Component
public class UserDubboConsumerService {
@Reference(version = "1.0.1")
UserDubboService userDubboService;
public void getUserByName(){
System.out.println(userDubboService.getUserByName("周杰伦"));
}
}
当项目其中的时候,会想zookeeper中查询服务生产者地址,然后直接,调用生产者服务中的服务。zookeeper是提供软负载均衡。比nginx中需要手动配置服务地址,来看,好多了。
码云地址:https://git.oschina.net/chinesszz/springboot-learning-example.git
三、引入zkui视图查看zookeeper
- 上面服务生产和消费都创建成功了,那么我们需要看一下。此时需要下载
- zkui 地址:https://github.com/DeemOpen/zkui
- 开发环境中安装好maven,mvn package打包,然后将config.cfg中的zookeeper地址改为自己的
zkServer=localhost:2181,localhost:2181
liuxin@KK-MINI ~/git/模仿项目/zkui/target master ls
archive-tmp nohup.out zkui-2.0-SNAPSHOT.jar
classes surefire-reports zkui-out.log
generated-sources test-classes
maven-archiver zkui-2.0-SNAPSHOT-jar-with-dependencies.jar
liuxin@KK-MINI ~/git/模仿项目/zkui/target master java -jar zkui-2.0-SNAPSHOT-jar-with-dependencies.jar
Please create config.cfg properties file and then execute the program!
✘ liuxin@KK-MINI ~/git/模仿项目/zkui/target master java -jar zkui-2.0-SNAPSHOT-jar-with-dependencies.jar
三月 15, 2017 2:03:35 下午 com.googlecode.flyway.core.metadatatable.MetaDataTableImpl createIfNotExists
信息: Creating Metadata table: "PUBLIC"."schema_version"
三月 15, 2017 2:03:35 下午 com.googlecode.flyway.core.command.DbMigrate migrate
信息: Current version of schema "PUBLIC": << Empty Schema >>
三月 15, 2017 2:03:35 下午 com.googlecode.flyway.core.command.DbMigrate applyMigration
信息: Migrating schema "PUBLIC" to version 1
三月 15, 2017 2:03:35 下午 com.googlecode.flyway.core.command.DbMigrate logSummary
信息: Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.143s).
log4j:WARN No appenders could be found for logger (org.eclipse.jetty.util.log).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
用户账号和密码都在配置文件中...
- 默认
{ "username":"admin" , "password":"manager","role": "ADMIN" }
参考: