(2-2)SpringCloud-服务注册到Eureka Server集群并消费
服务注册到Eureka Server集群
在(2-1)SpringCloue-Eureka实现高可用注册中心中我们搭建好了高可用的Eureka注册中心,下面我们要把服务注册到Eureka Server 的集群中。
步骤一、修改(1-1)SpringCloud-Eureka:服务的注册与发现中 “服务注册与服务发现---注册服务提供者” 中的配置文件
eureka.client.serviceUrl.defaultZone=http://peer2:8752/eureka,http://peer2:8751/eureka
server.port=8877
spring.application.name=service-ha-hello
步骤二、启动集群注册中心
java -jar eureka-server-1.0.0.jar --spring.profiles.active=peer1
java -jar eureka-server-1.0.0.jar --spring.profiles.active=peer2
步骤三、启动服务提供者
java -jar eureka-server-ha-service-1.0.0.jar --server.port=8877
java -jar eureka-server-ha-service-1.0.0.jar --server.port=8878
然后我们看到注册中心控制台已经有两个服务提供者了。说明注册成功了


https://gitee.com/huayicompany/springcloud-learn/tree/master/lesson2/eureka-server-ha-service
Ribbon负载均衡服务消费者实现:
根据 (1-2)SpringCloud:服务的消费者rest+ribbon 中的例子修改。
步骤一、修改ServiceRibbonApplication中的restTemplate()
package org.hope; 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; @SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication { public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
} @Bean
@LoadBalanced//添加了这个注解
RestTemplate restTemplate() {
return new RestTemplate();
} }
步骤二、修改HelloService中的restTemplate
package org.hope.service; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; @Service
public class HelloService { @Autowired
RestTemplate restTemplate; public String hiService(String name) {
//这里不能用这个localhost,否则调用不成功
// return restTemplate.getForEntity("http://localhost:8877/hello?name=" + name, String.class).getBody();
return restTemplate.getForEntity("http://service-ha-hello/hello?name=" + name, String.class).getBody();
} }
步骤三、浏览器中输入http://localhost:8764/hello?name=zhangsan

尝试多请求几次,看两个eureka-server-ha-service的控制台,可以看到两个控制台交替的打印日志。
https://gitee.com/huayicompany/springcloud-learn/tree/master/lesson2/service-ha-ribbon
参考:
[1] 《SpringCloud微服务实战》,电子工业出版社,翟永超
(2-2)SpringCloud-服务注册到Eureka Server集群并消费的更多相关文章
- Spring Cloud之微服务注册到Eureka Server集群
在Spring Cloud之服务注册中心搭建Eureka Server服务注册中⼼ - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)中已经搭建好了Eureka Server集群,本文就利用 ...
- Spring Cloud之微服务注册到Eureka Server集群后访问改造
上篇Spring Cloud之服务注册中心搭建Eureka Server服务注册中⼼ - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)已经已经成功将两个微服务注册到集群中,那么能正常能与注 ...
- SpringCloud系列四:实现Eureka Server的高可用并将应用注册到Eureka Sever集群上
1. 回顾 上一篇博客中,实现了单节点的Eureka Server.Eureka Client会定时连接Eureka Server,获取注册表中的信息并缓存到本地.微服务在消费远程API时总是使用本地 ...
- springcloud(四)-Eureka Server集群
Eureka Server的高可用 这一节我们接着上一节说. 有分布式应用开发经验的朋友应该发现,前文编写的单节点Eureka Server并不适合线上生产环境.Eureka Client会定时连接E ...
- SpringCloud 将服务注册到Eureka Server上
提供好服务生产者: 1.添加spring-cloud-starter-eureka依赖 <dependencyManagement> <dependencies> <de ...
- SpringCloud系列三:将微服务注册到Eureka Server上
1. 回顾 通过上篇博客的讲解,我们知道硬编码提供者地址的方式有不少问题.要想解决这些问题,服务消费者需要一个强大的服务发现机制,服务消费者使用这种机制获取服务提供者的网络信息.不仅如此,即使服务提供 ...
- spring boot 2.0.3+spring cloud (Finchley)1、搭建服务注册和发现组件Eureka 以及构建高可用Eureka Server集群
一 .搭建Eureka 编写Eureka Server 由于有多个spring boot项目,采用maven多module的结构,项目结构如下: 新建一个maven主工程,在主maven的pom文件中 ...
- 0402-服务注册与发现-Eureka Server使用、将服务注册到Eureka server上
一.Eureka Server使用 官方文档地址:http://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud. ...
- 将微服务注册到Eureka Server
一.微服务程序编写 1.在已写好的微服务程序中添加pom依赖: <dependency> <groupId>org.springframework.cloud</grou ...
随机推荐
- Python进阶内容(二)--- 装饰器
谈装饰器前,需要明白一件事,Python 中的函数和 Java.C++不太一样,Python 中的函数可以像普通变量一样当做参数传递给另外一个函数,例如: def foo(): print(" ...
- centos perl: symbol lookup error: /usr/local/lib64/perl5/auto/DBD/mysql/mysql.so: undefined symbol: mysql_init
之前在安装天兔数据库监控工具lepus的时候,运行时一直报perl: symbol lookup error: /usr/local/lib64/perl5/auto/DBD/mysql/mysql. ...
- 《JAVA程序设计与实例》记录与归纳--继承与多态
继承与多态 概念贴士: 1. 继承,即是在已经存在的类的基础上再进行扩展,从而产生新的类.已经存在的类成为父类.超类和基类,而新产生的类成为子类或派生类. 2. Java继承是使用已存在的类的定义作为 ...
- App开发 对生命周期的处理
//获取到当前所在的视图 - (UIViewController *)presentingVC:(UIApplication *)application{ UIWindow * window = ap ...
- Memory Map
计算机最重要的功能单元之一是Memory.Memory是众多存储单元的集合,为了使CPU准确地找到存储有某个信息的存储单元,必须为这些单元分配一个相互区别的“身份证号”,这个“身份证号”就是地址编码. ...
- python网站目录扫描器2.0版
改进了上次乱的要死的. #网站目录扫描器2.0的脚本 import sys import requests import time print('[+]扫描中') def gfc(): f=open( ...
- [bzoj1717][Usaco2006 Dec]Milk Patterns 产奶的模式 (hash构造后缀数组,二分答案)
以后似乎终于不用去学后缀数组的倍增搞法||DC3等blablaSXBK的方法了= = 定义(来自关于后缀数组的那篇国家集训队论文..) 后缀数组:后缀数组SA是一个一维数组,它保存1..n的某个排列S ...
- c与c++d的typedef
一.基本概念剖析 int* (*a[5])(int, char*); //#1 void (*b[10]) (void (*)()); //#2 double(*)() (*pa)[9]; ...
- Linux系统调优权威指南
1.关闭SELINUX功能1.1 修改配置文件,使关闭SELINUX永久生效sed 's#SELINUX=enforcing#SELINUX=disables#g' /etc/selinux/conf ...
- 在.Net中将RocketMQ跑起来_入门篇【2】
上一篇讲了如何再控制台将RocketMQ跑起来,本篇讲解,在asp.net mvc种跑起来,含(发布.订阅). 本次将不挨个贴源码,直接展示目录,根据上一篇文章,进行相应的调整即可. 1.新建一个类库 ...