以前学习java,一般就一个后端,都要学习如何在容器中运行,如tomcat,weblogic,现在微服务颠覆了这一切,一个系统要被拆分成多个服务,服务与服务间需要通信,让我想到了前端的ajax,java里可没js那样方便,一般使用resttemplate,httpclient。现在springcloud又带来了一种新的服务调用方式--feign。
下面,我们创建一个工程测试feign,先启动前面讲的注册中心,feign客户端作为一个消费端,还需要一个提供端。
创建消费端,工程依赖如下(这里使用boot1.5.x):
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Edgware.SR4'
}
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework.boot:spring-boot-starter-web')
compile 'org.slf4j:slf4j-api:1.7.14'
compile('org.springframework.cloud:spring-cloud-starter-eureka')
compile('org.springframework.cloud:spring-cloud-starter-feign')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
接着配置端口并注册到注册中心,如下:
spring.application.name=feign-consumer
# 单机
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
server.port=8083
启动类加上注解,一个用于服务发现,一个用于feign客户端,可通过EnableFeignClients调用其他服务的api,如下:
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class CloudApplication {
public static void main(String[] args) {
SpringApplication.run(CloudApplication.class, args);
}
}
接着编写service,如下:
@FeignClient(name="HELLO-SERVICE", fallback = HelloServiceFallback.class)
public interface HelloService {
@RequestMapping("/hello")
String hello();
@RequestMapping(value = "/hello1", method = RequestMethod.GET)
String hello(@RequestParam("name") String name) ;
@RequestMapping(value = "/hello2", method = RequestMethod.GET)
User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age);
@RequestMapping(value = "/hello3", method = RequestMethod.POST)
String hello(@RequestBody User user);
}
上面定义一个feign客户端,它指定了要消费的服务名以及降级的处理类,若调用service.hello()则会发起对应请求:http://HELLO-SERVICE/hello
降级处理类也很简单,只需实现service接口即可。
@Component
public class HelloServiceFallback implements HelloService {
@Override
public String hello() {
return "error";
}
@Override
public String hello(@RequestParam("name") String name) {
return "error";
}
@Override
public User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age) {
return new User("nothing", 0);
}
@Override
public String hello(@RequestBody User user) {
return "error";
}
}
其实上面的方法,实现比较繁琐,我们可以用更简单的方式,如下,
@RequestMapping("/refactor")
public interface HelloService {
@RequestMapping(value = "/hello1", method = RequestMethod.GET)
String hello(@RequestParam("name") String name) ;
@RequestMapping(value = "/hello2", method = RequestMethod.GET)
User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age);
@RequestMapping(value = "/hello3", method = RequestMethod.POST)
String hello(@RequestBody User user);
}
上面我们重构了service接口,将所有requestMapping写入,其实与上面的变化也不大,最主要的区别是它可以被多模块共享,可以以最简方式创建feignClient,下面看下feignClient的实现,如下:
@FeignClient(value = "HELLO-SERVICE")
public interface RefactorHelloService extends HelloService {
}
这样是不是很简单呢
下面我们编写controller,只需注入上面的服务即可。
@RestController
public class ConsumerController {
@Autowired
HelloService helloService;
@Autowired
RefactorHelloService refactorHelloService;
@RequestMapping(value = "/feign-consumer", method = RequestMethod.GET)
public String helloConsumer() {
return helloService.hello();
}
@RequestMapping(value = "/feign-consumer2", method = RequestMethod.GET)
public String helloConsumer2() {
StringBuilder sb = new StringBuilder();
sb.append(helloService.hello()).append("\n");
sb.append(helloService.hello("DIDI")).append("\n");
sb.append(helloService.hello("DIDI", 30)).append("\n");
sb.append(helloService.hello(new User("DIDI", 30))).append("\n");
return sb.toString();
}
@RequestMapping(value = "/feign-consumer3", method = RequestMethod.GET)
public String helloConsumer3() {
StringBuilder sb = new StringBuilder();
sb.append(refactorHelloService.hello("MIMI")).append("\n");
sb.append(refactorHelloService.hello("MIMI", 20)).append("\n");
sb.append(refactorHelloService.hello(new User("MIMI", 20))).append("\n");
return sb.toString();
}
}
上面主要讲了消费服务的创建,提供服务的创建请参考另一篇文章 SpringCloud-service 服务提供
学习交流,请加群:64691032