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. Spring笔记(5) - 声明式事务@EnableTransactionManagement注解源码分析

    一.背景 前面详解了实现Spring事务的两种方式的不同实现:编程式事务和声明式事务,对于配置都使用到了xml配置,今天介绍Spring事务的注解开发,例如下面例子: 配置类:注册数据源.JDBC模板 ...

  2. eclipse时一直卡在进程中

    (1)今天遇到进入eclipse时一直卡在 进程中,无论是重启电脑,还是重启软件 删除 D:\workspace\.metadata\.lock 文件才有用,特此记录下. (2)还有一种情况就是打开e ...

  3. python中,*args和**kwargs这两个参数的作用是什么?

    *args和**kwargs这两个都是不定长参数,可以解决函数中参数不固定的问题,*args可以把位置参数转化成元组,**kwagrs可以把关键字参数转化成字段

  4. 03.axios登录前端

    1.创建一个Login.vue页面   1.1 写页面 views/Login.vue   在 views/components 下创建 Login.vue 页面   <template> ...

  5. [Luogu P1006]传纸条 (网格DP)

    题面 传送门:https://www.luogu.org/problemnew/show/P1006 Solution 挺显然但需要一定理解的网络(应该是那么叫吧)DP 首先有一个显然但重要的结论要发 ...

  6. 我叫Mongo,干了「查询终结篇」,值得您拥有

    这是mongo第三篇"查终结篇",后续会连续更新5篇 mongodb的文章总结上会有一系列的文章,顺序是先学会怎么用,在学会怎么用好,戒急戒躁,循序渐进,跟着我一起来探索交流. 通 ...

  7. SQL删除语句DROP、TRUNCATE、 DELETE 的区别

    主要介绍了SQL删除语句DROP.TRUNCATE. DELETE 的区别,帮助大家更好的理解和学习sql语句,感兴趣的朋友可以了解下 DROP: 1 DROP TABLE test; 删除表test ...

  8. String字符串加号的作用与基本数据类型加号的作用的区别

    1 public static void main(String args[] ){ 2 String Str="hellow"; 3 int num=110; 4 char c= ...

  9. Redis学习(一)——初识Redis

    1.Redis是什么 1)REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. 2)Redis的特点 Red ...

  10. 19Jinja2中宏定义

    1 @app.route('/') 2 def hello_world(): 3 return render_template('index.html') 4 5 6 {% macro input(n ...