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

个人网站免费申请注册阿里云做的网站程序

个人网站免费申请注册,阿里云做的网站程序,企业网站建设规划的基本原则有哪些,什么是营销型的网站推广Spring Cloud中集成Feign (只是笔记而已 其中有点命名啥的不对应,搜到了就划走吧) Feign--[feɪn]:Web 服务客户端,能够简化 HTTP 接口的调用。 没有Feign的之前服务提供者 package com.springcloudprovide.controller;import com.springclo…

Spring Cloud中集成Feign

        (只是笔记而已 其中有点命名啥的不对应,搜到了就划走吧)

        Feign--[feɪn]:Web 服务客户端,能够简化 HTTP 接口的调用。

        没有Feign的之前服务提供者

package com.springcloudprovide.controller;import com.springcloudprovide.service.UserName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class UserController {@Autowiredprivate UserName userName;@RequestMapping("username")public String getUserName(){return userName.getUsername();}
}

        服务消费者,通过RestTemplate来调用服务提供者的服务接口

package com.springcloudconsumer.tempalte;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;@Configuration
public class RestTemplateConfiguration {@Bean@LoadBalancedpublic RestTemplate getRestTmplate(){return new RestTemplate();}
}
package com.springcloudconsumer.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;@RestController
public class UserBeanController {@Autowiredprivate RestTemplate restTemplate;@RequestMapping("/showName")public String getUserName(){System.out.println("123");return restTemplate.getForObject("http://localhost:8081/username", String.class);}
}

Feign的使用

创建基于Eureka模块的注册中心和服务提供者

基于Feign的服务消费者

 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId><version>3.0.2</version></dependency><!--  openfeign--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>3.0.2</version></dependency>

定义Feign的客户端接口

package com.springcloud.feign.service;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;@FeignClient(value = "eurekaprovider")
public interface UserServiceFeign {@RequestMapping("username")public String getUserName();
}

这个注解@FeignClient标识当前是一个 Feign 的客户端,value 属性是对应的服务名称,也就是你需要调用哪个服务中的接口。

创建服务消费者的控制类

package com.wangxing.eurekacomsumer.eurekacomsumerdemo.controller;
import com.wangxing.eurekacomsumer.eurekacomsumerdemo.serviceclient.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;=
@RestController
public class UserBeanController {//注入一个Feign的客户端对象@Autowiredprivate UserFeignClient  userFeignClient;@RequestMapping("/showName.do")public  String  getUserName(){return  userFeignClient.getUserName();}
}

配置主类

package com.springcloud.feign;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableHystrix
@EnableFeignClients
public class SpringcloudFeignApplication {public static void main(String[] args) {SpringApplication.run(SpringcloudFeignApplication.class, args);}}

如果你的 Feign 接口定义跟你的启动类不在同一个包名下,还需要制定扫描的包名 @EnableFeignClients(basePackages=“com.fangjia.api.client”)

通过跟之前的代码相比可以发现,我们的调用方式变得越来越简单了,从最开始的指定地址,到后面通过 Eureka 中的服务名称来调用,再到现在直接通过定义接口来调用。

Feign 提供了很多的扩展机制,让用户可以更加灵活的使用,这节我们来学习 Feign 的一些自定义配置。
使用配置自定义 Feign 的配置
除了使用代码的方式来对 Feign 进行配置,我们还可以通过配置文件的方式来指定 Feign 的配置。
# 链接超时时间
feign.client.config.feignName.connectTimeout=5000
# 读取超时时间
feign.client.config.feignName.readTimeout=5000
# 日志等级
feign.client.config.feignName.loggerLevel=full
# 重试
feign.client.config.feignName.retryer=com.example.SimpleRetryer
# 拦截器
feign.client.config.feignName.requestInterceptors[0]=com.example.FooRequestInterceptor
feign.client.config.feignName.requestInterceptors[1]=com.example.BarRequestInterceptor
# 编码器
feign.client.config.feignName.encoder=com.example.SimpleEncoder
# 解码器
feign.client.config.feignName.decoder=com.example.SimpleDecoder
# 契约
feign.client.config.feignName.contract=com.example.SimpleContract

http://www.yayakq.cn/news/296323/

相关文章:

  • 网站的模板演示怎么做个人介绍网页
  • 电子商务网站营销方案wordpress 调用分类文章
  • 点评网站建设城乡建设网站 资料员
  • 怎么用手机做刷赞网站公司介绍网页界面设计
  • 有什么网站是帮别人做设计的博罗网站建设哪家好
  • 如何在外管局网站做付汇延期wordpress 个性图标
  • 网站从建设到运行要多少钱网站建设一般需要几个步骤
  • 网站建设思路设计代做百度首页排名价格
  • 网站建设培训中心手机移动网站开发
  • 网站艺术设计99国精产品灬源码的优势
  • 郑州做网站找谁个人网站做淘宝客容易封吗
  • 营销型网站建设需要备案吗网站建设需要哪些设备
  • 织梦网站加网站地图做网站凡科如何
  • 做一个网站先做前段 还是后端旅游网站后台模板
  • 重庆网站开发设计公司林州网站建设
  • 各行业网站建设方案书万网商标查询
  • 加强图书馆网站建设ui交互设计课程培训
  • 诸城网站建设开发网站建设回龙观
  • 水土保持与生态建设网站搜索百度指数
  • 网站建设关键词布局成都网站制作设计公司
  • 北极动力网站建设常用设计网站有哪些软件
  • 浏览器如何做购物网站用python做网站的公司
  • wordpress怎么截图直接粘贴网站结构优化的优化包括
  • 中英文网站开发费用韩国优秀平面设计网站
  • 扬州市建设局网站 竣工备案网商之窗挂
  • 中国建设银行网站多少wordpress+禁用feed
  • 网站设计培训课程推广网页模板
  • 企业网站建设 英铭石龙镇网站仿做
  • 外贸网站建设服务wordpress和teakki
  • 沈阳做网站需要多少钱元宇宙app技术开发