(四)Spring Cloud实践:使用Feign实现负载均衡

除了通过Ribbon实现负载均衡外,还可以使用Feign,通过声明式服务调用实现负载均衡。在使用Feign进行服务调用时,Spring Cloud通过集成Ribbon和Eureka来提供HTTP客户端的负载均衡。

 创建模块6,模块名称为feignservice,其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.news</groupId>
	<artifactId>feignservice</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>feignservice</name>
	<description>用Feign来调用SERVICE-HELLOWORLD暴露的REST接口,以获取到“Hello World”信息。</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.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>Finchley.SR2</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-eureka-client</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</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>

在application.yml中添加如下内容:

eureka:
   client:
      serviceUrl:
        defaultZone: http://localhost:8080/eureka/
server:
   port: 8092
spring:
   application:
      name: service-feign

修改其启动主类,具体内容如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
 *@Description: https://www.cnblogs.com/chry/p/7266916.html
 * 					用声明式REST客户端Feign调用远端HTTP服务,实现负载均衡
 *
 *@Author: Young
 *@@CreateDate:
 */
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ServicefeignApplication {

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

注解@EnableFeignClients表明支持声明式调用来实现负载均衡

创建HelloController,其内容如下:

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;

/**
 * @program: springlearn
 * @description: controller
 * @author: Mr.Young
 * @create: 2018-11-02 17:10
 **/
@RestController
public class HelloController {
    @Autowired
    HelloWorldService helloWorldFeignService;
    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String sayHello(){
        return helloWorldFeignService.sayHello();
    }

    @RequestMapping(value = "/configtest",method = RequestMethod.GET)
    public String configtest(){
        return helloWorldFeignService.configTest();
    }
}

创建接口类HelloWorldService,其内容如下:

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @program: springlearn
 * @description: inteface
 * @author: Mr.Young
 * @create: 2018-11-02 17:09
 **/
@FeignClient(value = "CONFIG-CLIENT")
public interface HelloWorldService {
    @RequestMapping(value = "/")
    String sayHello();
    @RequestMapping(value ="/configtest")
    String configTest();
}

@FeignClient用于通知Feign组件对该接口进行代理,使用时可以直接通过@Autowire注入,比如HelloController中,通过使用 @Autowired    HelloWorldService helloWorldFeignService来实现注入。另外,value值为需要调用的微服务名称,依赖微服务在注册中心注册的名称。每一个@RequestMapping对应微服务config-client中提供的服务url。

启动该模块,然后访问http://localhost:8092/,多次刷新,其效果和Ribbon基本一样。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值