xl_echo编辑整理,欢迎转载,转载请声明文章来源。更多IT编程案例、资料请联系QQ:1280023003 ,群:298140694
百战不败,依不自称常胜,百败不颓,依能奋力前行。——这才是真正的堪称强大!!
这篇文章是衔接上一篇文章的,需要这个程序完成实现最终效果,需要先将之前几篇文章全部阅读并建立相应的工程。上篇参考地址:https://blog.csdn.net/xlecho/article/details/80683645
开发工具 | 版本 |
---|---|
JDK | 1.8 |
Maven | maven-3.1.1 |
IDEA | IntelliJ IDEA 2018.1.3 x64 |
Notepad | Notepad++ |
首先创建一个基本的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.echo.demo</groupId>
<artifactId>demo_consumer_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.5.13.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.SR5</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>
注意依赖的版本尽量引入最近使用较为稳定的版本,如果不知道直接复制粘贴以上的即可。
然后创建主类:
package com.echo.demo;
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;
/**
* @Author xl_echo
* @Date 2018/6/15 上午11:02
**/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceFeignApplication.class, args);
}
}
一定要引入EnableFeignClients。
然后创建配置文件application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:10080/eureka/
server:
port: 10086
spring:
application:
name: service-feign
配置文件完成之后,再次创建一个feign接口。注意:这个接口对于调用相当重要,一定要注意配置,注意配置。
package com.echo.demo.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @Author xl_echo
* @Date 2018/6/15 上午11:04
**/
@FeignClient(value = "DEMO-SERVICE")
public interface ServiceFeign {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
String getString();
}
最为关键的两个地方在于类上的注解、访问路径
@FeignClient(value = "DEMO-SERVICE")
是指向要找到哪一个服务
访问路径也不能错,一定要和提供服务中的接口路径一直,不然访问不到服务中的方法。
接口完成之后,实现调用接口即可,相当于简介调用服务提供者。实现类如下:
package com.echo.demo;
import com.echo.demo.feign.ServiceFeign;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author xl_echo
* @Date 2018/6/15 上午11:11
**/
@RestController
public class HelloController {
@Autowired
ServiceFeign serviceFeign;
@RequestMapping(value = "/getString",method = RequestMethod.GET)
public String getHello(){
return serviceFeign.getString();
}
}
这里基本工作已经完成了,接下来我们一次启动服务即可。先启动eureka,再启动服务提供者,然后启动我们本章所说的服务消费者,访问路径localhost:10086/getString 即可看到服务提供者返回的信息。