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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/251820.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[学习笔记]支配树

被支配树支配的恐惧 定义 显然&#xff0c;这个支配关系是一个树&#xff08;或者如果有的点不能从r到达&#xff0c;就是一个树一堆点&#xff09;。 首先不会成环&#xff0c;其次也不会是DAG 即如果A支配C&#xff0c;B支配C&#xff0c;那么A和B之间必然有支配关系 解法 首…

RBAC 权限设计(转载)

来源 &#xff1a;https://blog.csdn.net/rocher88/article/details/43190743 这是我在网上找的一些设计比较好的RBAC权限管理不知道&#xff0c;像新浪、搜狐、网易、百度、阿里巴巴、淘宝网的RBAC用户权限这一块&#xff0c;都是这种细颗粒的RBAC设计开发&#xff0c;还是把他…

54.get set

当程序查询对象属性时调用get方法,如果只有get方法那么他是一个只读属性&#xff0c;//程序对对象属性进行赋值操作时调用set方法&#xff0c;如果只有set方法那么他是是一个只读属性 <script type"text/javascript">var p {x:1.0,y:1.0,//当程序查询对象属性…

Codeforces Round #554 Div.2 E - Neko and Flashback

欧拉路径 神题啊神题&#xff01;这道题的突破口就是后两个数组每个元素是一一对应的。 也就是说&#xff0c;对于一个p的排列&#xff0c;b和c取得每一个元素的下标在p中都是一样的。 根据b和c数组的性质可以得出&#xff0c;b[i] < c[i]。 这也是我们输出-1的一个判断方法…

20172311 2017-2018-2 《程序设计与数据结构》第八周学习总结

20172311 2017-2018-2 《程序设计与数据结构》第八周学习总结 教材学习内容总结 本周对JAVA中的多态性进行了学习 多态性引用能够随时间变化指向不同类型的对象&#xff0c;是通过后绑定实现的。实现多态性的主要途径有两种&#xff1a; 1.由继承实现多态性 2.利用接口实现多态…

Linux系统安装Apache 2.4.6

http://www.cnblogs.com/kerrycode/p/3261101.html Apache简介 Apache HTTP Server&#xff08;简称Apache&#xff09;是Apache软件基金会的一个开放源码的网页服务器&#xff0c;可以在大多数计算机操作系统中运行&#xff0c;由于其多平台和安全性被广泛使用&#xff0c;是最…

深浅拷贝

lst1 ["金毛狮王", "紫衫龙王", "白眉鹰王", "青翼蝠王"] lst2 lst1 print(lst1) print(lst2) lst1.append("杨逍") print(lst1) print(lst2) # 结果: # [金毛狮王, 紫衫龙王, 白眉鹰王, 青翼蝠王, 杨逍] # [金毛狮王 紫衫…

lnmp化境开启pathinfo,支持tp5.0等访问

一、 开启pathinfo   #注释 下面这一行 #include enable-php.conf #载入新的配置文件 include enable-php-pathinfo.conf #添加如下location / {if (!-e $request_filename){rewrite ^/(.*)$ /index.php/$1 last;break;}}location ~ /index.php {fastcgi_pass 127.0.0.1:…

深度解密GO语言之反射

反射和 Interface 息息相关&#xff0c;而 Interface 是我们上一篇文章的内容。在开始正文前&#xff0c;和大家说点题外话。 上一篇关于 Interface 的文章发出后&#xff0c;获得了很多的关注和阅读。比如&#xff0c;登上了 GoCN 的每日新闻第一条&#xff1a; 可能是编辑者觉…

Python爬虫-正则表达式

正则表达式 只提取关注的数据&#xff0c;进行数据赛选 原子&#xff1a; 基本组成单位 普通的字符 非打印支付 通用字符 普通的字符 >>> import re >>> pat"yue" >>> string"http://yum.iqianyue.com" >>> rst1re.se…

openfire(一):使用idea编译openfire4.2.3源码

最近公司项目要使用openfire&#xff0c;并对源码做一些修改&#xff0c;使用的openfire版本为官网目前最新版本4.2.3&#xff0c;网上资料较少&#xff0c;踩了很多坑&#xff0c;特此记录。 1.下载源码 http://www.igniterealtime.org/downloads/source.jsp 2.使用idea导入源…

JAVA synchronized关键字锁机制(中)

synchronized 锁机制简单的用法&#xff0c;高效的执行效率使成为解决线程安全的首选。 下面总结其特性以及使用技巧&#xff0c;加深对其理解。 特性: 1. Java语言的关键字&#xff0c;当它用来修饰一个方法或者一个代码块的时候&#xff0c;能够保证在同一时刻最多只有一个线…

Python多线程豆瓣影评API接口爬虫

爬虫库 使用简单的requests库&#xff0c;这是一个阻塞的库&#xff0c;速度比较慢。 解析使用XPATH表达式 总体采用类的形式 多线程 使用concurrent.future并发模块&#xff0c;建立线程池&#xff0c;把future对象扔进去执行即可实现并发爬取效果 数据存储 使用Python ORM sq…

【自制工具类】Java删除字符串中的元素

这几天做项目需要把多个item的id存储到一个字符串中&#xff0c;保存进数据库。保存倒是简单&#xff0c;只需要判断之前是否为空&#xff0c;如果空就直接添加&#xff0c;非空则拼接个“&#xff0c;” 所以这个字符串的数据结构是这样的 String str "a,b,c,d"; 保…

DMA存储器到外设代码讲解

实验目的: bsp_dma_mtp.h #ifndef __BSP_DMA_MTP_H #define __BSP_DMA_MTP_H#include "stm32f10x.h" #include <stdio.h>// 串口工作参数宏定义 #define DEBUG_USARTx USART1 #define DEBUG_USART_CLK RCC_APB2Periph_USAR…

java基础集合类——LinkedList 源码略读

1.概览 LinkedList是java的动态数组另一种实现方式&#xff0c;底层是基于双向链表&#xff0c;而不是数组。 public class LinkedList<E>extends AbstractSequentialList<E>implements List<E>, Deque<E>, Cloneable, java.io.Serializable LinkedLis…

[BZOJ] 1688: [Usaco2005 Open]Disease Manangement 疾病管理

1688: [Usaco2005 Open]Disease Manangement 疾病管理 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 727 Solved: 468[Submit][Status][Discuss]Description Alas! A set of D (1 < D < 15) diseases (numbered 1..D) is running through the farm. Farmer John woul…

es6 var、let、const命令

1.let和var <1>let声明的变量仅在块级作用域内有效&#xff1b; var声明的变量在全局有效&#xff1b; <2> var变量乐意在声明之前使用&#xff0c;输出undefined; let 不可以&#xff0c;直接抛出一个错误&#xff1b; 例如&#xff1a;//var 声明console.log(a);…

实例属性和类属

1.Python是动态语言&#xff0c;根据类创建的实例&#xff0c;可以任意绑定属性 2.给实例绑定属性的方法有两种&#xff1a; 通过实例变量或者通过self变量。 1 class Student(object): 2 def __init__(self, name): 3 self.namename 4 5 ##或者如下&#xff1a; 6 &g…

vim中跳到第一行和最后一行

底线命令模式 :0或:1跳到文件第一行 :$跳到文件最后一行 命令模式 gg跳到第一行 shiftg跳到文件最后一行转载于:https://www.cnblogs.com/liuys635/p/10831196.html