SpringCloud 学习笔记------服务消费者(Feign)

本篇内容基于之前笔记所涉及的实例。

  1. 一个服务注册中心——eurekaserver:8081
  2. 三个服务者——peoduceserver:8082/peoduceserver:8083/peoduceserver:8084

第一步,新建名为service-feign的springboot工程,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.forezp</groupId>
	<artifactId>service-feign</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>service-feign</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.0.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>
	</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>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>

第二步,编辑application.yml配置文件。端口号8100,服务名service-fegin。

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8081/eureka/
server:
  port: 8100
spring:
  application:
    name: service-feign   #服务名

第三步,定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用的服务。

    此处我是调用了peoduceserver服务的“/hello”接口,代码如下:

@FeignClient(value = "peoduceserver")
public interface FeignService {

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    String helloFeign();
}

第四步,定义一个FeignContrller,调用以上的servcie接口。

@RestController
public class FeignContrller{

    @Autowired
    FeignService feignService;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String helloFegin(){
        return feignService.helloFeign();
    }
}
第五步,启动服务。浏览器输入 http://localhost:8100/hello访问。效果如下:


猜你喜欢

转载自blog.csdn.net/weixin_42074377/article/details/80650402
今日推荐