Spring Cloud 学习笔记(三) 之服务治理模块Spring Cloud 服务发现与消费
2019独角兽企业重金招聘Python工程师标准>>>
一、前言
前面的文章我们已经讲了如何搭建服务注册中心,如何搭建客户端去注册。接下来我们讲一下服务的发现与消费
二、服务注册
1.首先下载服务注册中心源码
2.项目源码请到 : https://github.com/mrg1511104848/my-eureka-server.git下载
3.导入到idea
4.运行服务注册中心
------------------------------------------------------------------------------------------
1.首先下载客户端源码
2.项目源码请到 : https://github.com/mrg1511104848/springboot-study下载
3.导入到 idea
4.通过idea 的 maven package 功能,将源码打包成 jar
5.通过命令行启动两个客户端
java -jar xxx.jar --server.port=9998
java -jar xxx.jar --server.port=9999
------------------------------------------------------------------------------------------
三、服务消费
这里的消费我们用Ribbon
1.概念:
Ribbon : 首先我们看以下spring官方怎么解释的
Ribbon is a client-side load balancer that gives you a lot of control over the behavior of HTTP and TCP clients
功能区是一个客户端负载均衡器,可以让您对HTTP和TCP客户端的行为有很多控制权。
2.如何引用Ribbon
a.创建项目
首先复制一下本地的spring-boot项目,将其中的git信息删除。改一下名 叫做ribbon-customer。
b.添加依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency>
c.使用
创建主类
package com.myspringboot;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;@EnableDiscoveryClient
@SpringBootApplication
public class StudyApplication {@Bean@LoadBalancedRestTemplate restTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(StudyApplication.class, args);}}
加入 @EnableDiscoveryClient
加入 restTemplate 用于获取负载均衡的一个管理对象
d.创建Controller,消费其他服务。
package com.myspringboot.web;import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.hypermedia.DiscoveredResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import java.util.List;@RestController
public class CustomerController {@Autowiredprivate RestTemplate restTemplate;@RequestMapping(value="/ribbon-consumer",method=RequestMethod.GET)public String helloConsumer(){return restTemplate.getForEntity("http://HELLO-SERVICE/hello",String.class).getBody();}}
这里通过调用restTemplate获取需要消费的服务。并消费。
e.修改配置
src\main\resources 下的application.yml 修改 server.port = 9898
、 spring.application.name=ribbon-consumer
f.访问刚创建的Controller,看看结果和我们预想是否一致
http://localhost:9898/ribbon-consumer。
浏览器输出
证明服务消费成功,成功调用了其他的几个服务。
四、总结
本章主要讲了如何Ribbon的概念,以及如何通过Ribbon进行服务的消费。
如果对本章有疑问,欢迎留言。
关于本章的源码已经更新至github,欢迎下载和star
https://github.com/mrg1511104848/ribbon-consumer
转载于:https://my.oschina.net/u/1178126/blog/1816323
Spring Cloud 学习笔记(三) 之服务治理模块Spring Cloud 服务发现与消费相关推荐
- Spring框架学习笔记(三)(AOP,事务管理)
Spring框架学习笔记(三) 九.AOP 9.1 AOP的注解配置 (1) 新建计算器核心功能(模拟:不能在改动核心代码) (2) 建立一个普通的Java类写增强代码(面向切面编程),使用Sprin ...
- Spring Cloud 学习笔记(一) 之服务治理模块Spring Cloud Eureka 搭建注册中心
2019独角兽企业重金招聘Python工程师标准>>> 关于springboot的学习请参考前面的文章 接下来我们会开启一系列关于springcloud的学习文章. 一.概念 首先我 ...
- springboot学习笔记(三)使用JDBC以及整合spring data jpa
spring boot JDBC的使用: 1.引入依赖 <dependency><groupId>mysql</groupId><artifactId> ...
- Spring Cloud 学习笔记(2 / 3)
Spring Cloud 学习笔记(1 / 3) Spring Cloud 学习笔记(3 / 3) - - - 56_Hystrix之全局服务降级DefaultProperties 57_Hystri ...
- Spring Cloud 学习笔记(2 3)
Spring Cloud 学习笔记(1 / 3) Spring Cloud 学习笔记(3 / 3) - - - 56_Hystrix之全局服务降级DefaultProperties 57_Hystri ...
- Spring Cloud 学习笔记一 微服务架构
文章目录 前言 一.微服务 二.微服务的特点 1.小型化 2.自治化 3. 扁平化 4. 轻量级设计 5. 渐进式设计 微服务架构和整体式架构的区别 微服务架构和SOA的比较 区别一:微服务通信的轻量 ...
- Spring Cloud 学习笔记(1 / 3)
Spring Cloud 学习笔记(2 / 3) Spring Cloud 学习笔记(3 / 3) - - - 01_前言闲聊和课程说明 02_零基础微服务架构理论入门 03_第二季Boot和Clou ...
- Spring Cloud学习笔记
Spring Cloud学习笔记 相关代码地址:https://github.com/gongxings/spring-cloud-study.git 一.工程环境搭建 spring cloud版本: ...
- Spring Cloud学习笔记【十二】Hystrix的使用和了解
Spring Cloud学习笔记[十二]Hystrix的使用和了解 Hystrix [hɪst'rɪks],中文含义是豪猪,因其背上长满棘刺,从而拥有了自我保护的能力.本文所说的Hystrix是Net ...
最新文章
- hashMap与arrayList,linkedList,hashTable的区别
- ubuntu9.10安装Tweak
- 供配电负荷计算方法详解
- oracle 得到一个树,Related to Oracle SQL 关于树形数据的遍历
- 快看Sample代码,速学Swift语言(3)-运算符
- FTP 协议和 HTTP 协议的比较
- 未来我们需要一辆什么样的智能汽车?
- linux一些杂项整理
- 用vue实现简单实时汇率计算功能
- linux下mysql用户_linux下mysql用户管理方法
- word2vec的理解
- 【210518】印象笔记 Windows 安装升级出现错误 “找不到msi文件” 解决办法
- FGUI使用方法(四):List列表的详细使用方式,包括List嵌套List
- linux连接交换机软件,如何用超级终端连接交换机 - 全文
- Scrapy0.24.1_中文文档
- 外部联接(Outer Join)和笛卡尔积(Cartesian Product)
- 夏天吃海鲜的八大禁忌
- YY语音无有效验证导致下载执行任意程序
- 煤气炉的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
- 2020研究生数模竞赛思路
热门文章
- Codeforces Round #172 (Div. 2) C. Rectangle Puzzle 数学题几何
- BCB 连接数据库和查询数据
- VC++6.0 内存泄露(Memory Leaks)征兆1
- ES6新特性_ES6扩展运算符的应用---JavaScript_ECMAScript_ES6-ES11新特性工作笔记014
- 大数据_Hbase-Filter 索引(优化)_根据column查询---Hbase工作笔记0020
- Netty工作笔记0033---Netty概述
- STM32工作笔记0051---NVIC中断优先级管理
- springcloud工作笔记099---springboot集成netty,进行线程管理,socket通讯
- memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
- relu 里面的是激活函数