赞
踩
目录
3.创建一个模块,实现服务提供者:provider-server , 选择web依赖即可
zookeeper的包我们去maven仓库下载,zkclient;
【新版的坑】zookeeper及其依赖包,解决日志冲突,还需要剔除日志依赖;
3.本来正常步骤是需要将服务提供者的接口打包,然后用pom文件导入,我们这里使用简单的方式,直接将服务的接口拿过来,路径必须保证正确,即和服务提供者相同;
- package com.guo.provider.service;
-
- public interface TicketService {
- public String getTicket();
- }
- package com.guo.provider.service;
-
- public class TicketServiceImpl implements TicketService {
- @Override
- public String getTicket() {
- return "《狂神说Java》";
- }
- }
5.创建一个模块,实现服务消费者:consumer-server , 选择web依赖即可
6.项目创建完毕,我们写一个服务,比如用户的服务;
- package com.guo.consumer.service;
-
- public class UserService {
- //我们需要去拿去注册中心的服务
- }
将服务提供者注册到注册中心,我们需要整合Dubbo和zookeeper,所以需要导包
我们从dubbo官网进入github,看下方的帮助文档,找到dubbo-springboot,找到依赖包
- <!-- Dubbo Spring Boot Starter -->
- <dependency>
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo-spring-boot-starter</artifactId>
- <version>2.7.3</version>
- </dependency>
- <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
- <dependency>
- <groupId>com.github.sgroschupf</groupId>
- <artifactId>zkclient</artifactId>
- <version>0.1</version>
- </dependency>
- <!-- 引入zookeeper -->
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-framework</artifactId>
- <version>2.12.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-recipes</artifactId>
- <version>2.12.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- <version>3.4.14</version>
- <!--排除这个slf4j-log4j12-->
- <exclusions>
- <exclusion>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- #当前应用名字
- dubbo.application.name=provider-server
- #注册中心地址
- dubbo.registry.address=zookeeper://127.0.0.1:2181
- #扫描指定包下服务
- dubbo.scan.base-packages=com.guo.provider.service
- import org.apache.dubbo.config.annotation.Service;
- import org.springframework.stereotype.Component;
-
- @Service //将服务发布出去
- @Component //放在容器中
- public class TicketServiceImpl implements TicketService {
- @Override
- public String getTicket() {
- return "《狂神说Java》";
- }
- }
逻辑理解 :应用启动起来,dubbo就会扫描指定的包下带有@component注解的服务,将它发布在指定的注册中心中!
- <!--dubbo-->
- <!-- Dubbo Spring Boot Starter -->
- <dependency>
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo-spring-boot-starter</artifactId>
- <version>2.7.3</version>
- </dependency>
- <!--zookeeper-->
- <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
- <dependency>
- <groupId>com.github.sgroschupf</groupId>
- <artifactId>zkclient</artifactId>
- <version>0.1</version>
- </dependency>
- <!-- 引入zookeeper -->
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-framework</artifactId>
- <version>2.12.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-recipes</artifactId>
- <version>2.12.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- <version>3.4.14</version>
- <!--排除这个slf4j-log4j12-->
- <exclusions>
- <exclusion>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- #当前应用名字
- dubbo.application.name=consumer-server
- #注册中心地址
- dubbo.registry.address=zookeeper://127.0.0.1:2181
- package com.guo.consumer.service;
-
- import com.guo.provider.service.TicketService;
- import org.apache.dubbo.config.annotation.Reference;
- import org.springframework.stereotype.Service;
-
- @Service //注入到容器中
- public class UserService {
-
- @Reference //远程引用指定的服务,他会按照全类名进行匹配,看谁给注册中心注册了这个全类名
- TicketService ticketService;
-
- public void buyTicket(){
- String ticket = ticketService.getTicket();
- System.out.println("在注册中心买到"+ticket);
- }
-
- }
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class ConsumerServerApplicationTests {
-
- @Autowired
- UserService userService;
-
- @Test
- public void contextLoads() {
-
- userService.bugTicket();
-
- }
-
- }
1.这么多的服务,客户端该如何去访问?
2.这么多的服务,服务器之间如何进行通信?
3.这么多的服务,该如何管理和治理?
4.服务挂了,怎么办?
SpringCloud,是一套生态,就是来解决以上分布式架构的四个问题
想使用SpringCloud,必须要掌握Springboot,因为它是基于SpirngBoot的
1.SpringCloud NetFlix,出了一套解决方案
2.Apcahe Dubbo zookeeper 第二套解决方案
API:没有,要么找第三方,要么自己实现
Dubbo是一个高性能的基于java实现的RPC通信框架
服务注册与发现,zookeeper,没有熔断机制,接住了Hystrix
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。