建网站教程视频下载,购物网站开发英文文献,苏州工业园区一站式服务中心,seo优化心得目录
一、服务提供者
二、服务消费者
三、测试效果
四、开启Hystrix实现服务降级 feign/openfeign和dubbo是常用的微服务RPC框架#xff0c;由于feigin内部已经集成ribbon#xff0c;自带了负载均衡的功能#xff0c;当有多个同名的服务注册到注册中心时#xff0c;会根…目录
一、服务提供者
二、服务消费者
三、测试效果
四、开启Hystrix实现服务降级 feign/openfeign和dubbo是常用的微服务RPC框架由于feigin内部已经集成ribbon自带了负载均衡的功能当有多个同名的服务注册到注册中心时会根据ribbon默认的负载均衡算法将请求分配到不同的服务。这篇文章就简单介绍一下怎么使用feign来调用远程的服务。 首先需要有一个微服务注册中心来提供服务注册与发现本章就使用之前创建的eureka作为注册中心。点击以下文章链接教你快速搭建一个eureka server
springboot整合eureka、config搭建注册中心和配置中心https://blog.csdn.net/heyl163_/article/details/131715281首先要实现服务间的调用需要有服务提供者和服务消费者创建两个项目分别用于服务提供者和服务消费者。
一、服务提供者 创建一个springboot项目取名为provider 1、修改maven配置文件pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.3.4.RELEASE/versionrelativePath//parentgroupIdcom.example/groupIdartifactIdprovider/artifactIdversion0.0.1-SNAPSHOT/versionpropertiesjava.version1.8/java.versioneureka.version1.4.4.RELEASE/eureka.version/propertiesdependencyManagementdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversionHoxton.SR12/versiontypepom/typescopeimport/scope/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-eureka-server/artifactIdversion${eureka.version}/version/dependency/dependencies/dependencyManagementdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-eureka-server/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build
/project
2、修改系统配置文件
server:port: 8085spring:application:name: providereureka:instance:hostname: localhostclient:service-url:defaultZone: http://${eureka.instance.hostname}:8761/eureka
3、创建一个controller 在根目录下创建controller包然后在controller包下创建一个UserController package com.example.provider.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;/*** author heyunlin* version 1.0*/
RestController
RequestMapping(path /user, produces application/json;charsetutf-8)
public class UserController {RequestMapping(value /name, method RequestMethod.GET)public String name() {return heyunlin;}}
4、启动类上添加EnableDiscoveryClient注解
package com.example.provider;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;EnableDiscoveryClient
SpringBootApplication
public class ProviderApplication {public static void main(String[] args) {SpringApplication.run(ProviderApplication.class, args);}} 二、服务消费者
1、创建一个springboot项目并命名为consumer
2、修改pom.xml配置文件
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.3.4.RELEASE/versionrelativePath//parentgroupIdcom.example/groupIdartifactIdconsumer/artifactIdversion0.0.1-SNAPSHOT/versionpropertiesjava.version1.8/java.versioneureka.version1.4.4.RELEASE/eureka.version/propertiesdependencyManagementdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversionHoxton.SR12/versiontypepom/typescopeimport/scope/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-eureka-server/artifactIdversion${eureka.version}/version/dependency/dependencies/dependencyManagementdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-eureka-server/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId/dependency/dependencies
/project
3、修改配置文件注册到eureka
server:port: 8086spring:application:name: consumereureka:instance:hostname: localhostclient:service-url:defaultZone: http://${eureka.instance.hostname}:8761/eureka
4、通过feign调用远程的方法 根目录下创建feign包在feign包下创建一个接口FeignService类名不重要 FeignClient(provider)指定注册到eurka的服务名 RequestMapping的路径写provider服务的控制器接口路径 package com.example.consumer.feign;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;/*** author heyunlin* version 1.0*/
FeignClient(provider)
public interface FeignService {RequestMapping(value /user/name, method RequestMethod.GET)String name();}
5、最后创建一个控制器类类名随便取
package com.example.consumer.controller;import com.example.consumer.feign.FeignService;
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;/*** author heyunlin* version 1.0*/
RestController
RequestMapping(/user)
public class UserController {AutowiredFeignService feignService;RequestMapping(value /name, method RequestMethod.GET)public String name() {return feignService.name();}}
这时候Autowired会报错找不到FeignService的bean因为没有在配置类上面添加EnableFeignClients注解
package com.example.consumer;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;EnableFeignClients
EnableDiscoveryClient
SpringBootApplication
public class ConsumerApplication {public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class, args);}}
三、测试效果
完成以上操作之后依次启动euraka-serverprovider和consumer
浏览器上访问consumer的控制器地址http://localhost:8086/user/name发现成功返回了字符串。 四、开启Hystrix实现服务降级
首先需要开启hystrix
在pom.xml文件中添加配置
feign:hystrix:enabled: true
然后创建一个FeignService的实现类交给spring管理
package com.example.consumer.feign;import org.springframework.stereotype.Component;/*** author heyunlin* version 1.0*/
Component
public class FeignServiceImpl implements FeignService {Overridepublic String name() {return error;}} 最后在FeiginService接口的的FeiginCilent注解上指定fallbackFeignServiceImpl.class
FeignClient(value provider, fallback FeignServiceImpl.class)
完成以上配置之后重启consumer访问http://localhost:8086/user/name时正确调用了provider的控制器方法得到了正确的结果。
接着把关掉provider项目再访问发现调用失败成功执行了配置的降级方法直接返回了error 好了springboot整合feign的介绍到这里就完了代码已开源按需获取~
注册中心
eurekahttps://gitee.com/he-yunlin/eureka.git服务提供者
providerhttps://gitee.com/he-yunlin/provider.git服务消费者
consumerhttps://gitee.com/he-yunlin/consumer.git