当前位置:网站首页>eureka client 消费者
eureka client 消费者
2022-07-18 13:54:00 【dzl84394】
上一个文章介绍了如何搭建一个 消费者 eureka client,如何注册到server,这篇文章介绍如何消费。
一般情况下有2种方式:一种是ribbon+restTemplate,另一种是feign,来调用服务
1、 ribbon+restTemplate
这里需要注意的就是负载均衡
2、feign
2.1、pom.xml添加openfeign
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>cn.dails</groupId>
<artifactId>dails-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath></relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dms-asset</artifactId>
<name>dms-asset 客户资产</name>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<build>
<finalName>dms-asset</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2、添加一个接口FeignClient
package cn.dms.service.feignClient;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "dms-user")
public interface UserService {
@RequestMapping(value = "/user/private/findUser",method = RequestMethod.GET)
String findUser(@RequestParam(value = "name") String name);
}
2.3、启动类添加@EnableFeignClients
package cn.dms;
import cn.dms.service.feignClient.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@RestController
public class AssetApplication {
public static void main(String[] args) {
SpringApplication.run( AssetApplication.class, args );
}
@Value("${server.port}")
String port;
@Autowired
UserService userInterface;
@RequestMapping("/hello")
public String home(@RequestParam(value = "name", defaultValue = "屌丝") String name) {
return userInterface.findUser("屌丝");
}
}
边栏推荐
- 恒烁半导体通过注册:年营收5.8亿 实控人为美籍华人
- Several other filters of OpenCV (square box filter and mean filter)
- 飞腾回应“造假”传闻:CPU芯片全部为自主知识产权!
- 大名鼎鼎的YOLO、PageRank影响力爆棚的研究,曾被CS顶会拒稿
- 图神经网络框架DGL学习 103——信息传递 (Message Passing Tutorial)
- [flask framework] II Jinja2 template of flask framework
- Digitalization has not gone far, and the era of digital intelligence has come
- Economic Daily: don't treat digital collections as "money making" products
- Service registration of Nacos registry
- Permission control of RBAC data model based on Database
猜你喜欢
Examples of neural network and graph neural network models for chemical molecules using deepchem
图像滤波基本知识
OpenCV的其他几个滤波器(方盒滤波和均值滤波)
图神经网络框架DGL学习 102——图、节点、边及其特征赋值
LeetCode_ 40_ Combined sum II
Over fitting and over fitting processing
Arduino -- self made Arduino burning bootloader method
MySQL数据库优化
恒烁半导体通过注册:年营收5.8亿 实控人为美籍华人
Luwei optoelectronics is registered through the science and Technology Innovation Board: with an annual revenue of 500million, Du wubing is the actual controller
随机推荐
二叉树刷题(三)
What is the importance of PCB plug hole process?
Luwei optoelectronics is registered through the science and Technology Innovation Board: with an annual revenue of 500million, Du wubing is the actual controller
Tree-Structured LSTM
【Renesas RA6M4开发板之DHT11温湿度读取】
Tongxing technology IPO meeting: annual revenue of 780million Zhang liangcan family is the actual controller
Chen Tianqi, Wang William and others recommend: a little advice from the winner of ACL best paper award to new researchers
Conduct software testing from 0 to 1
DeepChem-使用图卷积神经网络对化学分子建模
Deepchem data type -- how to input molecules into neural network models
【Flask框架】二. Flask框架之Jinja2模板
图神经网络框架DGL学习——101(入门)
Blog Automated Deployment
10GBASE XX related knowledge points
Hanyi shares passed the registration: the annual revenue was 220million, and Xie Liqun controlled 35% of the company's equity
乡村夜游项目如何打造创新体验模式
关于以ethtool为主的网络指标统计工具之间统计数据关系的研究
seaborn颜色
Permission control of RBAC data model based on Database
Management of jupyter kernel environment