SpringCould总结 | 第三篇 服务负载均衡ribbon
//1.工程结构
//2.pom文件
<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.spc.ribbon.server</groupId>
<artifactId>spc-ribbon-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath />
</parent>
<name>eurake-server</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</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>${spring-cloud.version}</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>
</project>
//3.配置文件
spring.application.name=spc-ribbon-server
server.port=2000
eureka.client.service-url.defaultZone=http://localhost:1000/eureka/
#eureka.instance.preferIpAddress=true
#eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
//服务类
package com.spc.ribbon.server.spc_ribbon_server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class AppService {
@Autowired
RestTemplate restTemplate;
public String queryOrderStatusById(String orderId) {
return restTemplate.getForObject("http://SPC-SERVICE-ORDER/queryOrderStatusById?orderId="+orderId,String.class);
}
public String queryWlInfoByOrderId(String orderId) {
//return restTemplate.getForObject("http://xxx/xxx?xxx="+xxx,String.class);
return ":[订单-商品-已发货]";
}
}
package com.spc.ribbon.server.spc_ribbon_server;
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 RibbonServer {
public static void main(String[] args) {
SpringApplication.run(RibbonServer.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
package com.spc.ribbon.server.spc_ribbon_server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AppControler {
@Autowired
private AppService appService;
/**
* 根据订单号查询订单状态查询物流信息
* @param orderId
* @return
*/
@RequestMapping(value = "/queryOrderStatusAndWlInfoByOrderId")
public String queryOrderStatusAndWlInfoByOrderId(@RequestParam String orderId){
/**
* 查询订单状态
*/
String orderInfo = appService.queryOrderStatusById(orderId);
/**
* 查询物流信息
*/
String wlInfo = appService.queryWlInfoByOrderId(orderId);
return orderInfo+wlInfo;
}
}
//运行结果
1.启动eureka注册中心 端口1000
2.注册服务到eureka spc-service-order 启动3001服务 修改配置文件端口3002然后在启动一次服务
3.启动ribbon使用appController调用spc-service-order服务
//启动的两个服务已经注册到eureka
//
//刷新查看负载均衡效果
//服务均衡ribbon已负载ok