SpringBoot与分布式

释放双眼,带上耳机,听听看~!

SpringBoot


Dubbo

zookeeper
整合

1、准备两个项目,一个服务提供者,一个消费者

2、将服务提供者注册到注册中心

(1) 
引入dubbo

starter

(2) 
引入zookeeper
的客户端工具

(3) 
在application.yml
中配置
dubbo
相关属性

(4) 
在要发布的类上标注上dubbo

@Service
注解并将类加入到容器

3、消费者使用服务者提供的功能(引用注册中心的服务)

(1) 
引入dubbo

starter

(2) 
引入zookeeper
的客户端工具

(3) 
在application.yml
中配置
dubbo
相关属性

(4) 远程调用发布到注册中心的类

引入dubbo


starter


1
2
3
4
5
6
1<dependency>
2    <groupId>com.alibaba.boot</groupId>
3    <artifactId>dubbo-spring-boot-starter</artifactId>
4    <version>0.1.0</version>
5</dependency>
6

引入zookeeper

的客户端工具
zkclient


1
2
3
4
5
6
1<dependency>
2    <groupId>com.101tec</groupId>
3    <artifactId>zkclient</artifactId>
4    <version>0.10</version>
5</dependency>
6

在application.yml

中配置
dubbo
相关属性


1
2
3
4
5
6
7
8
1dubbo:
2  application:
3    name: provider-ticket    # 项目的名字
4  registry:
5    address: zookeeper://172.17.121.58:2181   # 注册中心zookeeper的地址
6  scan:
7    base-packages: com.jinxin.task.controller   # 指定发布的包
8

在要发布的类上标注上dubbo


@Service
注解并将类加入到容器


1
2
3
4
1@Component   // 加到容器中
2@Service    // 导入dubbo的service将服务发布出去
3public class DubboService {
4

配置消费者的dubbo

相关属性


1
2
3
4
5
6
1dubbo:
2  application:
3    name: provider-ticket    # 项目的名字
4  registry:
5    address: zookeeper://172.17.121.58:2181   # 注册中心zookeeper的地址
6

远程调用注册中心的类

  • 首先需要复制一份跟服务者相同目录的包,带上刚刚发布到的服务中心的DubboService

的接口

  • 然后使用@Reference

远程调用
DubboService
即可


1
2
3
1@Reference
2DubboService dubboService;
3

Spring Boot和Spring Cloud

spring cloud
分布式开发五大常用组件

  • 服务发现——

Netflix Eureka

  • 客服端负载均衡——

Netflix Ribbon

  • 断路器——

Netflix Hystrix

  • 服务网关——

Netflix Zuul

  • 分布式配置——

Spring Cloud Config

选择Eureka-Server

,这个项目用来做注册中心

SpringBoot与分布式

启用注册中心

1、
配置Eureka
信息


1
2
3
4
5
6
7
8
9
10
11
1server:
2  port: 8761
3eureka:
4  instance:
5    hostname: eureka-server   # eureka实例的主机名
6  client:
7    register-with-eureka: false  # 不把自己注册到eureka上
8    fetch-registry: false   # 不从eureka上获取服务的注册信息
9    service-url:
10      defaultZone: http://localhost:8761/eureka/   # 注册中心的地址
11

2、
在主配置类上加上@EnableEurekaServer


1
2
3
4
5
6
7
8
9
1@EnableEurekaServer
2@SpringBootApplication
3public class EurekaServerApplication {
4
5    public static void main(String[] args) {
6        SpringApplication.run(EurekaServerApplication.class, args);
7    }
8}
9

之后就可以访问localhost:8761
来到
eureka
服务中心页面了

选择Eureka-Discovery

,这个项目用来做注册服务

SpringBoot与分布式

配置服务提供者信息


1
2
3
4
5
6
7
8
9
10
11
12
13
1server:
2  port: 8001
3spring:
4  application:
5    name: provide-ticket   # 提供者名字
6
7eureka:
8  instance:
9    prefer-ip-address: true    # 注册服务时使用服务的ip地址进行注册
10  client:
11    service-url:
12      defaultZone: http://localhost:8761/eureka/   # 注册中心的地址
13

然后启动项目就可以注册到的服务中心了

现在配置一个方法返回一个字符串,因为消费者是通过http
请求来去请求中心获取数据的,所以写一个
controller


1
2
3
4
5
1@GetMapping("/ticket")
2public String getTicket(){
3    return "《厉害了,我的国》";
4}
5

选择Eureka-Discovery

,这个项目用来做服务消费者

SpringBoot与分布式

配置消费者信息,跟配置服务者相同


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1server:
2  port: 8200
3
4
5spring:
6  application:
7    name: consumer-user
8
9eureka:
10  instance:
11    prefer-ip-address: true    # 注册服务时使用服务的ip地址进行注册
12  client:
13    service-url:
14      defaultZone: http://localhost:8761/eureka/   # 注册中心的地址
15

在主程序类中开启发现服务的功能跟注册RestTemplate


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1@EnableDiscoveryClient    // 开启发现服务功能
2@SpringBootApplication
3public class ConsumerUserApplication {
4
5    public static void main(String[] args) {
6        SpringApplication.run(ConsumerUserApplication.class, args);
7    }
8
9    @LoadBalanced    // 启用负载均衡机制,如果有多个服务提供者,会轮询着访问
10    @Bean
11    public RestTemplate restTemplate(){
12        return new RestTemplate();
13    }
14}
15

去注册中心获取数据


1
2
3
4
5
6
7
8
9
10
11
12
13
1@RestController
2public class BuyTicket {
3    @Autowired
4    RestTemplate restTemplate;
5
6    @GetMapping("buy")
7    public String buyTicket(String name){
8        // 发送http请求获取数据
9        String ticket = restTemplate.getForObject("http://PROVIDE-TICKET/ticket", String.class);
10        return name + "购买了" + ticket;
11    }
12}
13

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++异常

2022-1-11 12:36:11

安全技术

springboot 任务调度

2022-1-12 12:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索