JAVA分布式开发

JAVA分布式开发

Redis

Redis是一个开源的使用ANSIC语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API,Redis就像是一个HashMap以一个独立进程的形式运行,被当作缓存使用
步骤:安装Redis、启动服务端(redis-server.exe)、启动客户端(redis-cli.exe)
数据类型:String(字符串)、List(列表)、Hash(字典)、Set(集合)、Sorted Set(有序集合)
命令http://redisdoc.com/
Jedis操作数据:引入jar包、操作数据(字符串(set,get)、Map(hmset,hmget)、List(lpush,lrange)、set(sadd,srem))

Git

Git:分布式版本控制软件(本地版本控制、重写提交说明、可以“后悔”、分支系统、全量文件)
常用命令
git add:将本地文件增加到暂存区
git commit:将暂存区的内容提交到本地仓库(本地分支,默认master分支)
git push:将本地仓库的内容推送到远程仓库(远程分支)
git pull:将远程仓库(远程分支)的内容拉取到本地仓库(本地分支)
步骤
安装Git、配置路径和Git、搭建git服务器(远程仓库) :统一的托管网站(https://github.com/)、配置ssh并检测、本地新建Git项目、在远程建立Git项目、本地项目和远程项目关联
发布项目:

# 第一次发布
git remote add origin [email protected]:yanqun/mygitremote.git
git add .      //文件-暂存区
git commit -m "注释内容"  //暂存区-本地分支(默认master)
git push -u origin master
# 后续提交
git add.
git commit -m "提交到分支"
git push  origin master

下载项目:

# git clone 唯一标识符
git clone [email protected]:yanqun/mygitremote.git

更新项目:

git pull

冲突:发现冲突.进入同步视图->添加到本地暂存区 add to index->提交到本地分支 commit->更新服务端的分支内容到本地分支 pull->修改冲突:直接修改 或者 merge tool
多人开发:github中该项目 settings->增加合作者:Collaborators 加入合作者:github 全名或邮箱->发送邀请链接

Git进阶

三种状态:已修改(modified)、已暂存(staged)、已提交(commited)
本机三区:工作区(unstage)、暂存区(staged)、对象区(commited)
Git命令

//初始化(默认master分支)
git init
//查看文件状态
git status
//查看文件地址目录
pwd
//查看子文件
ls
//创建文件
touch 文件名
//删除文件
rm -rf 文件名
//文件工作区->暂存区
git add .  //当前目录全部放到暂存区
git add 文件名  //单个文件放到暂存区
//文件暂存区->对象区(默认master)
git commit
git commit -m "注释内容"  
//文件暂存区->工作区
git rm --cached 文件名
//查看日志
git log
git log -最近的次数  //查看最近提交
git reflog  //所有版本
git log --pretty=oneline  //单行查看
git log --pretty=format:"%h - %an ,%ar : %s"  //格式化查看
//sha1唯一值用于区分是哪一次的提交
//邮箱及用户名设置
git config --system user.name '用户名'
git config --system user.email '邮箱'
//删除邮箱及用户名设置
git config --local --unset user.name
git config --local --unset user.email
//修改文件(文件回退到工作区)
vi 文件名
//还原修改
git checkout -- 文件名
//删除已提交的文件
git rm 文件名  //删除之后文件被放到暂存区
rm 文件名  //删除之后文件被放到工作区,要add到暂存区
//再次提交即彻底删除
git commit -m "注释内容" ;
//撤销删除
git reset HEAD 文件名
git checkout -- 文件名
//重命名
git mv 原文件 新文件
mv 原文件 新文件
//上次注释重写
git commit --amend -m '修正'
//忽略文件(将忽略的文件放在gitignore配置文件中,默认忽略空目录)
.gitignore
//查看分支 
git branch
//创建分支 
git branch 分支名
//切换分支 
git checkout 分支名
//删除分支   (不能删除当前分支,删除未合并的内容建议先合并)
git branch -d 分支名
//强行删除分支
git branch -D 分支名
//分支重命名
git branch -m 原分支名 新分支名
//合并分支 (合并分支时默认使用fast forward,归于一点commit,但丢失分支信息)
git merge 分支名
git merge 分支名--no-ff
//合并冲突问题,解决冲突:vi 文件名,git add 文件名, git commit -m  "xx"
//版本穿梭:在多个commit之间进行穿梭(回退和前进)(游离状态)
git reset --hard HEAD~n
git reset --hard sha1值的前几位
//保存现场
git stash 
//还原现场
git stash pop  //将原来保存的删除, 用于还原内容
git stash apply //还原内容,不删除原保存的内容
//查看现场
git stash list
//责任:查看文件提交的作者
git blame 文件名
//差异性
diff 文件名 文件名  //比较的是文件本身差异
git diff  //比较的区中的文件差异
//推送push
git remote add origin https://github.com/yanqun/git2019.git  //http
git remote add origin git@github.com:yanqun/git2019.git  //ssh
git push -u origin master
//生成秘钥
ssh-keygen
//下载pull(pull = fetch + merge)
git pull
//感知远程github分支(fetch)
git remote show origin
//远程冲突问题,解决冲突:vi 文件名,git add 文件名, git commit -m  "xx"
//子模块:在一个仓库中引用另一个仓库的代码
//加入子模块(submodule)
git submodule add git@github.com:yanqun/git2019.git
//本地更新远端子模块
git submodule foreach git pull
//父模块加入子模块(subtree)
git subtree add -P 子模块名 子模块仓库名-origin  master (--squash)  //squash减少commit的次数
//本地更新远端子模块
git subtree pull -P 子模块名 子模块仓库名-origin  master
//本地修改的父工程内容推送到远程中真实的子模块中
git subtree push -P 子模块名 子模块仓库名-origin  master

Svn

Svn:实现版本控制、集中式储存开发
Svn常见操作:发布项目(右键要发布的项目-team- share project)、下载项目(file-import-svn)、提交项目(右键待更新的文件/项目: team-提交)、更新项目(右键待更新的文件/项目: team-提交)
步骤
svn的安装与配置、中央仓库的设置、svn启动(命令行方式和注册系统方式)、访问项目
仓库conf文件
authz文件:设置权限
svnserve文件:开启访问方式(匿名或授权)、
passwd:设置账号密码
冲突:(提交时更新时)右键项目与资源库同步,选中有红色标识的文件,右键-编辑冲突 ->修改->右键->team->编辑为解决(避免冲突:及时提交和及时更新)
历史版本:选中需要恢复/查看的文件- team,如果要恢复成历史版本(获取内容)
外网发布:nat123等软件 将内网映射成外网、租一台互联网服务器(新网、万网、阿里云)将项目发布到服务器中、svn托管网站 http://www.svnchina.com/

Nginx

Nginx:高性能的HTTP和反向代理web服务器,通常配合Tomcat协同工作
步骤:安装Nginx、启动(重启命令:nginx -s reload)、配置文件conf/nginx.conf文件(端口号、页面存放位置、欢迎页面)、然后修改nginx.conf实现反向代理

        location / {
        proxy_pass http://127.0.0.1:8080;
	}

动静分离:png、css、js交给nginx处理;jsp交给tomcat处理
修改nginx.conf实现动静分离

 location / {
        proxy_pass http://127.0.0.1:8080;
 location ~\.(css|js|png)$ {
        root F:\jdk\apache-tomcat-8.5.46\webapps\ROOT;
	}

负载均衡:对请求按照权重分配给多个Tomcat,从而缓解单独一个Tomcat受到的压力
修改nginx.conf实现负载均衡

upstream tomcat_8080_8888{
	server	127.0.0.1:8080 weight=1;
	server	127.0.0.1:8888 weight=2;
    }
location / {
       proxy_pass http://tomcat_8080_8888;
	}

session共享:解决负载均衡过程中多服务器的session问题
ip定位

upstream tomcat_8080_8888{
	server	127.0.0.1:8080 weight=1;
	server	127.0.0.1:8888 weight=2;
	ip_hash;
    }

Redis+Tomcat-sessoin-manager

  1. 启动redis
  2. 给两个tomcat使用jar包(jedis-2.5.2.jar,commons-pool2-2.0.jar,tomcat-redis-session-manager1.2.jar)放在多个tomcat的lib目录下
  3. 配置两个tomcat/conf/context.xml,增加
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> 
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" 
   host="127.0.0.1" 
   port="6379" 
   database="0" 
   maxInactiveInterval="60" />
  1. 重启两个Tomcat和Nginx
  2. 测试

Springcloud

分布式:形成不同的微服务分开管理
集群:不同服务器对同一个微服务进行服务
步骤
注册服务中心:
创建Maven-pom父工程(依赖于springboot、hutool)
创建注册服务子项目并继承自父工程(依赖spring-cloud-starter-netflix-eureka-server)
在启动类中@EnableEurekaServer
application.yml中配置服务详细信息

server:
  port :8080
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
  application:
  # name: 项目名
    name: eureka-server

运行启动类并访问

数据微服务:
创建注册数据微服务子项目并继承自父工程(依赖spring-cloud-starter-netflix-eureka-client、spring-boot-starter-web)
创建实体类
创建service层
创建控制器层
在启动类中@EnableEurekaServer
application.yml中配置服务详细信息

#   server:
#   port: 因为会启动多个 product-data-service, 所以端口号由用户自动设置,推荐 8001,8002,8003 
spring:
  application:
    name: product-data-service
eureka:
  client:
    serviceUrl:
    #服务中心ip
      defaultZone: http://localhost:8761/eureka/

运行启动类并访问

视图微服务Ribbon:
创建注册数据微服务子项目并继承自父工程(依赖spring-cloud-starter-netflix-eureka-client、spring-boot-starter-web、spring-boot-starter-thymeleaf)
创建实体类
创建Ribbon层(Ribbon 客户端,通过restTemplate访问http://PRODUCT-DATA-SERVICE/products,而product-data-service只是数据服务在eureka注册中心的名称)

@Component
public class ProductClientRibbon {
    @Autowired
    RestTemplate restTemplate;
    public List<Product> listProdcuts() {
        return restTemplate.getForObject("http://PRODUCT-DATA-SERVICE/products",List.class);
    }
}

创建service层从Ribbon层获取数据
创建控制器层
templates中创建html显示数据
在启动类中@EnableEurekaServer、@EnableDiscoveryClient,并且加入RestTemplate实现负载均衡

@Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

application.yml中配置服务详细信息

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: product-view-service-ribbon
  thymeleaf:
    cache: false
    prefix: classpath:/templates/
    suffix: .html
    encoding: UTF-8
    content-type: text/html
    mode: HTML5  

运行启动类并访问

视图微服务Feign:
创建注册数据微服务子项目并继承自父工程(依赖spring-cloud-starter-netflix-eureka-client、spring-boot-starter-web、spring-boot-starter-thymeleaf、spring-cloud-starter-openfeign)
创建实体类
创建Feign层(对Ribbon的封装,使用注解的方式)

@FeignClient(value = "PRODUCT-DATA-SERVICE")
public interface ProductClientFeign {
    @GetMapping("/products")
    public List<Product> listProdcuts();
}

创建service层从Feign层获取数据
创建控制器层
templates中创建html显示数据
在启动类中@EnableFeignClients、@EnableEurekaClient、@EnableDiscoveryClient
application.yml中配置服务详细信息

spring:
  application:
    name: product-view-service-ribbon

运行启动类并访问

服务链路追踪
安装zipkin-server-2.10.1-exec.jar
启动链路追踪服务器:java -jar zipkin-server-2.10.1-exec.jar
启动服务中心 eureka-server
改造数据微服务和视图微服务(增加zipkin的jar包、application.yml中增加spring:zipkin:base-url: http://localhost:9411)
在启动类里配置Sampler抽样策略

@Bean
public Sampler defaultSampler() {
    //ALWAYS_SAMPLE表示持续抽样
	return Sampler.ALWAYS_SAMPLE;
}  

视图微服务去访问数据微服务
打开链路追踪服务器http://localhost:9411/zipkin/dependency/查看

服务器配置:
在git里保存version信息,通过配置服务器把它获取下来,微服务再从配置服务器上取下来
创建注册数据微服务子项目config-server并继承自父工程(依赖spring-cloud-starter-netflix-eureka-client、spring-boot-starter-web、spring-cloud-config-server)
在启动类里配置@EnableConfigServer表示本springboot是配置服务器
application.yml中配置服务详细信息

spring:
  application:
    name: config-server
  cloud:
    config:
      label: master
      server:
        git:
          uri: https://github.com/how2j/springcloudConfig/
          searchPaths: respo
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

客户端配置:
配置客户端可以从配置服务器上获取版本信息
创建注册数据微服务子项目config-server并继承自父工程(依赖spring-cloud-starter-netflix-eureka-client、spring-boot-starter-web、 spring-cloud-starter-config)
bootstrap.yml中配置服务详细信息

spring:
  cloud:
    config:
      label: master
      profile: dev
      discovery:
        enabled:  true
        serviceId:  config-server
  client:
    serviceUrl:
      defaultZone:  http://localhost:8761/eureka/

application.yml中配置服务详细信息:把eureka地址信息删去
创建控制器层
创建Html文件显示结果

RabbitMQ
实现消息广播,达到有配置信息发生改变的时候,广播给多个微服务的效果
在配置客户端的pom中增加消息广播(spring-boot-starter-actuator、spring-cloud-starter-bus-amqp)
bootstrap.yml中配置服务详细信息

spring:
  cloud:
    bus:
      enabled: true
      trace:
        enabled: true
rabbitmq:
  host: localhost
  port: 5672
  username: guest
  password: guest

application.yml新增路径允许访问

management:
  endpoints:
    web:
      exposure:
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"      

新增 rabbitMQ 端口检测
改造服务链路追踪

java -jar zipkin-server-2.10.1-exec.jar --zipkin.collector.rabbitmq.addresses=localhost

断路器:暂时解决当被访问的微服务无法使用hystrix
断路器监控:可视化掌控微服务情况hystrix-dashboard
断路器聚合监控:通过turbine汇聚多个实例进行集群层面的服务监控turbine
网关:解决微服务地址和端口调整变化的问题zuul
上述使用的端口号总结

eureka-server: 8761
product-data-service: 8001,8002,8003
product-view-service-ribbon: 8010
product-view-service-feign: 8012, 8013, 8014
hystrix-dashboard: 8020
turbine: 8021
config-server: 8030
zuul: 8040
zipkin:9411
rabbitMQ: 5672
发布了12 篇原创文章 · 获赞 0 · 访问量 134

猜你喜欢

转载自blog.csdn.net/weixin_42142764/article/details/102568655
今日推荐