Spring cloud之Ribbon搭建

本文搭建一个Ribbon实现负载均衡的效果

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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.zsy.cloud</groupId>
	<artifactId>cloud-ribbon</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	
	<name>cloud-ribbon</name>
	<url>http://maven.apache.org</url>


	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Greenwich.M3</spring-cloud.version>
	</properties>

	<dependencies>
	    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
	    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<dependency>
		  <groupId>junit</groupId>
		  <artifactId>junit</artifactId>
		  <version>3.8.1</version>
		  <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>
	
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
</project>

properties文件:

server.port=2222
spring.application.name=ribbon

eureka.client.service-url.defaultZone=http://register1:8081/eureka

启动类:

package com.zsy.cloud.cloud_ribbon;

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 RibbonApp {

	public static void main(String[] args) {
		SpringApplication.run(RibbonApp.class, args);
	}
	
	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}
}

 

Controller类:

package com.zsy.cloud.cloud_ribbon.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zsy.cloud.cloud_ribbon.service.TestService;

@Controller
public class TestController {

	@Autowired
	private TestService testService;

	@GetMapping("/hello")
	@LoadBalanced
	public String test(String name) {
		return testService.test(name);
	}

}

 

Service类:

package com.zsy.cloud.cloud_ribbon.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


@Service
public class TestService {

	@Autowired
	private RestTemplate template;

	public String test(String name) {
		return template.getForObject("http://eureka-client/test?name="+name, String.class);
	}
}

 

启动注册中心,测试站点(4个,不同端口),效果图如下:

 

访问controller路径:

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
搭建Spring Cloud开发环境,你需要进行以下步骤: 1. 首先,你需要确保你的Java开发环境已经安装和配置好了。你可以去Oracle官网下载和安装Java Development Kit (JDK)。 2. 接下来,你需要使用构建工具(比如Maven或Gradle)来构建你的项目。在你的项目的pom.xml(如果使用Maven)或build.gradle(如果使用Gradle)文件中,你需要添加Spring Cloud的相关依赖。你可以在Maven Central Repository搜索并添加Spring Cloud的依赖,其中包括中提到的`org.springframework.cloud:spring-cloud-config-server`和中提到的`org.springframework.cloud:spring-cloud-config-client`。 3. 在你的项目中,你需要创建一个Spring Cloud Config Server。这是一个服务,用于管理和提供配置信息给其他应用程序。你可以使用Spring Boot创建一个新的Spring Cloud Config Server应用程序,并在其配置文件中指定一些基本设置,比如配置文件存储库的位置和访问控制规则。 4. 接下来,你需要创建一个Spring Cloud Config Client应用程序。这是真正的应用程序,它将从Config Server获取配置信息并使用它们。你可以使用Spring Boot创建一个新的Spring Cloud Config Client应用程序,并在其配置文件中指定Config Server的URL以及应用程序需要的其他配置属性。 5. 最后,你可以根据需要创建其他的Spring Cloud组件,比如服务注册与发现(如Spring Cloud Netflix Eureka),负载均衡(如Spring Cloud Netflix Ribbon)等等。根据你的具体需求,你可以在你的项目中添加这些组件。 综上所述,搭建Spring Cloud开发环境的步骤包括安装Java开发环境、配置构建工具、添加Spring Cloud依赖、创建Config Server和Config Client应用程序,以及根据需要添加其他的Spring Cloud组件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值