先建立父工程

..

..一路next

搭建注册中心(需要建立三个工程,端口不一样)

..

..

..

修改入口类

package com.cloud.eurekaserver1111;

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

修改属性文件(一共建立三个Eureka-Server服务,端口分别为1111,2222,3333)

server.port=1111

eureka.instance.hostname=server.one.com
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://server.two.com:2222/eureka,http://server.three.com:3333/eureka

..

server.port=2222

eureka.instance.hostname=server.two.com
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.three.com:3333/eureka

..

server.port=3333

eureka.instance.hostname=server.three.com
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka

..

修改hosts文件(C:\Windows\System32\drivers\etc)

127.0.0.1 server.one.com
127.0.0.1 server.two.com
127.0.0.1 server.three.com

修改pom文件,把parent改成父工程

    <parent>
<groupId>com.cloud</groupId>
<artifactId>cloud-parent-two</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

 建立服务提供者-8081

..

..

..

修改pom

    <parent>
<groupId>com.cloud</groupId>
<artifactId>cloud-parent-two</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

修改入口类:

package com.cloud.bookservice8081;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class BookService8081Application { public static void main(String[] args) {
SpringApplication.run(BookService8081Application.class, args);
}
}

修改属性文件

server.port=8081
# 服务名
spring.application.name=BookService # 注册地址
eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka,http://server.three.com:3333/eureka
# 注册名
eureka.instance.instance-id=book-service:8081
eureka.instance.prefer-ip-address=true

新建controller

package com.cloud.bookservice8081.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @Slf4j
@RestController
public class BookController { @Autowired
private DiscoveryClient discoveryClient; @RequestMapping("/book")
public String index(){
List<String> services = discoveryClient.getServices();
services.forEach(e -> log.info("book-service:8081:" + e));
List<ServiceInstance> list = discoveryClient.getInstances("BOOKSERVICEPROVIDER");
list.forEach(e -> {
log.info("book-service:8081:" + e.getServiceId() + "," + e.getHost() + "," + e.getPort() + "," + e.getUri());
});
return "{\n" +
" \"bookName\": \"Apache Kafka实战\",\n" +
" \"bookSize\": \"16开\",\n" +
" \"pack\": \"平装\",\n" +
" \"isbn\": \"9787121337765\",\n" +
" \"publisher\": \"电子工业出版社\",\n" +
" \"publishTime\": \"2018-05-01\",\n" +
" \"service\": \"book-service:8081\"\n" +
"}";
}
}

建立服务提供者-8082

..

..

修改pom

    <parent>
<groupId>com.cloud</groupId>
<artifactId>cloud-parent-two</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

修改入口类

package com.cloud.bookservice8082;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class BookService8082Application { public static void main(String[] args) {
SpringApplication.run(BookService8082Application.class, args);
}
}

修改属性文件

server.port=8082
# 服务名
spring.application.name=BookService # 注册地址
eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka,http://server.three.com:3333/eureka
# 注册名
eureka.instance.instance-id=book-service:8082
eureka.instance.prefer-ip-address=true

增加controller

package com.cloud.bookservice8082.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @Slf4j
@RestController
public class BookController { @Autowired
private DiscoveryClient discoveryClient; @RequestMapping("/book")
public String index(){
List<String> services = discoveryClient.getServices();
services.forEach(e -> log.info("book-service:8082:" + e));
List<ServiceInstance> list = discoveryClient.getInstances("BOOKSERVICEPROVIDER");
list.forEach(e -> {
log.info("book-service:8082:" + e.getServiceId() + "," + e.getHost() + "," + e.getPort() + "," + e.getUri());
});
return "{\n" +
" \"bookName\": \"Apache Kafka实战\",\n" +
" \"bookSize\": \"16开\",\n" +
" \"pack\": \"平装\",\n" +
" \"isbn\": \"9787121337765\",\n" +
" \"publisher\": \"电子工业出版社\",\n" +
" \"publishTime\": \"2018-05-01\",\n" +
" \"service\": \"book-service:8082\"\n" +
"}";
}
}

最后建立消费者-8080

..

..

修改pom

    <parent>
<groupId>com.cloud</groupId>
<artifactId>cloud-parent-two</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

修改入口类(配置了@LoadBalanced注解的Bean

package com.cloud.bookconsumer8080;

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.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableDiscoveryClient
@EnableEurekaClient
public class BookConsumer8080Application { @Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(BookConsumer8080Application.class, args);
}
}

属性文件

server.port=8080

eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka,http://server.three.com:3333/eureka

controller

package com.cloud.bookconsumer8080.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @RestController
public class BookController { private static final String PREFIX = "http://BOOKSERVICE"; // 微服务名字 @Autowired
private RestTemplate restTemplate; @RequestMapping("consumeBook")
public String index(){
return restTemplate.getForEntity(PREFIX + "/book",String.class).getBody();
}
}

与上一节不同,这次指向的是服务名

目录结构

 下面开始运行

访问 http://server.one.com:1111/ 出现

可以看见当前Eureka连接另外两个Eureka,证明注册中心高可用集群搭建成功。

再看下面的服务,有两个,证明服务已经注册进来了

下面访问消费者 http://localhost:8080/consumeBook

