项目搭建看前两篇
1.在Ribbon讲解时创建的项目基础上创建新的模块
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>com.wxz</groupId>
<artifactId>springcloud-demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<!--feign依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId> spring-cloud-starter-openfeign</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<!--urekaClient 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.1.5.RELEASE</version>
</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>
启动类:
package com.wxz.eurekafeignclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class EurekaFeignClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaFeignClientApplication.class, args);
}
}
创建接口:
package com.wxz.eurekafeignclient.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient("eureka-client") //指定药访问的服务名
public interface FService {
//指定在服务提供者中的路径和参数
@RequestMapping(method=RequestMethod.GET,value ="/sayHello")
String sayHello(@RequestParam("name")String name);
}
配置类:
package com.wxz.eurekafeignclient.config;
import feign.Retryer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Wangxingze
* @date 2019-08-22 20:52
*/
@Configuration
public class FeignCongig {
/**
* 注入该Bean后远程调用失败会进行重试
* @return
*/
@Bean
public Retryer feignRetryer(){
return new Retryer.Default(100,1000,5);
}
}
创建另外的调用接口和实现:注意实现的接口不是上面的
package com.wxz.eurekafeignclient.service;
public interface FHService {
public String sayHello(String name);
}
package com.wxz.eurekafeignclient.service.serviceImpl;
import com.wxz.eurekafeignclient.service.FService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author Wangxingze
* @date 2019-08-22 21:07
*/
@Service
public class FHServiceImpl implements com.wxz.eurekafeignclient.service.FHService {
@Autowired
private FService fService;
@Override
public String sayHello(String name) {
return fService.sayHello(name);
}
}
yml文件:
---
spring:
application:
name: eureka-feign-client1
server:
port: 8766
eureka:
client:
register-with-eureka: true #注册
fetch-registry: true #要获取注册表
serviceUrl:
serverZone: http//localhost:8761/eureka/
controller:
package com.wxz.eurekafeignclient.controller;
import com.wxz.eurekafeignclient.service.serviceImpl.FHServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Wangxingze
* @date 2019-08-22 21:11
*/
@RestController
@RequestMapping(value = "/f")
public class FController {
@Autowired
private FHServiceImpl fhService;
@GetMapping(value = "/sayHello")
public String sayHello(@RequestParam("name") String name){
return fhService.sayHello(name);
}
}
启动注册中心-》两个服务提供者-》消费者
访问:
再次: