Ribbon实战
注意:
本SpringCloud系列博客案例代码,是建立在一个共同的项目中,以后所有模块公共依赖包不再赘述,具体请参考Eureka实战篇.
<!--想让本项目成为spring-boot项目,怎么办?-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!--<version>2.1.5.RELEASE</version>-->
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<!--基本配置-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<encoding>UTF-8</encoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<lombok.version>1.16.20</lombok.version>
<!--SpringCloud版本号F版-->
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<!--<version>Camden.SR5</version>-->
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-actuator</artifactId>-->
<!--</dependency>-->
<!--web启动器是与SpringMVC相关的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
一. 创建Eureka Server模块
1. pom.xml中添加依赖
<dependencies>
<!--eureka的服务端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
2. 创建配置文件
server:
port: 8761
spring:
application:
name: eurka-server
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3. 创建入口类
package com.syc.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* 跟一一哥学SpringCloud微服务
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args){
SpringApplication.run(EurekaApplication.class,args);
}
}
4. 项目结构

二. 创建一个服务提供者
1. 添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
2. 创建配置文件
server:
port: 8762
spring:
application:
name: eureka-provider
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
注意:
本案例中共有2个服务提供者,代码一样,端口号分别为8762,8763,我们是在一个机器上部署两个不同端口号的服务实例.
3. 创建对外提供服务的接口
package com.syc.cloud.web;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Value("${server.port}")
private String port;
@RequestMapping("/hello")
public String sayHi(@RequestParam String name) {
return "Hello," + name + "! This is " + port;
}
}
4. 创建入口类
package com.syc.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
*
*/
@SpringBootApplication
@EnableEurekaClient
public class ServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
}
5. 项目结构

三. 创建一个服务消费者
1. 添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--客户端负载均衡组件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
</dependencies>
2. 创建配置文件
server:
port: 8764
spring:
application:
name: eureka-ribbon
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
3. 创建远程调用的service
package com.syc.cloud.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* 远程调用eureka-provider的方法
*/
@Service
public class RibbonService {
@Autowired
private RestTemplate restTemplate;
public String helloService(String name) {
return restTemplate.getForObject("http://eureka-provider/hello?name="+name,String.class);
}
}
4. 创建web层的controller
package com.syc.cloud.web;
import com.syc.cloud.service.RibbonService;
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 HelloController {
@Autowired
private RibbonService helloService;
@RequestMapping(value = "/hello")
public String hello(@RequestParam String name){
return helloService.helloService(name);
}
}
5. 创建入口类
package com.syc.cloud;
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.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
*
*/
@SpringBootApplication
@EnableDiscoveryClient //注意:必须是@EnableDiscoveryClient注解,不能是@EnableEurekaClient,否则无法实现负载均衡.
//@EnableEurekaClient
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
//创建负载均衡组件
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
注意:
入口类必须添加@EnableDiscoveryClient注解,不能是@EnableEurekaClient注解,否则无法实现负载均衡.
四. 一个服务实例多端口同时启动配置
为了创建高可用的微服务,我们配置同一个服务实例按多个端口同时启动.
1. 首先配置8762端口的服务实例.

2. 然后复制8762端口的配置.

修改端口号为8763.
3. 创建一个Compound
注意就可以通过Compound来同时启动2个服务实例了.
五. 启动4个服务

此时已经成功的启动了4个服务实例.
接口测试:
第一次访问接口,端口号8762.

第2次访问接口,端口号8763.

可见默认采用的是轮询的负载均衡方式.
六. 项目架构

- 一个服务注册中心,eureka server,端口为8761;
- EUREKA-PROVIDER工程跑了两个实例,端口分别为8762,8763,分别向服务注册中心注册;
- EUREKA-RIBBON端口为8764,向服务注册中心注册;
- 当EUREKA-RIBBON通过restTemplate调用EUREKA-PROVIDER的hello接口时,因为用ribbon进行了负载均衡,会轮流的调用EUREKA-PROVIDER: 8762和8763 两个端口的hello接口.