参看网址: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,可以发现端口是随机变化的,说明负载均衡已经生效: