【第二种 通过Feign】
1.需要单独建立一个接口,接口上通过@ FeignClient(“服务名”)注解来调用指定的服务。接口中编写一个方法,该方法提供的接口就是 我们需要调用的服务的接口名。同时如果我们需要调用的服务的那个接口是需要传参的,那么这个方法我们也需要传入参数。
2.在启动类上加注解@EnableFeignClients 来开启Feign的功能。
3.提供接口,暴露服务。
步骤一:编写配置文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:10001/eureka/
server:
port: 8085
spring:
application:
name: service-feign
步骤二:建立接口
package com.cloud.interfac;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
//调用了service-hello服务的“/hello”接口
@FeignClient(value="service-hello")
public interface SchedualServiceHello {
@RequestMapping(value="/hello",method=RequestMethod.GET)
String sayHelloFromClientOne(@RequestParam(value="name")String name);
}
步骤三:在启动类上加注解@EnableFeignClients 来开启Feign的功能。
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceFeignApplication.class, args);
System.out.println("*******Feign 已开启************");
}
}
步骤四:提供接口,暴露服务。
package com.cloud.controller;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.cloud.interfac.SchedualServiceHello;
@RestController
public class HelloController {
@Autowired
SchedualServiceHello schedualServiceHello;
@RequestMapping(value="/hello",method=RequestMethod.GET)
public String hello(@RequestParam String name) {
return schedualServiceHello.sayHelloFromClientOne(name);
}
}
看一下pom文件中的依赖:
<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>springcloud-service-feign</groupId>
<artifactId>springcloud-service-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<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>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>