接着前两节:
Eureka服务注册:https://blog.csdn.net/weixin_37460672/article/details/90414085
Ribbon负载均衡:https://blog.csdn.net/weixin_37460672/article/details/90451192
1.创建Module工程的springboot项目eureka-feign-client
2.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.wl</groupId>
<artifactId>springcloud20190521</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.wl</groupId>
<artifactId>eureka-feign-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-feign-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.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-feign</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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.application.yml配置文件配置程序名eureka-feign-client,端口号8765,服务注册地址http://localhost:8761/eureka/
spring:
application:
name: eureka-feign-client
server:
port: 8765
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
4.启动类加注解@EnableEurekaClient开启EurekaClient功能,加注解@EnableFeignClients开启FeignClient功能
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class EurekaFeignClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaFeignClientApplication.class, args);
}
}
5.已经具备Feign功能,现在实现一个简单的FeignClient,新建EurekaClientFeign接口,接口加注解@FeignClient,其中value为远程调用其他服务名,FeignConfig.class为FeignClient的配置类,接口内有一个sayHiFromClientEureka()方法,该方法通过Feign来调用eureka-client服务的"/hi"的API接口
/**
* Created by lailai on 2019/5/23.
*/
@FeignClient(value = "eureka-client",configuration = FeignConfig.class)
public interface EurekaClientFeign {
@GetMapping("hi")
String sayHiFromClientEureka(@RequestParam(value = "name") String name);
}
6.FeignConfig配置类,注入Retryer的bean,远程调用失败后会重试
/**
* Created by lailai on 2019/5/23.
*/
@Configuration
public class FeignConfig {
@Bean
public Retryer feignRetryer(){
return new Retryer.Default(100,SECONDS.toMillis(1),5);
}
}
7.在Service层注入EurekaFeignClient,调用sayHiFromClientEureka()方法
/**
* Created by lailai on 2019/5/23.
*/
@Service
public class HiService {
@Autowired
private EurekaClientFeign eurekaClientFeign;
public String sayHi(String name) {
return eurekaClientFeign.sayHiFromClientEureka(name);
}
}
8.创建HiController调用HiService,HiService通过EurekaClientFeign远程调用eureka-client服务的API接口"/hi"
/**
* Created by lailai on 2019/5/23.
*/
@RestController
public class HiController {
@Autowired
private HiService hiService;
@RequestMapping("hi")
public String sayHi(@RequestParam(defaultValue = "admin",required = false) String name){
return hiService.sayHi(name);
}
}
9.启动eureka-server工程端口8761,启动两个eureka-client工程,端口分别为8762,8763,启动eureka-feign-client工程,端口8765
10.轮流访问http://localhost:8765/hi?name=weilai地址