java版b2b2c社交电商spring cloud分布式微服务(二) 服务消费者(rest+ribbon)
一、ribbon简介
Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.
—–摘自官网
ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。Feign默认集成了ribbon。
ribbon 已经默认实现了这些配置bean:
IClientConfig ribbonClientConfig: DefaultClientConfigImpl
IRule ribbonRule: ZoneAvoidanceRule
IPing ribbonPing: NoOpPing
ServerList ribbonServerList: ConfigurationBasedServerList
ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter
ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer
二、准备工作
这一篇文章基于上一篇文章的工程,启动eureka-server 工程;启动service-hi工程,它的端口为8762;将service-hi的配置文件的端口改为8763,并启动,这时你会发现:service-hi在eureka-server注册了2个实例,这就相当于一个小的集群。访问localhost:8761如图所示:
三、建一个服务消费者
重新新建一个spring-boot工程,取名为:service-ribbon;
在它的pom.xml文件分别引入起步依赖spring-cloud-starter-eureka、spring-cloud-starter-ribbon、spring-boot-starter-web,代码如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.forezp</groupId><artifactId>service-ribbon</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>service-ribbon</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-ribbon</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.RC1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>
在工程的配置文件指定服务的注册中心地址为http://localhost:8761/eureka/,程序名称为 service-ribbon,程序端口为8764。配置文件application.yml如下:
eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/ server:port: 8764 spring:application:name: service-ribbon
在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
@SpringBootApplication @EnableDiscoveryClient public class ServiceRibbonApplication {public static void main(String[] args) {SpringApplication.run(ServiceRibbonApplication.class, args);}@Bean@LoadBalancedRestTemplate restTemplate() {return new RestTemplate();}}
写一个测试类HelloService,通过之前注入ioc容器的restTemplate来消费service-hi服务的“/hi”接口,在这里我们直接用的程序名替代了具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名,代码如下:
@Service public class HelloService {@AutowiredRestTemplate restTemplate;public String hiService(String name) {return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);}}
写一个controller,在controller中用调用HelloService 的方法,代码如下:
/*** Created by fangzhipeng on 2017/4/6.*/ @RestController public class HelloControler {@AutowiredHelloService helloService;@RequestMapping(value = "/hi")public String hi(@RequestParam String name){return helloService.hiService(name);}}
在浏览器上多次访问http://localhost:8764/hi?name=forezp,浏览器交替显示:
hi forezp,i am from port:8762hi forezp,i am from port:8763
这说明当我们通过调用restTemplate.getForObject(“http://SERVICE-HI/hi?name=“+name,String.class)方法时,已经做了负载均衡,访问了不同的端口的服务实例。
代码架构如下:
需要JAVASpring Cloud大型企业分布式微服务云构建的B2B2C电子商务社交平台源码请加企鹅求求:一零三八七七四六二六
转载于:https://www.cnblogs.com/sunnysunny/p/10819551.html
java版b2b2c社交电商spring cloud分布式微服务(二) 服务消费者(rest+ribbon)相关推荐
- java版b2b2c社交电商spring cloud分布式微服务-服务提供与调用
java版b2b2c社交电商spring cloud分布式微服务源码请加企鹅求求:一零三八七七四六二六.上一篇文章我们介绍了eureka服务注册中心的搭建,这篇文章介绍一下如何使用eureka服务注册 ...
- java版b2b2c社交电商spring cloud分布式微服务:服务消费(Ribbon)
Spring Cloud Ribbon 电子商务社交平台源码请加企鹅求求:一零三八七七四六二六Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具.它 ...
- (十二)java版b2b2c社交电商spring cloud分布式微服务:使用Spring Cloud Sleuth和Zipkin进行分布式链路跟踪...
Spring Cloud Sleuth Spring cloud b2b2c电子商务社交平台源码请加企鹅求求:一零三八七七四六二六.一般的,一个分布式服务跟踪系统,主要有三部分:数据收集.数据存储和数 ...
- java版b2b2c社交电商spring cloud分布式微服务(七)springboot开启声明式事务
java b2b2c 电子商务社交平台源码请加企鹅求求:一零三八七七四六二六.springboot开启事务很简单,只需要一个注解@Transactional 就可以了.因为在springboot中已经 ...
- (十)java版b2b2c社交电商spring cloud分布式微服务- SSO单点登录之OAuth2.0登录认证(1)...
之前写了很多关于spring cloud的文章,今天我们对OAuth2.0的整合方式做一下笔记,首先我从网上找了一些关于OAuth2.0的一些基础知识点,帮助大家回顾一下知识点:Spring Clou ...
- java版b2b2c社交电商spring cloud分布式微服务(八)springboot整合mongodb
电子商务社交平台源码请加企鹅求求:一零三八七七四六二六 准备工作 安装 MongoDB jdk 1.8 maven 3.0 idea 环境依赖 在pom文件引入spring-boot-starter- ...
- java版b2b2c社交电商spring cloud分布式微服务(十)高可用的服务注册中心
电子商务社交平台源码请加企鹅求求:一零三八七七四六二六.文章 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka) 介绍了服务注册与发现,其中服务注册中心Eure ...
- java版b2b2c社交电商spring cloud分布式微服务(五)springboot整合 beatlsql
电子商务社交平台源码请加企鹅求求:三五三六二四七二五九.BeetSql是一个全功能DAO工具, 同时具有Hibernate 优点 & Mybatis优点功能,适用于承认以SQL为中心,同时又需 ...
- java spring cloud版b2b2c社交电商spring cloud分布式微服务:分布式配置中心
JAVASpring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码请加企鹅求求:一零三八七七四六二六.Spring Cloud Config是Spring Cloud团队创建的一个 ...
最新文章
- win下ubuntu安装mysql_MySQL在Win10与Ubuntu下的安装与配置
- 干掉Postman?测试接口直接生成API文档,这工具强烈推荐!
- python基础语法 第0关print-温州文成高校邦数据科学通识课【Python基础语法】答案...
- 程序员面试100题之七:最长公共子字符串
- android天气查询(一)websevice之ksoap2软件包的使用
- python 美化输出_python基础_格式化输出(%用法和format用法)
- 倒叙输出 php,php foreach正序倒序输出示例代码
- 大数据学习要知道的十大发展趋势,以及学习大数据的几点建议
- Android 多级树形菜单
- [Vue.js]实战 -- 电商项目(三)
- flask 配置静态文件模板文件
- Flask cookie
- @RequestParam接收解析不到 POST 提交的 数据
- html的代码作用域,JavaScript作用域的全面解析(附代码)
- Sqlite加密问题
- 『Java安全』tabby代码审计工具Windows环境搭建
- 此beta版已额满_《魔域口袋版》福利狂欢:现金红包天天送 魔石神器免费拿
- Ubuntu 命令行 安装 Operator Mono 字体
- 不可能得到的最短骰子序列
- 计算机与网络五十四所有版面费吗,网络与信息化中心财务管理办法(暂行)
热门文章
- jquery上传图片_文件上传三种方式
- opencv python教程简书_Python-OpenCV —— 基本操作一网打尽
- java图书管理系统技术难度_Java图书管理系统练习程序(一)
- python生成相似句子_4种方法计算句子相似度
- python集合的基本操作不包括_Python基础知识储备,List集合基本操作大盘点
- oracle loop index,oracle index 聚集因子
- GPU Gems1 - 22 颜色控制(Color Controls)
- 3.顶点外扩方法实现的描边shader
- java调用keras theano模型_使用Keras获得模型输出的梯度w.r.t权重
- Jenkins 安装与使用--实例