博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring cloud之Ribbon搭建
阅读量:4170 次
发布时间:2019-05-26

本文共 3512 字,大约阅读时间需要 11 分钟。

本文搭建一个Ribbon实现负载均衡的效果

pom文件:

4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.1.RELEASE
com.zsy.cloud
cloud-ribbon
0.0.1-SNAPSHOT
jar
cloud-ribbon
http://maven.apache.org
1.8
Greenwich.M3
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
junit
junit
3.8.1
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false

properties文件:

server.port=2222spring.application.name=ribboneureka.client.service-url.defaultZone=http://register1:8081/eureka

启动类:

package com.zsy.cloud.cloud_ribbon;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@EnableDiscoveryClientpublic class RibbonApp {	public static void main(String[] args) {		SpringApplication.run(RibbonApp.class, args);	}		@Bean	@LoadBalanced	RestTemplate restTemplate() {		return new RestTemplate();	}}

 

Controller类:

package com.zsy.cloud.cloud_ribbon.controller;import javax.servlet.http.HttpServletRequest;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import com.zsy.cloud.cloud_ribbon.service.TestService;@Controllerpublic class TestController {	@Autowired	private TestService testService;	@GetMapping("/hello")	@LoadBalanced	public String test(String name) {		return testService.test(name);	}}

 

Service类:

package com.zsy.cloud.cloud_ribbon.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;@Servicepublic class TestService {	@Autowired	private RestTemplate template;	public String test(String name) {		return template.getForObject("http://eureka-client/test?name="+name, String.class);	}}

 

启动注册中心,测试站点(4个,不同端口),效果图如下:

 

访问controller路径:

转载地址:http://fvbai.baihongyu.com/

你可能感兴趣的文章
用python的matplotlib和numpy库绘制股票K线均线
查看>>
以互联网公司的经验告诉大家,架构师究竟比高级开发厉害在哪?
查看>>
GanttProject 使用的控件第三方包:jdnc-modifBen.jar
查看>>
ps、grep和kill联合使用杀掉进程
查看>>
openfire中的mina框架使用
查看>>
去掉Windows Messager的自动登录
查看>>
dspace可以检索中文了
查看>>
利用Eclipse编辑中文资源,配置文件
查看>>
将中文转为unicode 及转回中文函数
查看>>
《程序员》专访金蝶:是谁不相信国产软件?
查看>>
debian的gnome下的xmms乱码解决方案
查看>>
python切片操作
查看>>
python 中的split()函数和os.path.split()函数
查看>>
python 矩阵转置
查看>>
python 使用zip合并相邻的列表项
查看>>
python iter( )函数
查看>>
Python 迭代器(iterator)
查看>>
Python enumerate类
查看>>
leetcode 99 Recover Binary Search Tree (python)
查看>>
linux echo 向文件中追加信息
查看>>