本例子主要使用了eureka集群作为注册中心来保证高可用,客户端来做ribbon服务提供者的负载均衡。

负载均衡有两种,第一种是nginx,F5这种集中式的LB,对所有的访问按照某种策略分发。

第二种是客户端知道所有的服务的地址,在客户端做负载均衡,客户端自己去发送。

github地址:https://github.com/linjiaqin/scdemo

一. eureka注册中心的搭建

1.为了实现HA(高可用性),采用集群方式搭建,由mu01(192.168.0.100),cu01(192.168.0.1), cu02(192.168.0.2)三台机子组成

为了不写ip地址而使用主机名表示,首先修改/etc/hosts文件,将对应的hostname和ip写入该文件,然后source生效

2.idea新建一个module,spring assistant类型,选择springcloud,web和eureka server两个模板。

为其启动类加上@EnableEurekaServer

package com.ljq;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer
@SpringBootApplication
public class ServerApplication {public static void main(String[] args) {SpringApplication.run(ServerApplication.class, args);}}

3.编写配置文件

配置文件application-backone.properties

#服务名和服务暴露的接口
spring.application.name=backup_one
server.port=8762eureka.instance.hostname=cu01
#本项目是注册中心所以不需要自己向自己注册和检索服务,如果是eureka集群就要
#eureka.client.register-with-eureka=false
#eureka.client.fetch-registry=false#eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
# 默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。
eureka.client.serviceUrl.defaultZone=http://mu01:8761/,http://cu02:8763/

配置文件application-backtwo.properties

#服务名和服务暴露的接口
spring.application.name=backup_two
server.port=8763eureka.instance.hostname=cu02
#本项目是注册中心所以不需要自己向自己注册和检索服务,如果是eureka集群就要
#eureka.client.register-with-eureka=false
#eureka.client.fetch-registry=false#eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
# 默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。
eureka.client.serviceUrl.defaultZone=http://mu01:8761/,http://cu01:8762/

配置文件application-master.properties

#服务名和服务暴露的接口
spring.application.name=master
server.port=8761eureka.instance.hostname=mu01
#本项目是注册中心所以不需要自己向自己注册和检索服务,如果是eureka集群就要
#eureka.client.register-with-eureka=false
#eureka.client.fetch-registry=false#eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
# 默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。
eureka.client.serviceUrl.defaultZone=http://cu01:8762/,http://cu02:8763/

4.如果在本机模拟的话,在idea中打开3个terminal,分别执行下面的命令,开启了eureka集群,后面那里不需要使用配置文件的全名

mvn spring-boot:run -Dspring.profiles.active=master
mvn spring-boot:run -Dspring.profiles.active=backone
mvn spring-boot:run -Dspring.profiles.active=backtwo

5.在集群中开启的话,为了不每一次都登录到多台机子上,写一个一键启动脚本,自动ssh到每台机子上去开启。

这里bash脚本是linux脚本,sh脚本是unix脚本,虽然通用,但是语法有些不同。

因为是阻塞式进程,所以使用nohup … > /dev/null,使得该进程能在后台执行。

同时ssh之后还要export javahome/bin,不知为什么这么玄学,ssh之后用的不是该用户环境变量之中的java

#!/bin/bash
echo "start eureka server..."
num=0
server=("master" "backone" "backtwo")
for host in mu01 cu01 cu02
do
echo ${server[$num]}
ssh $host "export JAVA_HOME=/data/home/hadoop/jdk1.8.0_40;
CLASSPATH=.:$JAVA_HOMElib/tools.jar:$JAVA_HOME/lib/dt.jar;
export CLASSPATH;
PATH=$JAVA_HOME/bin:$PATH;
export PATH;
java -version;
cd /software/home/linjiaqing/eureka;
nohup java -jar server-0.0.1-SNAPSHOT.jar --spring.profiles.active=${server[$num]} > /dev/null"
num=$(($num+1))
done

打开浏览器,下面是效果图,每个节点都能看到备份

二. 服务提供者

1.引导类加上注解EnableEurekaClient,说明他是一个服务提供者

package com.ljq;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
//服务提供者用这个注解
public class ProviderApplication {public static void main(String[] args) {SpringApplication.run(ProviderApplication.class, args);}}

2.Controller

package com.ljq;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ljqController {@Value("${server.port}")String port;@RequestMapping("/")public String home(){return "Hello world, port is:" + port;}
}

3.配置文件

spring.application.name=eureka-client-service-provider
server.port=20001
eureka.client.serviceUrl.defaultZone=http://mu01:8761/eureka,http://cu01:8762/eureka,http://cu02:8763/eureka

4.启动,IDEA打开三个客户端,分别使用命令mvn spring-boot:run -Dserver.port=20001,20002,20003如下图可以看到它去eureka的某个节点上注册了


5.HA的体现,将cu01的进程kill之后,发现服务会自动去mu01上注册

三 服务消费者使用负载均衡

1.ljqConfig

这里是一个beanconfig,为了RestTemplate能够被只用

package com.ljq;import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;@Configuration
public class ljqConfig {@Bean@LoadBalancedpublic RestTemplate restTemplate(){return new RestTemplate();}
}

2.ljqController

<span style="font-size: 18px;">使用服务提供者的名字http://eureka-client-service-provider/,不用再使用地址</span>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.ljq;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 ljqController {@Autowiredprivate RestTemplate restTemplate;//这里不写eureka的注册中心,而是写服务提供者的应用名@GetMapping(value = "/hello")public String hello(){return restTemplate.getForEntity("http://eureka-client-service-provider/", String.class).getBody();}
}

