从零搭建springcloud(三)服务提供与调用 Feign
在此之前,我们已经搭建了一个eureka-server服务,及两个eureka-client服务,
现在,我们来看微服务之间如何通信。
Feign
Feign是Netflix为我们提供的一个框架,他让我们在服务之间的调用变得更简单。
他其实是一个声明式的Web Service客户端,只需要简单的配置一些注解,编写一些接口,就可以根据其中的http模板配置好http请求。
关于定义不再多说,下面来看如何通过Feign来进行服务间通信
为生产者编写一些方法
首先进入上一节中创建的springcloud-eureka-service服务中
创建HelloController类、Person类
结构如下:
具体代码比较简单 不再详细解释:
package com.yang.springcloudeurekaservice.controller;
import com.yang.springcloudeurekaservice.entity.Person;
import org.springframework.web.bind.annotation.*;
@RestController
public class HelloController {
@RequestMapping(value = "/hello", method= RequestMethod.GET)
public String sayHello(@RequestParam("string") String string){
return string;
}
@RequestMapping(value = "/person", method= RequestMethod.POST)
public Person getPerson(@RequestBody Person person) {
return person;
}
}
package com.yang.springcloudeurekaservice.entity;
public class Person {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
为消费者服务添加依赖
进入上一节中创建的springcloud-eureka-consume服务中,
打开pom.xml文件
新增依赖:
<dependency>
<!--在f版本中,feign版本需要使用openfeign-->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
将服务标记为Feign客户端服务
进入springcloud-eureka-consume服务的启动类中,添加@EnableFeignClients注解
编写服务调用接口
代码如下:
package com.yang.springcloudeurekaconsume.feign;
import com.yang.springcloudeurekaconsume.entity.Person;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
//@FeignClient注解表明这是一个Feign服务调用接口。value中的内容标记了该接口要调用哪个生产者的服务
@FeignClient(value = "eureka-service")
public interface FeignService {
//每个接口都与上面生产者controller中的方法对应。
@RequestMapping(value = "/hello", method= RequestMethod.GET)
String sayHello(@RequestParam("string") String string) ;
@RequestMapping(value = "/person", method= RequestMethod.POST)
String getPerson(@RequestBody Person person) ;
}
消费者Controller类及实体类
目录如下:
代码较简单 如下:
package com.yang.springcloudeurekaconsume.controller;
import com.yang.springcloudeurekaconsume.entity.Person;
import com.yang.springcloudeurekaconsume.feign.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
public class FeignController {
//注入服务调用接口
@Autowired
FeignService feignService;
@RequestMapping("/hello")
public String sayHello(@RequestParam String string){
return feignService.sayHello(string);
}
@RequestMapping("/person")
public String getPerson(@RequestBody Person person){
return feignService.getPerson(person);
}
}
package com.yang.springcloudeurekaconsume.entity;
public class Person {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
测试
依次启动eureka-server、eureka-service、eureka-consume
浏览器访问
http://localhost:8801/hello?string=HelloWorld!
结果为:
表明Feign调用成功。
下一节:从零搭建springcloud(三)配置中心 Config