先建立父工程

..

..一路next

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

..

..

..

修改入口类

  1. package com.cloud.eurekaserver1111;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  6.  
  7. @SpringBootApplication
  8. @EnableEurekaServer
  9. public class EurekaServer1111Application {
  10.  
  11. public static void main(String[] args) {
  12. SpringApplication.run(EurekaServer1111Application.class, args);
  13. }
  14. }

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

  1. server.port=1111
  2.  
  3. eureka.instance.hostname=server.one.com
  4. eureka.client.fetch-registry=false
  5. eureka.client.register-with-eureka=false
  6. eureka.client.service-url.defaultZone=http://server.two.com:2222/eureka,http://server.three.com:3333/eureka

..

  1. server.port=2222
  2.  
  3. eureka.instance.hostname=server.two.com
  4. eureka.client.fetch-registry=false
  5. eureka.client.register-with-eureka=false
  6. eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.three.com:3333/eureka

..

  1. server.port=3333
  2.  
  3. eureka.instance.hostname=server.three.com
  4. eureka.client.fetch-registry=false
  5. eureka.client.register-with-eureka=false
  6. eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka

..

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

  1. 127.0.0.1 server.one.com
  2. 127.0.0.1 server.two.com
  3. 127.0.0.1 server.three.com

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

  1. <parent>
  2. <groupId>com.cloud</groupId>
  3. <artifactId>cloud-parent-two</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. </parent>

 建立服务提供者-8081

..

..

..

修改pom

  1. <parent>
  2. <groupId>com.cloud</groupId>
  3. <artifactId>cloud-parent-two</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. </parent>

修改入口类:

  1. package com.cloud.bookservice8081;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  7.  
  8. @SpringBootApplication
  9. @EnableEurekaClient
  10. @EnableDiscoveryClient
  11. public class BookService8081Application {
  12.  
  13. public static void main(String[] args) {
  14. SpringApplication.run(BookService8081Application.class, args);
  15. }
  16. }

修改属性文件

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

新建controller

  1. package com.cloud.bookservice8081.controller;
  2.  
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.cloud.client.ServiceInstance;
  6. import org.springframework.cloud.client.discovery.DiscoveryClient;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9.  
  10. import java.util.List;
  11.  
  12. @Slf4j
  13. @RestController
  14. public class BookController {
  15.  
  16. @Autowired
  17. private DiscoveryClient discoveryClient;
  18.  
  19. @RequestMapping("/book")
  20. public String index(){
  21. List<String> services = discoveryClient.getServices();
  22. services.forEach(e -> log.info("book-service:8081:" + e));
  23. List<ServiceInstance> list = discoveryClient.getInstances("BOOKSERVICEPROVIDER");
  24. list.forEach(e -> {
  25. log.info("book-service:8081:" + e.getServiceId() + "," + e.getHost() + "," + e.getPort() + "," + e.getUri());
  26. });
  27. return "{\n" +
  28. " \"bookName\": \"Apache Kafka实战\",\n" +
  29. " \"bookSize\": \"16开\",\n" +
  30. " \"pack\": \"平装\",\n" +
  31. " \"isbn\": \"9787121337765\",\n" +
  32. " \"publisher\": \"电子工业出版社\",\n" +
  33. " \"publishTime\": \"2018-05-01\",\n" +
  34. " \"service\": \"book-service:8081\"\n" +
  35. "}";
  36. }
  37. }

建立服务提供者-8082

..

..

修改pom

  1. <parent>
  2. <groupId>com.cloud</groupId>
  3. <artifactId>cloud-parent-two</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. </parent>

修改入口类

  1. package com.cloud.bookservice8082;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  7.  
  8. @SpringBootApplication
  9. @EnableEurekaClient
  10. @EnableDiscoveryClient
  11. public class BookService8082Application {
  12.  
  13. public static void main(String[] args) {
  14. SpringApplication.run(BookService8082Application.class, args);
  15. }
  16. }

修改属性文件

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

