Ribbon实战
注意:
本SpringCloud系列博客案例代码,是建立在一个共同的项目中,以后所有模块公共依赖包不再赘述,具体请参考Eureka实战篇.
https://blog.csdn.net/syc000666/article/details/95388985
org.springframework.boot
spring-boot-starter-parent
2.0.3.RELEASE
UTF-8
UTF-8
UTF-8
1.8
1.8
1.8
1.16.20
Finchley.RELEASE
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
${lombok.version}
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
一. 创建Eureka Server模块
1. pom.xml中添加依赖
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
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. 项目结构

org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
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. 项目结构

org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
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端口的服务实例.

修改端口号为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接口.