1.注册中心搭建
参考下述注册中心搭建
https://blog.youkuaiyun.com/weixin_42941199/article/details/111592052
2.Feign声明式服务端搭建
2.1.pom依赖引入
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>03-eureka-feign-client-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>03-eureka-feign-client-provider</name>
<description>声明式声明式调用提供者</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
2.2.配置文件
spring.application.name=03-eureka-feign-client-provider
server.port=8081
#注册中心访问地址
eureka.client.service-url.defaultZone=http://eureka-server-slave:9101/eureka,http://eureka-server-master:9100/eureka
#表示eureka client发送心跳给server端的频
eureka.instance.lease-renewal-interval-in-seconds=2
#表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance。
#默认为90秒
#如果该值太大,则很可能将流量转发过去的时候,该instance已经不存活了。
#如果该值设置太小了,则instance则很可能因为临时的网络抖动而被摘除掉。
#该值至少应该大于leaseRenewalIntervalInSeconds
eureka.instance.lease-expiration-duration-in-seconds=10
2.3.接口发布
package com.example.controller;
import com.example.entry.User;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
public class Test {
@RequestMapping("/test")
public String test(){
return "第一个服务提供者";
}
/*
但当服务端是GET请求时,那么需要在请求参数前加上@RequestParam注解修饰
*/
@RequestMapping(value="/testGetParams",method= RequestMethod.GET)
public String testGetParams(@RequestParam("name") String name,@RequestParam("age") Integer age){
return "第一个服务testGetParams提供者name:"+name+" age:"+age;
}
@RequestMapping(value="/testPostParams",method= RequestMethod.POST)
public String testPostParams(String name,Integer age){
return "第一个服务testPostParams提供者name:"+name+" age:"+age;
}
@RequestMapping("/testParamsMap")
public Map<String,Object> testParamsMap(@RequestBody Map<String,Object> map){
for(Map.Entry<String, Object> entry : map.entrySet()){
String mapKey = entry.getKey();
Object mapValue = entry.getValue();
System.out.println(mapKey+":"+mapValue);
}
return map;
}
@RequestMapping("/testCustomParams")
public User testCustomParams(@RequestBody User user){
System.out.println(user.getId());
return user;
}
}
2.4.启动类编写
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
//激活eureka客户端
@EnableEurekaClient
public class EurekaFeignClientProvider {
public static void main(String[] args) {
SpringApplication.run(EurekaFeignClientProvider.class, args);
System.out.println("声明式feign提供者启动ok");
}
}
3.Feign声明式客户端搭建
3.1.pom依赖引入
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>03-eureka-feign-client-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>03-eureka-feign-client-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency><!-- feign依赖 -->
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
3.2.配置文件
spring.application.name=03-eureka-feign-client-consumer
eureka.client.service-url.defaultZone=http://eureka-server-slave:9101/eureka,http://eureka-server-master:9100/eureka
server.port=8080
3.3.引入要调用的接口
package com.example.sevice;
import com.example.entry.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
/**
* @FeignClient 用于标记当前接口是一个Feign的声明式服务接口
* Spring会为这个接口生成动态代理对象
* 属性
* name 用于指定注册中心的提供者服务名称
* fallback 用于指定熔断器
*/
@FeignClient(name = "03-eureka-feign-client-provider")
public interface TestService {
/**
* 定义抽象方法,并使用RequestMapping标记这个方法用于访问服务提供者
* 其中RequestMapping的参数/test应该与当前接口所标记的服务名中的服务中的
* 某个请求路劲相同
* @return
*/
@GetMapping("/test")
String test();
/*
*当使用Feign时,需要在请求参数前加上@RequestParam注解修饰
*/
@GetMapping("/testGetParams")
String testGetParams(@RequestParam("name") String name, @RequestParam("age") Integer age);
@PostMapping("/testPostParams")
String testPostParams(@RequestParam("name") String name, @RequestParam("age") Integer age);
@PostMapping("/testParamsMap")
Map<String,Object> testParamsMap(Map<String,Object> map);
@PostMapping("/testCustomParams")
User testCustomParams(User user);
}
3.4.接口发布
package com.example.controller;
import com.example.entry.User;
import com.example.sevice.TestService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Map;
@RestController
public class Test {
@Resource
private TestService testService;
@RequestMapping("/test")
public Object test(){
String result = testService.test();
return result;
}
/*
但当服务端是GET请求时,那么需要在请求参数前加上@RequestParam注解修饰
*/
@RequestMapping("/testGetParams")
public Object testGetParams(@RequestParam("name") String name, @RequestParam("age") Integer age){
String result = testService.testGetParams(name,age);
return result;
}
@RequestMapping("/testPostParams")
public Object testPostParams(String name, Integer age){
String result = testService.testGetParams(name,age);
return result;
}
@RequestMapping("/testParamsMap")
public Object testParamsMap(@RequestBody Map<String,Object> map){
Map<String,Object> result = testService.testParamsMap(map);
return result;
}
@RequestMapping("/testCustomParams")
public Object testCustomParams(@RequestBody User user){
User result = testService.testCustomParams(user);
return result;
}
}
3.5.启动类编写
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
//激活声明式消费feign的支持
@EnableFeignClients
public class EurekaFeignClientConsumer {
public static void main(String[] args) {
SpringApplication.run(EurekaFeignClientConsumer.class, args);
System.out.println("声明式feign消费者启动ok");
}
}