当前位置:网站首页>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
边栏推荐
- The wisdom of questioning? How to ask questions?
- Equivalence class, boundary value, application method and application scenario of scenario method
- 论文笔记: 多标签学习 MSWL
- Trillions of hot money smashed into the space economy. Is it really a good business?
- What should it personnel over 35 years old do if they are laid off by the company one day?
- ES6 - numerical extension and object extension
- Flex & Bison 開始
- Sword finger offer 46 Translate numbers to strings (DP)
- Exclusive or operator simple logic operation a^=b
- 等价类,边界值,场景法的使用方法和运用场景
猜你喜欢

No absurd tea applet - rule change

1281_ FreeRTOS_ Implementation analysis of vtaskdelayuntil

Glory launched the points mall to support the exchange of various glory products

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

ES6-- 模板字符串、对象的简化写法、箭头函数

ES6学习-- LET

Live800在线客服系统:跨越时空做生意,从每次互动开始

Paper notes: multi tag learning MSWl

ES6 learning -- let

Baidu: in 2022, the top ten hot spots will rise and the profession will be released. There is no suspense about the first place!
随机推荐
Unity技术手册 - 生命周期旋转RotationOverLifetime-速度旋转RotationBySpeed-及外力
Jupiter notebook common shortcut keys
【ModuleBuilder】GP服务实现SDE中两个图层相交选取
String deformation (string case switching and realization)
What is 5g? What can 5g do? What will 5g bring in the future?
UE4_UE5結合offline voice recognition插件做語音識別功能
MySQL数据库索引
ES6 learning -- let
不荒唐的茶小程序-规则改动
ES6 -- formal parameter setting initial value, extension operator, iterator, and generating function
Core points of assembly language
. SQL database import error: / *! 40101 SET @OLD_ COLLATION_ [email protected]@COLLATION_ CONNECTION */
华为云SRE确定性运维专刊(第一期)
Unity technical manual - color in life cycle coloroverlifetime-- speed color colorbyspeed
2022年中职组网络安全新赛题
App new function launch
Trillions of hot money smashed into the space economy. Is it really a good business?
Paper notes: multi tag learning MSWl
Recently prepared to translate foreign high-quality articles
2022年河南省第一届职业技能大赛网络安全项目试题