快速入门
服务提供方添加代码
- 创建controller
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
private final Logger logger = LoggerFactory.getLogger(HelloController.class);
@Value("${spring.application.name}")
private String applicationName;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello(){
logger.info("访问的服务名称是:"+applicationName);
return "hello world :"+applicationName;
}
}
创建服务消费方
- 创建springboot项目添加依赖,
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 在resource下创建application.yml,添加配置。
spring:
application:
name: feign-consumer
server:
port: 9001
eureka:
client:
service-url:
defaultZone: http://springcloudtest.com:1111/eureka/,http://springcloudtest.com:1112/eureka/
- 创建服务调用接口HelloService通过注解@FeignClient注解指定服务名来绑定服务,然后在使用springmvc的注解绑定具体的服务接口。
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient("hello-client1")
public interface HelloService {
@RequestMapping(value = "/hello",method = RequestMethod.GET)
String hello();
}
注意:这里的服务名不区分大小写,所以使用hello-client1或者HELLO-CLINET1都是可以的。
- 创建一个HelloController,来实现对Feign客户端的调用。
import com.dome.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@RequestMapping(value = "/feign-consumer",method = RequestMethod.GET)
public String helloConsumer(){
return helloService.hello();
}
}
- 最后创建主类,EurekaClient1Application并通过@EnableFeignClients注解开启feign的支持功能。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class EurekaClient1Application {
public static void main(String[] args) {
SpringApplication.run(EurekaClient1Application.class,args);
}
}
测试验证
- 分别启动注册中心和两个客户端。
- 使用浏览器测试服务访问服务http://localhost:9001/feign-consumer
- 通过访问可以看到通过访问消费者的接口,我们访问到了提供者的服务,通过feign我们只需要定义服务绑定的接口,就可以实现服务调用。