springCloudalibaba实战三之OpenFeign(远程调用)

更新时间:2023-05-24 03:01:33 阅读: 评论:0

springCloudalibaba实战三之OpenFeign(远程调⽤)
OpenFeign就是springCloud中的组件,⼀个优雅的远程调⽤组件,内部集成了ribbon负载均衡,我前期的博客貌似⼜写过它的集中负载⽅式,以及⾃定义负载
实例化restTemplater,⾃定义负载机制,如果要了解springCloud的创建过程,
@Configuration
public class ConfigBeans {
//boot -->spring  l --- @Configuration配置  ConfigBean = l
@Bean
伍德苏铁
@LoadBalanced//Spring Cloud Ribbon是基于Netflix Ribbon实现的⼀套客户端负载均衡的⼯具。
public RestTemplate getRestTemplate()
{
return new RestTemplate();
}
@Bean
public IRule myRule()
{
//return new RoundRobinRule();//该策略实现安装线性轮询的⽅式依次选择每个服务实例
return new RandomRule();//该策略实现安随机算法,
//return new RetryRule();//具备重试机制的实例选择功能morning采⽤的轮训
}
}
这⾥就不多说了,直接进⼊feign的打开⽅式
在公共项⽬中sca-common创建feign⽂件夹,专门⽤来对外暴露接⼝,供其他服务调⽤
创建BalanceFeignService,准备在account-rvice模块中调⽤balance-rvice的接⼝
注解@FeignClient的属性
name = "payment-rvice"  #被调⽤的服务名/
fallback = BalanceFeignService.BalanceFeignServiceFallback.class #服务出错时,调⽤该类的⽅法, 服务降级类
qualifier="paymentService" #限定服务调⽤名称,就是起了给名字
configuration = ServiceFeignConfiguration.class #feign的配置类,⽐如超时等等
其他属性请⾃⾏去了解,
内部类中记得加@Component注解,以便能被spring扫描加载
代码
package com.hc.scacommon.feign;
import com.hc.scacommon.pojo.Balance;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/
/qualifier="paymentService",
//configuration = ServiceFeignConfiguration.class,
@FeignClient(name = "payment-rvice", fallback = BalanceFeignService.BalanceFeignServiceFallback.class) public interface BalanceFeignService {
//远程调⽤
@RequestMapping(value = "/pay/balance", method = RequestMethod.GET)
Balance getBalance(@RequestParam("id") Integer id);
//请求不到,则转⼊降级
@Component
class BalanceFeignServiceFallback implements BalanceFeignService {
@Override
public Balance getBalance(Integer id) {
return new Balance(0, 0, 0, "服务降级,该服务已下线");
}
}
}
创建⼦项⽬account-rvice
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"        xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>
合肥健身
<parent>
<groupId>com.hc</groupId>
<artifactId>sca-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hc</groupId>better man 林忆莲
<artifactId>account-rvice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>account-rvice</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
<alibaba.version>0.9.0.RELEASE</alibaba.version>
<spring-cloud-netflix.version>2.1.1.RELEASE</spring-cloud-netflix.version>
<spring-cloud-openfeign.version>2.1.1.RELEASE</spring-cloud-openfeign.version>
</properties>
服装行业特点<dependencies>
<dependency>
<groupId>com.hc</groupId>
<artifactId>sca-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${alibaba.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>            <version>${alibaba.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
fite<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
fresh start
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
baptize
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-dependencies</artifactId>
<version>${spring-cloud-openfeign.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>怎样使记忆力提高
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugins>
</build>
</project>
spring:
application:
name: account-rvice
cloud:
nacos:
config:
rver-addr: 127.0.0.1:8848
spring:
profiles:
active: dev
cloud:
nacos:
discovery:
rver-addr: 127.0.0.1:8848
rver:
port: 8081
feign:
client:
config:
default:        #default默认所有服务的超时时间
connect-timeout: 10000
read-timeout: 20000
#      payment-rvice:    #指定payment-rvice这个服务的超时时间#        connect-timeout: 10000
#        read-timeout: 20000
controller
package com.ller;
import com.hc.scacommon.feign.BalanceFeignService;
import com.hc.scacommon.pojo.Balance;
import com.hc.scacommon.pojo.Ur;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class AccountController {
//@Qualifier("paymentService")
@Autowired
private BalanceFeignService balanceFeignService;
final static Map<Integer, Ur> urMap = new HashMap() {{
put(1, new Ur(1, "张三"));
put(2, new Ur(2, "李四"));
put(3, new Ur(3, "王五"));
}
};
@RequestMapping("/acc/ur")
public Ur getUr(@RequestParam Integer id) {
if(id != null && ainsKey(id)) {
Ur ur = (id);
Balance balance = Balance(id);
if(balance != null){
ur.tBalance(balance);
}
return ur;
}
return new Ur(0, "");
}
}
启动类
package com.hc.accountrvice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
firmness@EnableFeignClients
public class AccountServiceApplication {
public static void main(String[] args) {
SpringApplication.run(AccountServiceApplication.class, args);appeared
}
}

本文发布于:2023-05-24 03:01:33,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/120342.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:服务   机制   负载   实现   创建
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图