Spring Cloud入门教程之服务消费者 Feign(三)(Finchley版本+Boot2.0)

什么是Feign?

        Feign是受到Retrofit,JAXRS-2.0和WebSocket的影响,它是一个java的到http客户端绑定的开源项目。 Feign的主要目标是将Java Http 客户端变得简单。

 

推荐博客:

      Feign的源码地址:https://github.com/OpenFeign/feign

     深入理解Feign之源码解析:https://blog.csdn.net/forezp/article/details/73480304

 

常见错误:

1、Spring Cloud服务消费者使用Feign,不识别@EnableFeignClients 注解解决办法

 查阅资料后是Spring Cloud对Feign的支持由org.springframework.cloud:spring-cloud-netflix-core 移到org.springframework.cloud:spring-cloud-openfeign-core下。

     报错信息:cannot resolve sysmol EnableFeignClients

    

    解决方案:

 

    更改Spring Cloud的版本Finchley.RC2换为Dalston.RC1由于Finchley.RC2使用需要Spring Boot2.0,这里需要把Spring Boot版本换到1.5即可

<dependencies>
   <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>Dalston.RC1</version>
      <type>pom</type>
      <scope>import</scope>
   </dependency>
</dependencies>


 

   

一、建立服务消费者

    1、项目建立可参考上一篇博客:https://blog.csdn.net/zjh_746140129/article/details/80557302

 

    2、项目pom.xml 新增

<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>

完整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.serverfeign</groupId>
	<artifactId>serverfeign</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

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

	<!--<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.2.RELEASE</version>
		<relativePath/> <!– lookup parent from repository –>
	</parent>-->
	<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>
		<spring-cloud.version>Finchley.RC2</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>-->

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.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-feign</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</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>-->
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.RC1</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>

 

 

 

    3、编写服务消费Service和Controller

        通过@ FeignClient(“服务名”),来指定调用哪个服务

package com.serverfeign.serverfeign.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


/**
 * 通过@FeignClient(“服务名”),来指定调用哪个服务
 * Created by zhoujh on 2018/6/04
 */
@FeignClient(value = "hello-service")
public interface HelloService {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

  

 

package com.serverfeign.serverfeign.controller;

import com.serverfeign.serverfeign.service.HelloService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

@RestController

public class HelloController {

@Autowired

HelloService helloService;

@RequestMapping(value = "/hi",method = RequestMethod.GET)

@ResponseBody

public String HiFeign(@RequestParam String name){

return helloService.sayHiFromClientOne(name);

}

}

 

4、修改配置文件

server.port=8765
spring.application.name=service-feign
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

 

 

 

5、修改启动类

在程序的启动类ServiceFeignApplication ,加上@EnableFeignClients注解开启Feign的功能

package com.serverfeign.serverfeign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServerfeignApplication {

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

 

二、启动服务注册中心、服务提供者(2个)、服务消费者(Feign)

 

三、刷新消费者,查看服务提供者后台打印,此时已经完成了消费的负载均衡(这里采用了轮询策略)

 

 

 

 

Spring Boot与Spring Cloud学习使用可参看笔者博客

       ①Spring Cloud入门教程之服务注册与发现Eureka

       ②Spring Cloud入门教程之服务消费者 Ribbon

       ③Spring Cloud入门教程之服务消费者 Feign

       ④Spring Cloud入门教程之断路器 Hystrix

       ⑤Spring Cloud入门教程之断路由网关 Zuul

       ⑥Spring Cloud入门教程之分布式配置中心 Spring Cloud Config

       ⑦idea下新建Spring Boot项目并配置启动

       ⑧Spring Boot无法自动注入bean问题解决方案

       ⑨idea 设置Spring Boot热部署

 

 

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值