医疗器械网站制作,wordpress科技模板,揭阳市住房和城乡建设局官方网站,宣传平台有哪些类型以前学习java#xff0c;一般就一个后端#xff0c;都要学习如何在容器中运行#xff0c;如tomcat#xff0c;weblogic#xff0c;现在微服务颠覆了这一切#xff0c;一个系统要被拆分成多个服务#xff0c;服务与服务间需要通信#xff0c;让我想到了前端的ajax#…以前学习java一般就一个后端都要学习如何在容器中运行如tomcatweblogic现在微服务颠覆了这一切一个系统要被拆分成多个服务服务与服务间需要通信让我想到了前端的ajaxjava里可没js那样方便一般使用resttemplatehttpclient。现在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.14compile(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.namefeign-consumer# 单机eureka.client.serviceUrl.defaultZonehttp://localhost:1111/eureka/server.port8083启动类加上注解一个用于服务发现一个用于feign客户端可通过EnableFeignClients调用其他服务的api如下EnableFeignClientsEnableDiscoveryClientSpringBootApplicationpublic class CloudApplication {public static void main(String[] args) {SpringApplication.run(CloudApplication.class, args);}}接着编写service如下FeignClient(nameHELLO-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接口即可。Componentpublic class HelloServiceFallback implements HelloService {Overridepublic String hello() {return error;}Overridepublic String hello(RequestParam(name) String name) {return error;}Overridepublic User hello(RequestHeader(name) String name, RequestHeader(age) Integer age) {return new User(nothing, 0);}Overridepublic 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只需注入上面的服务即可。RestControllerpublic class ConsumerController {AutowiredHelloService helloService;AutowiredRefactorHelloService 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