3.引导类的EnableDiscoveryClient注解表明是消费者

package com.ljq;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class, args);}}

4.配置文件

spring.application.name=ribbon-consumer
server.port=30001
eureka.client.serviceUrl.defaultZone=http://mu01:8761/eureka,http://cu01:8762/eureka,http://cu02:8763/eureka

5.消费者启动后也去eureka中注册了

可以看到已经有了ribbon负载均衡的效果

加java高级架构师群获取Java工程化、高性能及分布式、高性能、深入浅出。高架构。
性能调优、Spring,MyBatis,Netty源码分析和大数据等多个知识点高级进阶干货的直播免费学习权限
都是大牛带飞 让你少走很多的弯路的 群号是: 798891710对了 小白勿进 最好是有开发经验

SpringCloud分布式微服务搭建(一)相关推荐

  1. SpringCloud 分布式微服务架构

    SpringCloud 分布式架构 前言 SpringCloud微服务 单体架构和微服务分布式架构 单体架构分析 微服务分布式架构分析 服务拆分和远程调用 服务拆分 案例需求准备 远程调用初步 Eur ...

  2. java版b2b2c社交电商springcloud分布式微服务 (九)服务链路追踪(Spring Cloud Sleuth)...

    电子商务社交平台源码请加企鹅求求:一零三八七七四六二六.这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 一.简介 Spring cloud B ...

  3. 基于SpringCloud分布式微服务+微信小程序实现短视频社交app设计

    开发软件: Idea + 微信web开发者工具 + mysql5.6 + redis 1 微信小程序端 (1)用户信息模块:包含:注册.登陆.发布短视频.编辑短视频,其中编辑视频包含为视频增加背景音乐 ...

  4. 企业分布式微服务云SpringCloud SpringBoot mybatis (十一)docker部署spring cloud项目

    一.docker简介 Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs(虚拟机). ...

  5. (十六)java springcloud版b2b2c社交电商spring cloud分布式微服务-使用spring cloud Bus刷新配置...

    b2b2c电子商务社交平台源码请加企鹅求求:一零三八七七四六二六.我们使用spring cloud分布式微服务云架构做了b2b2c的电子商务系统,除了架构本身自带的系统服务外,我们将b2b2c的业务服 ...

  6. java springcloud版b2b2c社交电商spring cloud分布式微服务 (七)高可用的分布式配置中心(Spring Cloud Config)...

    Springcloud b2b2c电子商务社交平台源码请加企鹅求求:一零三八七七四六二六.讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取 ...

  7. java springcloud版b2b2c社交电商spring cloud分布式微服务-docker-feign(四)

    简介 Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码请加企鹅求求:一零三八七七四六二六.上一节,我们讨论了怎么通过,restTemlate调用cloud的生产者,实现起 ...

  8. SpringCloud干货(2)---------大时代下的分布式微服务

    2019独角兽企业重金招聘Python工程师标准>>> 在学习SpringCloud之前,需要弄清楚SpringCloud是什么,他能够解决什么问题.另外,还需要区分一些具有混淆性的 ...

  9. springcloud和分布式微服务学习笔记

    1.什么是SpringCloud Spring cloud 流应用程序启动器是基于 Spring Boot 的 Spring 集成应用程序,提供与外部系统的集 成.Spring cloud Task, ...

最新文章

  1. 使用分支限界法解决单源最短路径问题。
  2. 手机照片导入电脑步骤_如何将手机中的照片、视频快速的保存到U盘上?3分钟教你详细步骤...
  3. [arm驱动]linux内核中断编程
  4. php 实现百度坐标转换,PHP实现腾讯与百度坐标转换
  5. java做 binggo,Linux启动与停止spring boot工程的脚本示例
  6. bundle中vim相关快捷键的使用
  7. 【渝粤教育】国家开放大学2018年春季 3819-21T燃气安全管理 参考试题
  8. Swift 委托/代理设计模式
  9. 计算机系统结构概念,计算机系统结构的基本概念
  10. 微信小程序开发常用代码
  11. JVM常见面试题及详解
  12. 电机驱动详解--从原理到智能车驱动(DRV8701)
  13. javax.persistence.EntityNotFoundException: Unable to find报错
  14. 墨迹天气灰白色风格Discuz模板源码
  15. AGV (Automated guided vehicle)基础(一) - AGV的导航种类
  16. 操作系统简史(1)东方会有新的操作系统诞生吗?让历史告诉未来
  17. 阿里云9块5主机的“开箱作业”
  18. 华为VRRP双机热备(基于接口设置热备)
  19. chrome同步书签实现
  20. 银豹收银系统零费率怎么切

热门文章

  1. 在jupyter安装jieba出错ModuleNotFoundError: No module named ‘jieba‘的解决办法
  2. 小葫芦直播管家找不到服务器,小葫芦直播管家-开播版,直播插件手动安装教程...
  3. 入职以来一个月的心得体悟
  4. 《C语言程序设计》第五版谭浩强课后答案 第九章《用户自己建立数据类型​》习题答案 (大一大二、考研、计算机二级必看)
  5. 第一次当管理时,为啥会有很强的挫败感?
  6. Linux Centos7.6下安装zsh、oh-my-zsh、powerlevel10k美化终端
  7. DeepMotion自动驾驶高精度地图采集模块
  8. Vue入门(个人理解)
  9. 【sqoop2】创建job报错There are issues with entered data, please revise your input
  10. **在一个字符串中寻找另外一个字符串**