我们使用Feign来进行应用间的通信,我们在调用方增加一个依赖,第一步加依赖,第二步在启动类上加注解,
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
定义好你要调用哪些接口
@FeignClient(name = "product")
public interface ProductFeignClient {
@RequestMapping(value = "/msg", method = RequestMethod.GET)
public String productMsg();
}
这个方法名字可以和product不一样的,上面加一个注解@FeignClient,name是表示你要访问哪个应用的接口,
我们要访问的是product,就这么来写,product下面的msg接口,你千万要注意,你没有写过的话可能会有点奇怪,
这不是服务端的写法吗,怎么客户端写成这样子,这里写完之后怎么来用,这里是一个接口,只是定义,定义完了,
通信完全是一个类,调用某个方法
@RestController
public class ClientController2 {
@Autowired
private ProductFeignClient productFeignClient;
@GetMapping("/getProductMsg")
public String findById(@PathVariable Long id) {
String productMsg = this.productFeignClient.productMsg();
System.out.println(productMsg);
return productMsg;
}
}
你可以看到刚刚我们在服务端,是一点改变都没有的,完全没有动过,只是在客户端修改了相关的代码,
localhost:7900/msg
localhost:7901/msg
localhost:8010/getProductMsg
Feign作为客户端要去访问别人的地址,Feign是一个声明式客户端,注意他并不是RPC,刚才我们已经使用过了,
他是接口加注解的方式,也就是定义一个接口,然后在接口上添加注解,回忆我们刚才在程序的入口类,通过注解,
来开启Feign,然后定义了一个Feign接口,来指定调用哪个服务,Feign让我们不习惯的原因呢,他感觉是在用RPC,
完全感觉不到是远程方法,更感觉不到这是一个HTTP请求,但是Feign本质还是HTTP客户端,通过Feign我们可以
远程调用,开发者完全透明,得到与本地方法一样的体验,Fegin内部也使用了Ribbon做负载均衡
<?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.learn</groupId>
<artifactId>order</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>cn.learn</groupId>
<artifactId>microcloud02</artifactId>
<version>0.0.1</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<dependency>
<!-- 引入web模块 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot进行单元测试的模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
<!-- 这个插件,可以将应用打包成一个可执行的jar包 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
#debug=true
server.port=8010
eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eureka
spring.application.name=order
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
eureka.client.healthcheck.enabled=true
product.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule
package com.learn.cloud.client;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name = "product")
public interface ProductFeignClient {
@RequestMapping(value = "/msg", method = RequestMethod.GET)
public String productMsg();
}
package com.learn.cloud.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.learn.cloud.client.ProductFeignClient;
@RestController
public class FeignClientController {
@Autowired
private ProductFeignClient productFeignClient;
@GetMapping("/getProductMsg")
public String findById() {
String productMsg = this.productFeignClient.productMsg();
System.out.println(productMsg);
return productMsg;
}
}
package com.learn.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}