Spring Cloud 服务消费 [ Ribbon ]

原创
2017/09/05 17:03
阅读数 42

Ribbon是一个基于HTTP和TCP客户端的负载均衡器

Spring Cloud 服务注册与发现 [ eureka ] 创建了一个简单的服务注册中心。

应为要运行多个应用作为服务提供方,所以将 eureka-client 打包。

准备环境:

1、运行 eureka 服务注册中心

2、运行两个 eureka-client 应用,用--server.port指定端口

eureka-client-0.0.1-SNAPSHOT.jar


eureka-client-0.0.1-SNAPSHOT.jar --server.port=9762

此时访问 http://localhost:9760/ 发现eureka-client-test服务有两个单元运行。

--------------------------------------------------------------------------------------

创建 SpringBoot应用,添加 ribbon依赖和 eureka 依赖,pom.xml

<?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.forwy</groupId>
	<artifactId>eureka-ribbon</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>eureka-ribbon</name>
	<description>eureka-ribbon project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.6.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>
		<spring-cloud.version>Dalston.SR3</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</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-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>

配置文件

spring.application.name=ribbon-consumer

server.port=9991

eureka.client.serviceUrl.defaultZone=http://localhost:9760/eureka/

启动类加上 @EnableDiscoveryClient注解,并加上一个 RestTemplate 的bean

package com.forwy.eurekaribbon;

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;

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaRibbonApplication {

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

	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}
}

编写一个 ConsumerController,实现调用 eureka-client 的eruekaTest这个方法

package com.forwy.eurekaribbon.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerController {

    @Autowired
    RestTemplate rt;

    @RequestMapping(value = "/eurekaTest",
                    method = RequestMethod.GET
    )
    public String eurekaTest() {
        return rt.getForEntity("http://EUREKA-CLIENT/eurekaTest?w=hello&y=skye", String.class).getBody();
    }

}

至此,ribbon负载均衡应用完毕

--------------------

启动 ribbon 应用,访问两次 http://localhost:9991/eurekaTest

发现两个 eureka 实例分别打印出一条日志,说明各调用一次

2017-09-05 16:51:15.899  INFO 9692 --- [nio-9761-exec-6] c.f.e.controller.EurekaController        : the host is : DESKTOP-LSB5ED0 ,the port is : 9761, the servier id is : eureka-client
2017-09-05 16:51:11.116  INFO 13744 --- [nio-9762-exec-1] c.f.e.controller.EurekaController        : the host is : DESKTOP-LSB5ED0 ,the port is : 9762, the servier id is : eureka-client

 

展开阅读全文
加载中

作者的其它热门文章

打赏
0
1 收藏
分享
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部