java B2B2C springmvc mybatis仿淘宝电子商城系统-服务消费者(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
二、建一个服务消费者
重新新建一个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 @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
写一个测试类HelloService,通过之前注入ioc容器的restTemplate来消费service-hi服务的“/hi”接口,在这里我们直接用的程序名替代了具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名,代码如下:
@Service public class HelloService { @Autowired RestTemplate 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 { @Autowired HelloService 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:8762 hi forezp,i am from port:8763
这说明当我们通过调用restTemplate.getForObject(“http:/ /SERVICE-HI/hi?name=“+name,String.class)方法时,已经做了负载均衡,访问了不同的端口的服务实例。 java B2B2C springmvc mybatis仿淘宝电子商城系统
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
超详细新手建站指南
概述 对于一个建站新手来讲,最重要的莫过于2件事 时间效率 性价比 换句话讲,对于非专业选手,在整个建站过程,如何省时省力,用相对简单的方式,花更少的钱建好网站是关键。 基于上述,给大家带来一版适合新手的建站指南,供大家参考 建站指南 建站三大必备条件:域名+主机空间(虚机主机/服务器)+建站程序无论你是自己建站还是外包第三方建站,都需要具备这三个要素才能建网站。 注册域名 注册域名(域名就是人们常说的“网址”,好比一个网站的门牌号,不可或缺) 注意事项: 1.域名具有唯一性,同时域名要在63个字符之内,先注先得,一次性最多注册10年,并非永久有效,到期要进行需续费。 2.常见的域名后缀为.com(ps:应用最为广泛),有时候您可能无法获得想要的.com域名,那就建议使用 .net, .org 等后缀的域名。不同后缀代表和适合的行业不同,建议选择有相关性的域名后缀,例如:.com 商业性的机构或公司 .org 非盈利的组织、团体.gov.cn 政府部门 .net 从事互联网相关服务的机构或公司 3.随着互联网发展,域名价格越来越便宜,建议选择大一点的域名服务商,域名所有权和管理权限都 ...
- 下一篇
测试工具: 2019年测试自动化最佳Python框架
Pytest pytest用于各种软件测试,是测试自动化的顶级Python测试框架。 开源和易学,该工具可供QA团队,开发团队以及个人实践团队和开源项目使用。 如'断言重写'有用的功能,互联网上的大多数项目,包括像Dropbox和Mozilla,已经从unittest(Pyunit)切换到pytest。 让我们深入探讨一下这个Python框架的特殊之处。 先决条件? 除了Pythonpytest不需要任何复杂的东西。 优点 在Python测试社区中,在pytest到来之前,开发人员将他们的测试包含在大型类中。 然而,pytest带来了革命,因为它使得以比以前更紧凑的方式编写测试套件成为可能。 其他测试工具要求开发人员或测试人员使用调试器或检查日志并检测特定值的来源。 Pytest可以帮助您编写测试用例,使您能够将所有值存储在测试用例中,最后通
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS7设置SWAP分区,小内存服务器的救世主
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- CentOS关闭SELinux安全模块
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- CentOS7安装Docker,走上虚拟化容器引擎之路
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- 设置Eclipse缩进为4个空格,增强代码规范