springcloud微服务学习之ribbon

526 阅读2分钟

ribbon的作用

上一个教程中服务注册好之后,访问服务是精确指定ip端口的,不能满足高可用的要求。

ribbon用于客户端的负载均衡,nginx用于服务端的负载均衡

ribbon的配置

新建一个springcloud工程,修改pom文件

<?xml version="1.0" encoding="UTF-8"?>
<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.guohe3</groupId>
	<artifactId>ribbon</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>ribbon</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<fastjson.version>1.2.45</fastjson.version>
	</properties>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>


		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-ribbon</artifactId>
			<version>1.4.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>

		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

application.properties配置

server.port=9000
spring.application.name=ribbon-consumer
stores.ribbon.listOfServers=localhost:8082,localhost:8081  #配置负载均衡的地址,其中stores是自定义的。
stores.ribbon.NFLoadBalancerRuleClassName:com.netflix.loadbalancer.RandomRule
#配置负载均很算法

ConsumerController类

@RestController
public class ConsumerController {
    @Autowired
    private LoadBalancerClient loadBalancerClient;//负载均衡的客户端
    @RequestMapping("helloConsumer")
    public String helloConsumer(){
        ServiceInstance instance=loadBalancerClient.choose("stores");//选择一个uri 默认是轮询
        URI uri=URI.create(String.format("http://%s:%s",instance.getHost(),instance.getPort()));
        return uri.toString();
    }
}

上述操作很有问题,stores.ribbon.listOfServers=localhost:8082,localhost:8081 里面的服务地址是写死的,如果一个服务挂了不能及时动态删除,所以一般要和Eureka客户端联合起来使用,而且不能有单点故障问题

ribbon与Eureka结合

application.properties

server.port=9000
spring.application.name=ribbon-consumer
#stores.ribbon.listOfServers=localhost:8082,localhost:8081
eureka.client.service-url.defaultZone=http://localhost:8888/eureka/,http://localhost:8889/eureka/

配置其实和Eureka的配置是一样的

启动类

@SpringBootApplication
@EnableDiscoveryClient //服务发现
//@RibbonClient(name = "hello-service",configuration = .class)修改负载均衡算法
public class EurekaClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaClientApplication.class, args);
	}

	@Bean
	@LoadBalanced
	RestTemplate restTemplate(){ //面向服务调用
		return new RestTemplate();
	}


}

多了一个@EnableDiscoveryClient服务发现注解和一个RestTemplate的实例注册

Controller

@RestController
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;
    @RequestMapping("helloConsumer")
    public String helloConsumer(){
        return restTemplate.getForEntity("http://HELLO-SERVICE/hello",String.class).getBody();
    }
}

会去调用HELLO-SERVICE的服务。

一般ribbon不会单独使用回合eureka客户端一起使用,避免了单点故障