当前位置:网站首页>Fegin client entry test
Fegin client entry test
2022-06-25 23:20:00 【WhyLearnCode】
Fegin Client- Introductory quiz
List of articles
1 Interface provider
1.1 To configure :pom.yml and bootstrap.yml
1) pom.xml Add the following dependencies
| artifactId | explain | edition |
|---|---|---|
| spring-cloud-starter-alibaba-nacos-discovery | nacos Registry dependent packages | |
| spring-boot-starter-actuator | Monitoring checks | |
| spring-cloud-starter-alibaba-nacos-config | nacos Configuration center depends on support | |
| spring-cloud-dependencies | Spring Cloud begin | Greenwich.SR2 |
| spring-cloud-alibaba-dependencies | Spring Cloud Alibaba begin | 2.1.0.RELEASE |
Pay attention to the version problem ,spring-boot 、spring-cloud as well as spring.cloud.alibaba
and The difference between
Main management version , It is useful for subclasses to inherit from the same parent class , Centrally manage dependent versions without adding dependencies , For the version defined therein , Son pom You don't have to inherit from your father pom Defined version . Medium jar Add directly to the project , Managing dependencies ( If there is a father pom, Son pom, Then son pom Can only passively accept the version of the parent class );
2) bootstrap.yml
bootstrap.yml and application.yml The difference between
a. Use spring cloud need bootstrap.yml Provide... Before the project loads server Configuration information , As boot configuration .
server:
port: 9005
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/myqx?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 1234
application:
name: itcast-map
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
server-addr: localhost:8848
1.2 Provide the interface
@Autowired
private UserInfoService userInfoService;
@GetMapping("/get")
public List<UserInfo> findAll() {
List<UserInfo> result = userInfoService.findAll();
return result;
}
Only the configuration and interfaces are exposed here , The details are CURD Simple interface implementation , No need to pay attention. , Just know it's through
2 Service caller
2.1 To configure :pom.yml and bootstrap.yml
No business dependencies are required compared to service providers , Such as mybatis-plus、mysql etc. , Also need to nacos Registry dependent packages 、 Monitoring checks 、nacos Configuration center dependency , You need to add remote calls and Feign rely on .
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<!-- Spring Cloud Version information --> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> <!-- Spring Cloud Alibaba Version information --> <spring.cloud.alibaba.version>2.1.0.RELEASE</spring.cloud.alibaba.version>
</properties>
<!-- The parent project -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<!-- Spring Cloud begin--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Spring Cloud end--> <!-- Spring Cloud Alibaba begin--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring.cloud.alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Spring Cloud Alibaba end -->
</dependencies>
</dependencyManagement>
<dependencies>
<!--SpringMVC--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- tool kit --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> <scope>provided</scope> </dependency> <!-- restTemplate The remote invocation --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.5</version> </dependency> <!-- nacos Registry dependent packages --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- Monitoring checks --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- nacos Configuration center depends on support --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
<!-- feign call --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
</dependencies>
2) bootstrap.yml
server:
port: 9013
spring:
application:
name: itcast-nacos
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
server-addr: localhost:8848
2.2 Start class
You need to add... On the startup class @SpringBootApplication、@EnableDiscoveryClient、@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients // Open remote call
public class NacosApplication {
public static void main(String[] args) {
SpringApplication.run(NacosApplication.class, args);
}
}
2.3 Interface
With the interface caller Controller The difference between writing interfaces is , Service callers are used to create interface classes @FeignClien Annotation implementation
@FeignClien annotation
@FeignClient The common attributes of tags are as follows :
name: Appoint FeignClient The name of , If the project is used Ribbon,name The property will be used as the name of the microservice , For service discovery
url: url Generally used for debugging , Can be specified manually @FeignClient The address of the call
decode404: Happen when http 404 When it's wrong , If the word segment true, Would call decoder decode , Otherwise throw FeignException
configuration: Feign Configuration class , You can customize Feign Of Encoder、Decoder、LogLevel、Contract
fallback: Defines fault-tolerant handling classes , When the call to the remote interface fails or times out , The fault-tolerant logic of the corresponding interface is invoked ,fallback The specified class must be implemented @FeignClient Tagged interface
fallbackFactory: Factory , Used to generate fallback Class example , With this property we can implement fault tolerance logic common to each interface , Reduce duplicate code
path: Define the current FeignClient Unified prefix of
1、 The interface provider is in the registry .
If the service provider has registered with the registry , that name perhaps value The value of is : Service provider's service name . One... Must be specified for all clients name perhaps value
@FeignClient(value=“run-product”,fallback = ProductClientServiceFallBack.class)2、 A single one http Interface , The interface provider is not registered with the registry .
@FeignClient(name=“runClient11111”,url=“localhost:8001”)
here name The value of is : The name of the calling client .
@FeignClient(value = "itcast-map",fallback = UserHystrix.class)
public interface UserFegin {
@GetMapping("user/get")
public List<UserInfo> findAll();
}
2.4 Fault tolerant processing class
@Component
public class UserHystrix implements UserFegin {
@Override
public List<UserInfo> findAll() {
return null;
}
}
2.5 UserController
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private UserFegin userFegin;
/** * Parameters * @return */
@GetMapping("/test006")
public String feignClien() {
List<UserInfo> all = userFegin.findAll();
System.out.println(all);
return "myqxin";
}
3 nacos
3.1 Start a local nacos
No building nacos You can refer to windows install nacos Set up a registration center

