Eureka消费者使用rest方式调用 依赖ribbon负载均衡器

上两篇博客已经描述了搭建Eureka注册中心和把服务提供者注册到Eureka

搭建Eureka注册中心

服务提供者注册到Eureka

在此基础上使用服务消费者去注册中心获取服务提供者的别名进行调用

新建一个maven项目并添加依赖

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
	</parent>
	<!-- 管理依赖 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.M7</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<!-- SpringBoot整合Web组件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- SpringBoot整合eureka客户端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

	</dependencies>
	<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/libs-milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

启动类


package com.vhukze.App;

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.context.annotation.ComponentScan;
import org.springframework.web.client.RestTemplate;

/**
* @author zsz
* @version 
* @创建时间:2019年10月10日 下午1:44:31
*/
@SpringBootApplication
@ComponentScan(basePackages = "com.vhukze")
@EnableEurekaClient
public class UseApp {
	
	public static void main(String[] args) {
		SpringApplication.run(UseApp.class, args);
	}
	
	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}
}

application.yml配置文件

###服务启动端口号
server:
  port: 8001
###服务名称(服务注册到eureka名称)  
spring:
    application:
        name: Use
    cloud: 
      client: 
        ipAddress: 127.0.0.1
###服务注册到eureka地址  
eureka:
  client:
    service-url:
           defaultZone: http://localhost:8100/eureka
    #注册
    register-with-eureka: true
    ###是否需要从eureka上获取注册信息
    fetch-registry: true
  instance: 
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ipAddress}:${spring.application.name}:${server.port}

这里使用rest方式调用,@LoadBalanced注册可以使RestTemplate在请求时拥有负载均衡的能力,可以解析服务在注册中心的别名。

新建一个controller,使用RestTemplate进行调用,底层是使用的Httpclient技术


package com.vhukze.controller;

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

/**
* @author zsz
* @version 
* @创建时间:2019年10月10日 下午1:52:14
*/
@RestController
public class TestController {

	@Autowired
	private RestTemplate restTemplate;
	
	@RequestMapping("/fun")
	public String fun() {
		String url = "http://Provider/Info/fun";
		String result = restTemplate.getForObject(url, String.class);
		return result;
	}
}

在服务提供者项目添加一个controller


package com.vhukze.api.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author zsz
* @version 
* @创建时间:2019年10月10日 上午9:11:06
*/
@RestController
@RequestMapping("Info")
public class InfoController {

	@Value("${server.port}")
	private String port;
	
	@RequestMapping("fun")
	public String fun() {
		return "服务提供者:"+port;
	}
}

启动三个项目并访问服务消费者中的fun方法

页面结果是    服务提供者:8000

如果把服务提供者的port改成8010,再启动一个,就是现在又两个服务提供者在启动着,只是端口号不同

然后你每刷新一次就变一次后面的端口号

因为负载均衡的基本策略是轮训机制。

发布了66 篇原创文章 · 获赞 35 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_41890624/article/details/102482216