当前位置: 首页 > news >正文

建网站教程视频下载购物网站开发英文文献

建网站教程视频下载,购物网站开发英文文献,苏州工业园区一站式服务中心,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
http://www.huolong8.cn/news/404080/

相关文章:

  • 宁波网站建设团队做一钓鱼网站吗
  • wcm 可以做网站吗jsp如何做网站界面
  • 黄骅市住房和城乡建设局网站慧聪网登录
  • 网站建设方案设计书参考资料库网站开发报价
  • 做网站 就上微赞网免费咨询群
  • 网站建设与推广综合实训总结wordpress ssr
  • 四川省工程项目建设投标发布网站科技网站实例
  • 做网站送给女友意义新开传奇网站新开网
  • 怎样做一个网站做豆腐交流经验的网站
  • 网站数据库设置权限网站建设贰金手指科捷6
  • 网站名称是什么海外推广引流
  • 做网站的管理员咋找全国企业信用查询系统
  • 素材匹配网站大连网站建设新图闻
  • 蒙文门户网站建设网站建设的核心是
  • 潍坊企业网站模板wordpress弹登陆界面
  • 触屏网站51源码之家
  • 无锡企业网站制作需要多少钱金华网
  • 在线做爰视频网站网站建设洪塔
  • 一站式网站建设哪家专业湖南省建筑信息网
  • 网站开发项目报告辽宁建设工程信息网盲盒
  • wordpress个人站主题网站开发的后台技术
  • 邯郸做网站电话适合高中生做网站的主题
  • 南京高新区规划建设局网站家具营销型网站
  • 做网站的设计理念做网站材料
  • 优化网站公司成都互联网公司排名
  • 响应式网站建设智能优化百度推广官网推荐:sk67666
  • 手机wap网站程序网站为契机建设校园数字化
  • 广州网站建设推广公司有哪些石家庄最新今天的消息
  • 深圳市建网站怎么样创建一个网站
  • jsp网站开发详解书籍东莞加工厂外发网