刷新

证明负载均衡也成功了

GitHub

SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)的更多相关文章

  1. 高性能Linux服务器 第11章 构建高可用的LVS负载均衡集群

    高性能Linux服务器 第11章 构建高可用的LVS负载均衡集群 libnet软件包<-依赖-heartbeat(包含ldirectord插件(需要perl-MailTools的rpm包)) l ...

  2. 高可用 & 七层负载均衡与四层负载均衡

    内容概要 高可用 七层负载均衡 和 四层负载均衡 内容详细 一.高可用 1.什么是高可用 一般是指2台机器启动着完全相同的业务系统,当有一台机器down机了,另外一台服务器就能快速的接管,对于访问的用 ...

  3. SpringCloud无废话入门02:Ribbon负载均衡

    1.白话负载均衡 在上一篇的介绍中,我们创建了两个一模一样的服务提供者:Provider1和Provider2,然后它们提供的服务也一模一样,都叫Hello-Service.为什么一样的服务我们要部署 ...

  4. SpringCloud学习笔记(2):使用Ribbon负载均衡

    简介 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡工具,在注册中心对Ribbon客户端进行注册后,Ribbon可以基于某种负载均衡算法,如轮询(默认 ...

  5. SpringCloud微服务实现生产者消费者+ribbon负载均衡

    一.生产者springcloud_eureka_provider (1)目录展示 (2)导入依赖 <dependency> <groupId>org.springframewo ...

  6. keepalived+nginx实现HA高可用的web负载均衡

    Keepalived 是一种高性能的服务器高可用或热备解决方案, Keepalived 可以用来防止服务器单点故障的发生,通过配合 Nginx 可以实现 web 前端服务的高可用.Keepalived ...

  7. Keepalived高可用、四层负载均衡

    目录 Keepalived高可用 高可用简介 常用的工具 问题 名称解释 VRRP协议 部署keepalived 下载安装 Keepalived配置 保证nginx配置一样 解决keepalived的 ...

  8. nginx与keepalived实现高可用+Apache实现负载均衡

    nginx与keepalived实现高可用 本实验使用了四台虚拟机 两台需要安装nginx及keepalived 两台安装Apache nginx可以源码安装也可以用yum安装nginx yum安装n ...

  9. SpringCloud系列五:Ribbon 负载均衡(Ribbon 基本使用、Ribbon 负载均衡、自定义 Ribbon 配置、禁用 Eureka 实现 Ribbon 调用)

    1.概念:Ribbon 负载均衡 2.具体内容 现在所有的服务已经通过了 Eureka 进行了注册,那么使用 Eureka 注册的目的是希望所有的服务都统一归属到 Eureka 之中进 行处理,但是现 ...

随机推荐

  1. DAY07、字符编码和文件操作

    一.字符编码 1.什么是字符编码? 人类能识别的是字符等高级标识符,电脑只能识别0,1组成的标识符,要完成人与机器之间的信息交流,              一定需要一个媒介,进行两种标识符的转化(两 ...

  2. Python——Flask框架——Web表单

    一.框架Flask-WTF 安装: pip install flask-wtf 需要程序设置一个密钥 app = Flask(__name__) app.config['SECRET_KEY'] = ...

  3. C#里XML(JSON)序列化时,自动隐藏值为Null的成员的输出

    从StackOverflow里找到的答案.发现对最新的Newtownsoft的JSON序列化也同样适用. https://stackoverflow.com/questions/5818513/xml ...

  4. react 入坑笔记(四) - React 事件绑定和传参

    React 事件处理 建议:在了解 js 的 this 取值后食用更佳. 一.react 与 Html 中用法的异同和注意点 html 中的绑定事件的写法: <button onclick=&q ...

  5. sun.misc.BASE64Encoder----》找不到jar包的解决方法

    1.右键项目->属性->java bulid path->jre System Library->access rules->resolution选择accessible ...

  6. ASP.NET Core 2.0 Cookie Authentication

    using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Builder; using Microso ...

  7. Ubuntu16.04系统美化、常用软件安装等,长期更新

    Ubuntu16.04系统美化.常用软件安装等,长期更新 IT之家啊 18-09-0915:00 因为我个人偏向于玩VPS.服务器之类的东西,所以一般我都是用CentOS.不过对于桌面版的Linux, ...

  8. hashlib 模块用来进行hash

    hashlib的基本概述: python中的 hashlib 模块用来进行hash 或者md5加密,而且这种加密是不可逆的,所以这种算法又被称为摘要算法, 其支持Opennssl库提供的所有算法,包括 ...

  9. ☆ [POJ1021] Intervals 「差分约束」

    传送门 >Here< 题意:给出N段区间,并告诉你每段区间里有几个数(一个位置只能放一个数) 问总共至少有几个数 解题思路 差分约束题,本蒟蒻也是第一次做差分约束题…… 所谓差分约束,常常 ...

  10. 【洛谷U20626】gemo 容斥 FWT 高斯消元

    题目大意 给你一个无向图,有\(m\)个询问,每次给你一个点\(x\)和一个点集\(S\),问你从\(x\)开始走,每次从一个点随机的走到与这个点相邻的点,问你访问\(S\)中每个点至少一次的期望步数 ...