增加controller

  1. package com.cloud.bookservice8082.controller;
  2.  
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.cloud.client.ServiceInstance;
  6. import org.springframework.cloud.client.discovery.DiscoveryClient;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9.  
  10. import java.util.List;
  11.  
  12. @Slf4j
  13. @RestController
  14. public class BookController {
  15.  
  16. @Autowired
  17. private DiscoveryClient discoveryClient;
  18.  
  19. @RequestMapping("/book")
  20. public String index(){
  21. List<String> services = discoveryClient.getServices();
  22. services.forEach(e -> log.info("book-service:8082:" + e));
  23. List<ServiceInstance> list = discoveryClient.getInstances("BOOKSERVICEPROVIDER");
  24. list.forEach(e -> {
  25. log.info("book-service:8082:" + e.getServiceId() + "," + e.getHost() + "," + e.getPort() + "," + e.getUri());
  26. });
  27. return "{\n" +
  28. " \"bookName\": \"Apache Kafka实战\",\n" +
  29. " \"bookSize\": \"16开\",\n" +
  30. " \"pack\": \"平装\",\n" +
  31. " \"isbn\": \"9787121337765\",\n" +
  32. " \"publisher\": \"电子工业出版社\",\n" +
  33. " \"publishTime\": \"2018-05-01\",\n" +
  34. " \"service\": \"book-service:8082\"\n" +
  35. "}";
  36. }
  37. }

最后建立消费者-8080

..

..

修改pom

  1. <parent>
  2. <groupId>com.cloud</groupId>
  3. <artifactId>cloud-parent-two</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. </parent>

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

  1. package com.cloud.bookconsumer8080;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  7. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.web.client.RestTemplate;
  10.  
  11. @SpringBootApplication
  12. @EnableDiscoveryClient
  13. @EnableEurekaClient
  14. public class BookConsumer8080Application {
  15.  
  16. @Bean
  17. @LoadBalanced
  18. public RestTemplate restTemplate(){
  19. return new RestTemplate();
  20. }
  21.  
  22. public static void main(String[] args) {
  23. SpringApplication.run(BookConsumer8080Application.class, args);
  24. }
  25. }

属性文件

  1. server.port=8080
  2.  
  3. eureka.client.register-with-eureka=false
  4. eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka,http://server.three.com:3333/eureka

controller

  1. package com.cloud.bookconsumer8080.controller;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import org.springframework.web.client.RestTemplate;
  7.  
  8. @RestController
  9. public class BookController {
  10.  
  11. private static final String PREFIX = "http://BOOKSERVICE"; // 微服务名字
  12.  
  13. @Autowired
  14. private RestTemplate restTemplate;
  15.  
  16. @RequestMapping("consumeBook")
  17. public String index(){
  18. return restTemplate.getForEntity(PREFIX + "/book",String.class).getBody();
  19. }
  20. }

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

目录结构

 下面开始运行

访问 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. Spring标签之Bean @Scope

    @Bean 的用法 @Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里.添加的bean的id为方法名 定义bean 下面是@Co ...

  2. python数据结构与算法第九天【选择排序】

    1.选择排序的原理 2.代码实现 def selection_sort(alist): n = len(alist) # 需要进行n-1次选择操作 for i in range(n-1): # 记录最 ...

  3. H5的使用

    1.h5的部分标签浏览器兼容性问题 解决方案: <head> <!--[if lt IE 9]>  <script src="http://cdn.static ...

  4. Appium之开发环境搭建

    1.下载Appium 去官方网站下载http://appium.io/# : 本次以appium-desktop-setup-1.8.0.exe 文件为例,使用桌面版就不再需要下载server版本: ...

  5. How to blog on Github

    git clone https://github.com/test/test.github.io.git cd ~/test.github.io git config --global push.de ...

  6. Reading Text from Images Using C#

    Introduction By using Optical Character Recognition (OCR), you can detect and extract handwritten an ...

  7. 洛谷P1123取数游戏题解

    题目 这是一道简单的搜索题,考查的还是比较基础的东西,其时搜索有时候并不难写,主要是要想到怎么搜.比如这个题,如果想二维四个方向搜则没有头绪,反之因为搜索是用递归实现的,所以我们可以使用递归的特性,把 ...

  8. [USACO2008 Mar]土地购买

    传送门:>HERE< 题意:购买一组土地的费用是最长的长乘以最长的宽.现给出n块土地,求购买所有土地(可以将土地分为任意组,不需按顺序)的最小费用 解题思路 动态规划+斜率优化 斜率优化在 ...

  9. Quartus prime 16.0 in_system memory content editor 使用

    前言 quartus提供了片内存储器的实时查看与修改,而不用编译工程,很棒.你可以方便的查看到存储器中到底存储了什么东西. 流程 1.打开: 2.主界面: 3.设置jtag项之后,查看即可. sign ...

  10. linux命令:查看系统版本

    debian系统 (1)  lsb_release -a No LSB modules are available. Distributor ID: Debian Description: Debia ...