Spring cloud学习 (三)ribbon
来源:互联网 发布:淘宝话费充值软件 编辑:程序博客网 时间:2024/06/06 18:32
参看网址:http://blog.csdn.net/forezp/article/details/69788938
在Spring cloud 学习(二)Feign的基础上继续实现ribbon的负载均衡效果。
1.修改pom.xml
加入以下依赖:
<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>
2.改造service0
为了显示负载均衡的效果,把端口也显示出来,修改:
package service0.controller;
import java.util.Date;
import org.apache.commons.lang.time.FastDateFormat;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
@RestController
public class Service0Controller {
@Value("${server.port}")
String port;
@GetMapping("/service0/{value}")
public String service0(@PathVariable("value") String value){
return value + ":" + FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.sss").format(new Date()) + ":" +port;
}
}
3.添加module service01
代码跟service0基本相同。
application.yml:
spring:
application:
name: service0 #冒号后面必须要有空格
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/ #冒号后面必须要有空格
registry-fetch-interval-seconds: 1
instance:
hostname: localhost #冒号后面必须要有空格
instance-id: http://localhost:8084 #冒号后面必须要有空格
server:
port: 8084 #冒号后面必须要有空格
Service01.java:
package service01;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@SpringBootApplication(scanBasePackages = "service01")
@EnableEurekaClient
@EnableWebMvc
@EnableHystrixDashboard
@EnableCircuitBreaker
public class Service01 {
public static void main(String[] args) {
SpringApplication.run(Service01.class, args);
}
}
Service01Controller.java:
package service01.controller;
import java.util.Date;
import org.apache.commons.lang.time.FastDateFormat;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
@RestController
public class Service01Controller {
@Value("${server.port}")
String port;
@GetMapping("/service0/{value}")
public String service0(@PathVariable("value") String value){
return value + ":" + FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.sss").format(new Date()) + ":" +port;
}
}
3.添加module ribbon
application.yml:
spring:
application:
name: service-ribbon #冒号后面必须要有空格
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/ #冒号后面必须要有空格
server:
port: 8085 #冒号后面必须要有空格
Ribbon.java(启动类):
package 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
@EnableDiscoveryClient
public class Ribbon {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(Ribbon.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
新建两个包:
建一个service类,RibbonService.java:
package ribbon.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class RibbonService {
@Autowired
RestTemplate restTemplate;
public String Service0(String name) {
return restTemplate.getForObject("http://SERVICE0/service0/" + name, String.class);
}
}
新建一个controller类,RibbonController.java:
package ribbon.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ribbon.service.RibbonService;
@RestController
public class RibbonController {
@Autowired
RibbonService ribbonService;
@RequestMapping("/service0/{value}")
public String Service0(@PathVariable("value") String value) {
return ribbonService.Service0(value);
}
}
4.测试
启动discovery,service0,service01,ribbon,访问以下地址http://127.0.0.1:8085/service0/qq,可以发现端口是随机变化的,说明负载均衡已经生效:
- Spring cloud学习 (三)ribbon
- Spring Cloud 之ribbon(三)
- Spring Cloud学习--负载均衡(Ribbon)
- Spring Cloud(三):服务消费Ribbon
- 三、Spring Cloud 的 Ribbon 负载均衡
- 学习Spring Cloud第七课(Ribbon的基本使用)
- 学习Spring Cloud第七课(Ribbon的基本使用)
- 学习Spring Cloud第十课(Ribbon脱离Eureka使用)
- Spring Cloud实战(三)-Spring Cloud Netflix Ribbon
- 疯狂Spring Cloud连载(8)Spring Cloud与Ribbon
- Spring Cloud源码分析(二)Ribbon
- Spring Cloud源码分析(二)Ribbon
- Spring Cloud源码分析(二)Ribbon
- Spring Cloud Ribbon(负载均衡)
- Spring Cloud源码分析(二)Ribbon
- spring cloud-ribbon
- spring cloud ribbon配置
- spring cloud Ribbon
- 【SpringMVC-001】idea创建SpringMVC项目傻瓜教程
- 程序打成jar包路径不对、找不到配置文件的解决
- 常用的内排序算法的描述和实现
- 51nod-1831 小C的游戏
- X264参数之zerolatency的分析
- Spring cloud学习 (三)ribbon
- BASE64Decoder 对文件编码解码
- element-ui,后台status状态为1,2,3,转化为中文
- 1035. 插入与归并(25)
- HTTP协议/IIS 原理及ASP.NET运行机制浅析
- 使用cmake进行Android项目的构建
- 仪表盘
- echarts自定义配置(持续更新)
- 1018. Public Bike Management (30)