3.2 Start two project services
stay nacos View the two service descriptions in to see if the registration is successful

request :http://localhost:9013/user/test006 , The effect is as follows

4 common problem
- spring-boot 、spring-cloud as well as spring.cloud.alibaba Version inconsistency
5 reference
Use nacos As a registry to achieve @FeignClient
边栏推荐
- ES6-- 集合
- Mysql database index
- String deformation (string case switching and realization)
- 小程序-视图与逻辑
- Unity技术手册 - 粒子发射和生命周期内速度子模块
- Multithreaded learning 1
- 作为一个程序员我们如何快乐的学习成长进步呢?(个人感悟和技术无关)
- Basic concepts of processor scheduling
- Unity technical manual - particle emission and life cycle velocity sub module
- 22 years of a doctor in Huawei
猜你喜欢

Circuit module analysis exercise 6 (switch)

22 years of a doctor in Huawei

ES6 -- formal parameter setting initial value, extension operator, iterator, and generating function
[email protected]@COLLATION_CONNECTION */"/>.sql数据库导入错误:/*!40101 SET @[email protected]@COLLATION_CONNECTION */

Civil Aviation Administration: by 2025, China will initially build a safe, intelligent, efficient and green aviation logistics system

Three layer architecture + routing experiment

Why is the frame rate calculated by opencv wrong?

百度:2022年十大热度攀升专业出炉,第一名无悬念!

民航局:到 2025 年我国将初步建成安全、智慧、高效和绿色的航空物流体系

Another breakthrough! Alibaba cloud enters the Gartner cloud AI developer service Challenger quadrant
随机推荐
汇编语言核心要点
作为一个程序员我们如何快乐的学习成长进步呢?(个人感悟和技术无关)
pdm导入vscode的实现方式
22 years of a doctor in Huawei
Basic concepts of processor scheduling
LM small programmable controller software (based on CoDeSys) note XVII: PTO pulse function block
Transformers load pre training model
牛客小白月賽52--E 分組求對數和(二分)
关闭MongoDB一些服务需要注意的地方(以及开启的相关命令)
万亿热钱砸向太空经济,真的是一门好生意?
Jupiter notebook common shortcut keys
最近准备翻译外国优质文章
Pit resolution encountered using East OCR (compile LAMS)
Baidu: in 2022, the top ten hot spots will rise and the profession will be released. There is no suspense about the first place!
Somme logarithmique (deux points) pour le Groupe 52 - - e de la course de la lune blanche de niuke
CDN加速是什么
Unity technical manual - getKey and getaxis and getbutton
判断预约时间是否已经过期
Unity技术手册 - 生命周期旋转RotationOverLifetime-速度旋转RotationBySpeed-及外力
Ribbon core ⼼ source code analysis