1.分布式简介 

2.Zookeeper和Dubbo 

3.zookeeper

(1).zookeeper安装

官方文档:https://hub.docker.com/_/zookeeper?tab=description

在docker上安装zookeeper

[root@hosystem ~]# docker search zookeeper

NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED

zookeeper                          Apache ZooKeeper is an open-source server wh…   947                 [OK]

[root@hosystem ~]# docker pull zookeeper:3.4.11

(2).zookeeper启动

[root@hosystem ~]# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

zookeeper           3.4.11              56d414270ae3        2 years ago         146MB

[root@hosystem ~]# docker run --name ZK01 -p 2181:2181 --restart always -d 56d414270ae3

054d27805228d7d3cc6f8df96962a2dd9c24f3f5907554160854c661ef8d67dd

[root@hosystem ~]# docker ps

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                                                                                         NAMES

054d27805228        56d414270ae3            "/docker-entrypoint.…"   41 seconds ago      Up 21 seconds       2888/tcp, 0.0.0.0:2181->2181/tcp, 3888/tcp                                                                    ZK01

#将2181端口添加到防火墙

[root@hosystem ~]# firewall-cmd --permanent --zone=public --add-port=2181/tcp

success

[root@hosystem ~]# firewall-cmd --reload

success

(3).zookeeper整合dubbo

[1].创建项目

①.创建Empty Project

②.New Module

创建provider

创建consumer

4.Spring Boot和Spring Cloud

Martin Fowler 微服务原文 https://martinfowler.com/articles/microservices.html 

(1).创建工程

[1].创建Empty Project

[2].创建eureka-server

[3].创建provider-ticket

[4].创建consumer-user

(2).配置eureka-server信息

[1].application.properties

server.port=8761

eureka.instance.hostname=eureka-server

eureka.client.register-with-eureka=false

eureka.client.fetch-registry=false

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

[2].EurekaServerApplication.java

package com.hosystem.eurekaserver;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**

 *     注册中心

 *     1.配置Eureka信息

 *     2.@EnableEurekaServer

 */

@EnableEurekaServer

@SpringBootApplication

public class EurekaServerApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaServerApplication.class, args);

}

}

访问eureka页面

(3).配置provider-ticket信息

[1].8001

①.TicketController.java

package com.hosystem.providerticket.controller;

import com.hosystem.providerticket.service.TicketService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class TicketController {

@Autowired

TicketService ticketService;

@GetMapping("/ticket")

public String getTicket(){

System.out.println("8001");

return ticketService.getTicket();

}

}

②.TicketService.java

package com.hosystem.providerticket.service;

import org.springframework.stereotype.Service;

@Service

public class TicketService {

public String getTicket(){

return "《模仿游戏》";

}

}

③.application.properties

server.port=8001

spring.application.name=provider-ticket

#注册服务的时候使用服务的ip地址

eureka.instance.prefer-ip-address=true

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

[2].8002

①.TicketController.java

package com.hosystem.providerticket.controller;

import com.hosystem.providerticket.service.TicketService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class TicketController {

@Autowired

TicketService ticketService;

@GetMapping("/ticket")

public String getTicket(){

System.out.println("8002");

return ticketService.getTicket();

}

}

②.TicketService.java

package com.hosystem.providerticket.service;

import org.springframework.stereotype.Service;

@Service

public class TicketService {

public String getTicket(){

return "《模仿游戏》";

}

}

③.application.properties

server.port=8002

spring.application.name=provider-ticket

#注册服务的时候使用服务的ip地址

eureka.instance.prefer-ip-address=true

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

(4).provider-ticket

[1].package

打包provider-ticket两次,一个端口为8001一次端口为8002

[2].启动provider

(5).consumer-user

[1]application.properties

server.port=8200

spring.application.name=consumer-user

#注册服务的时候使用服务的ip地址

eureka.instance.prefer-ip-address=true

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

[2].ConsumerUserApplication.java

package com.hosystem.consumeruser;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;

import org.springframework.context.annotation.Bean;

import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient //开启发现功能

@SpringBootApplication

public class ConsumerUserApplication {

public static void main(String[] args) {

SpringApplication.run(ConsumerUserApplication.class, args);

}

@LoadBalanced  //使用负载均衡机制

   @Bean

public RestTemplate restTemplate(){

return new RestTemplate();

}

}

[3].UserController.java

package com.hosystem.consumeruser.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.client.RestTemplate;

@RestController

public class UserController {

@Autowired

RestTemplate restTemplate;

@GetMapping("/buy")

public String buyTicket(String name){

String s =  restTemplate.getForObject("http://PROVIDER-TICKET/ticket",String.class);

return name+"观看了"+s;

}

}

注意,观察加@LoadBalanced和不加@LoadBalanced的区别。加了@LoadBalanced的可以实现负载均衡。

参考文档:

https://github.com/alibaba/dubbo

https://hub.docker.com/_/zookeeper?tab=description

https://github.com/apache/dubbo/

https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient

https://martinfowler.com/articles/microservices.html

