SpringCloud学习(⼆):Feign接⼝调⽤服务菜鸟学渣接触spring cloud 系列...
公司也上微服务了,再不学习下就凉了,所以来踩坑吧...
版本:
spring-boot: 2.0
spring-cloud: Finchley.SR1
已有项⽬:
[eureka-server] # 注册中⼼
[eureka-client-one] # 微服务1
能上图绝不BB:
微服务之间的接⼝相互调⽤,使⽤的是Feign,⽐HTTPClient好⽤多了...
⼀、Feign端 [eureka-client-two]
这⾥新建⼀个微服务[eureka-client-two]调⽤[eureka-client-one]中的helloworld接⼝
主要引⼊依赖 spring-cloud-starter-openfeign
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId&ku</groupId>
<artifactId>eureka-client-two</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-client-two</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
springcloud难学吗<artifactId>spring-boot-starter-web</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>
配置⽂件 l
server:
port: 8502
spring:
application:
name: eureka-client-two
eureka:
client:
serviceUrl:
defaultZone: localhost:8761/eureka/
启动类 EurekaClientTwoApplication.java
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableEurekaClient // 开启eureka客户端
@EnableFeignClients(basePackages = {"ku.eurekaClientTwo.feign"}) // 开启feign
public class EurekaClientTwoApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientTwoApplication.class, args);
}
}
Feign类 EurekaClientOneFeign.java
/**
* value为eureka-client-one注册时的 spring.application.name
*/
@FeignClient(value = "eureka-client-one")
public interface EurekaClientOneFeign {
/**
* eureka-client-one的helloworld访问mapping
*/
@RequestMapping("/")
String home();
}
Rest服务 FeignTest.java :逻辑是调⽤rest接⼝,接⼝中通过feign调⽤eureka-client-one的接⼝,返回接受数据@RestController
public class FeignTest {
@Autowired
EurekaClientOneFeign eurekaClientOneFeign; // 如果提⽰“No Beans Named***" 其实没问题,恶...
@RequestMapping("/test/home")
public String testHome(){
// 调⽤feign,feign调⽤ eureka-client-one的api
return eurekaClientOneFeign.home();
}
@RequestMapping("/")
public String hello(){
return "hello world -- client two";
}
}
⽬录结构
依次启动项⽬ [eureka-server]、[eureka-client-one]、[eureka-client-two]
访问注册中⼼,2个服务都添加到了实例列表
⼆、结果
当feign中RequestMapping下的接⼝带了参数,会发现⽤起来确实⽐⼿搓HttpClient舒服...
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论