10、Spring Boot分布式的更多相关文章

  1. spring boot分布式技术,spring cloud,负载均衡,配置管理器

    spring boot分布式的实现,使用spring cloud技术. 下边是我理解的spring cloud的核心技术: 1.配置服务器 2.注册发现服务器eureka(spring boot默认使 ...

  2. spring boot 分布式session实现

    spring boot 分布式session实现 主要是通过包装HttpServletRequest将session相关的方法进行代理. 具体是的实现就是通过SessionRepositoryFilt ...

  3. 2020最新的Spring Boot 分布式锁的具体实现(内附代码)

    前言 面试总是会被问到有没有用过分布式锁.redis 锁,大部分读者平时很少接触到,所以只能很无奈的回答 "没有".本文通过 Spring Boot 整合 redisson 来实现 ...

  4. spring boot 分布式事务实现(XA方式)

    关于spring boot 支持分布式事务,XA是常用的一种方式. 这里把相关的配置记下,方便以后使用. 首先配置两个不同的数据源 : 订单库.持仓库. /** * Created by zhangj ...

  5. spring boot 分布式锁组件 spring-boot-klock-starter

    基于redis的分布式锁spring-boot starter组件,使得项目拥有分布式锁能力变得异常简单,支持spring boot,和spirng mvc等spring相关项目 快速开始 sprin ...

  6. Spring Boot 分布式Session状态保存Redis

    在使用spring boot做负载均衡的时候,多个app之间的session要保持一致,这样负载到不同的app时候,在一个app登录之后,而打到另外一台服务器的时候,session丢失. 常规的解决方 ...

  7. 【spring boot】10.spring boot下的单元测试

    spring boot下的单元测试,思前想后还是需要单独用一章篇幅来看看. 然后在看了介绍和使用时候,我感觉并不想多去看了. 但是还是给后来人留下参考的路径: 官网说明:https://spring. ...

  8. (38)Spring Boot分布式Session状态保存Redis【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 在使用spring boot做负载均衡的时候,多个app之间的session要保持一致,这样负载到不同的app时候,在一个app登录之后,而访问到另外 ...

  9. 使用idea maven开发spring boot 分布式开发入门

    1:使用idea创建一个maven工程 bos2 2:删除bos2的src 文件夹,向pom.xml文件 中添加版本号 <packaging>pom</packaging> & ...

随机推荐

  1. vue中iframe加载慢,给它加loading效果

    js框架:vue ui框架:element 因为iframe加载慢,所以在它加载完成前添加loading效果,loading用的是element家的加载效果 <template> < ...

  2. array_walk_recursive 地址引用报错的问题

    今天看十八哥的视频,学习array_walk_recursive的用法,发现一直报错: PHP版本:5.6.19 代码界面: 报错界面: 查了很长时间,不知道什么问题,后来在网上终于找到原因所在: + ...

  3. 【转载】HPL与HPCG测试(一)

    来源:HPL与HPCG测试 (一) 一.HPL与HPCG 简介 1.HPL HPL 即 High Performance Linpack,它是针对现代并行计算集群的测试工具.用户不修改测试程序,通过调 ...

  4. 851. Loud and Rich —— weekly contest 87

    851. Loud and Rich 题目链接:https://leetcode.com/problems/loud-and-rich/description/ 思路:有向图DFS,记录最小的quie ...

  5. python爬虫07BeautifulSoup

    有一个高效的网页解析库 它的名字叫做 BeautifulSoup 它   是一个可以从 HTML 或 XML 文件中提取数据的 Python 库 首先我们要安装一下这个库 pip install be ...

  6. 蒲公英 &#183; JELLY技术周刊 Vol.29: 前端智能化在阿里的那些事

    蒲公英 · JELLY技术周刊 Vol.29 前端智能化是指借助于 AI 和机器学习的能力拓展前端,使其拥有一些超出现阶段前端能力的特性,这将是未来前端方向中一场重要的变革.目前各家互联网厂商都有自己 ...

  7. Mybatis的缓存——一级缓存和源码分析

    目录 什么是缓存? 一级缓存 测试一. 测试二. 总结: 一级缓存源码分析: 1. 一级缓存到底是什么? 得出结论: 2. 一级缓存什么时候被创建? 3. 一级缓存的执行流程 结论: 一级缓存源码分析 ...

  8. MySql中指定符号分割并分行展示

    1.涉及到的函数三个: 1.1 REPLACE('value','str1','str2') 用法规则:使用str2替换掉value中的所有的str1; SELECT REPLACE('我来了','来 ...

  9. 强迫自己学Jquery

    Jquery 适合我吗? 里面有很多用不上的功能, 我需要什么,常用的判断,常用的扩展,来自Dojo的Hitch函数,事件绑定,样式处理(添加 删除 替换),Ajax,模块加载, 足够了,真心不想用这 ...

  10. C# 中大端序与小端序

    C# 中大端序与小端序 static void Main(string[] args) { uint value = 0x12345678; Console.WriteLine("原始字节序 ...