SpringCloud 之 服务提供者

前提

便于理解,我修改了本地域名=》这里!!!

127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com

学习Rest实例之提供者

在这里插入图片描述

提供者模块展示

1、导入依赖

<!--        实体类 + Web--><dependency><groupId>com.jyl</groupId><artifactId>springcloud-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-core</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</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></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId></dependency>
<!--        热部署--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka --><!-- eureka       --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId><version>1.4.6.RELEASE</version></dependency>
<!--        解决 eureka Status  actuator/info 完善监控信息--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>

2、application.yml 用户级配置文件

配置端口号、mybatis配置、spring配置、eureka配置、监控信息配置

server:port: 8001
# mybatis 配置mybatis:type-aliases-package: com.jyl.springcloud.pojoconfig-location: classpath:mybatis/mybatis-config.xmlmapper-locations: classpath:mapper/*.xml# spring 的配置spring:application:name: springcloud-provider-deptdatasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: org.gjt.mm.mysql.Driverurl: jdbc:mysql://localhost:3306/db01?useUnicode=true&useSSL=false&characterEncoding=utf-8username: rootpassword: rootdruid:test-while-idle: false  # 关闭空闲检测eureka:client:service-url:  #       替换源码 默认this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/");# 端口号 7001 7002 7003  ,实际工作中是3台不同的电脑域名或IPdefaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7002.com:7003/eureka/instance:instance-id: springcloud-provider-dept8001  # 修改eureka 上	Status默认描述信息# 导入 actuator
info:app.name: jiangyl-springcloud8001company.name: jiang.yl.com

3、Controller层

通过DiscoveryClient的API从注册中心获取其他已经注册的服务的信息

@RestController
public class DeptController {@Autowiredprivate DeptService deptService;// 可以获取服务列表 @Autowiredprivate DiscoveryClient client;@PostMapping("/dept/add")public boolean addDept(Dept dept){return deptService.addDept(dept);}@GetMapping("/dept/get/{id}")public Dept get(@PathVariable("id") Long id){return deptService.queryById(id);}@GetMapping("/dept/list")public List<Dept> qeuryAll(){return deptService.queryAll();}// 还需要在启动类中添加 @EnableDiscoveryClient  服务发现  用于团队协助@GetMapping("/dept/discovery")//注册进来的微服务  , 获取一些信息public Object discovery(){// 获取微服务列表的清单List<String> services = client.getServices();System.out.println("discovery->services = " + services);// Eureka 中Application iDList<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");for (ServiceInstance instance : instances) {System.out.println(instance.getHost()+"\t"+instance.getUri()+"\t"+instance.getPort()+"\t"+instance.getServiceId());}return this.client;}
}

4、service层

public interface DeptService {public boolean addDept(Dept dept);public Dept queryById(Long id);public List<Dept> queryAll();}

5、启动类

@EnableDiscoveryClient

@SpringBootApplication
@EnableEurekaClient  // 自动在服务启动后自动注册到Eureka中
@EnableDiscoveryClient // 服务发现
public class SpringBootProviderApplication_8001 {public static void main(String[] args) {SpringApplication.run(SpringBootProviderApplication_8001.class,args);}
}

测试结果展示

前提是先启动eureka集群

测试1

访问eureka服务 http://localhost:7001/可以查看到注册的提供者信息名称为SPRINGCLOUD-PROVIDER-DEPT

在这里插入图片描述

eureka默认界面

测试2

访问服务提供者接口 http://localhost:8001/dept/list

在这里插入图片描述

提供者list

测试3

访问http://localhost:8001/dept/discovery

在这里插入图片描述控制台输出

discovery->services = [springcloud-provider-dept]
[本机用户名]	http://[本机用户名]:8001	8001	SPRINGCLOUD-PROVIDER-DEPT
discovery->services = [springcloud-provider-dept]
[本机用户名]	http://[本机用户名]:8001	8001	SPRINGCLOUD-PROVIDER-DEPT

搭建eureka服务==>传送
搭建springcloud的提供者–>传送

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/650443.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

AvalonDock实现可停靠式布局

链接&#xff1a; 源码地址 优势&#xff1a; 1. 可创建类似VisualStudio风格的可吸附式UI界面&#xff1b; 2. 界面可切换不同的颜色风格。

《2023网信自主创新调研报告》正式发布,云起无垠连年参编

近日&#xff0c;备受瞩目的《2023网信自主创新调研报告》&#xff08;以下简称《报告》&#xff09;正式对外发布。在这一重要报告的编写过程中&#xff0c;云起无垠公司作为参编单位&#xff0c;发挥了重要作用。公司凭借在软件测试工具、漏洞管理和软件供应链安全等领域的深…

智谱AI大模型-系列_1

关键概念GLM GLM 全名 General Language Model &#xff0c;是一款基于自回归填空的预训练语言模型。ChatGLM 系列模型&#xff0c;支持相对复杂的自然语言指令&#xff0c;并且能够解决困难的推理类问题。该模型配备了易于使用的 API 接口&#xff0c;允许开发者轻松将其融入…

PHP项目搭建与启动

1、拉取项目 2、安装phpstudy 下载地址&#xff1a; Windows版phpstudy下载 - 小皮面板(phpstudy) (xp.cn) 软件安装&#xff1a; Apache2.4.39、Nginx1.15.11、MySQL8.0.12、 composer2.5.8 添加伪静态 将下面代码写入到伪静态配置文本域框内&#xff1a; location ~* (ru…

Idea:阿里巴巴Java编码插件

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 目录 一、Alibaba Java Coding Guidelines插件介绍 二、使用步骤 总结 提示&#xff1a;以下是本篇文章正文内容&#xff0c;下面案例可供参考 一、Alibaba Java Coding …

走向大规模应用之前,DePIN 如何突破技术、数据与市场之网

近期&#xff0c;随着分布式物理基础设施网络&#xff08;DePIN&#xff09;的快速演变&#xff0c;一个旨在利用区块链技术彻底改造传统基础设施模型的新兴生态系统正在逐渐浮现。2024 年 4 月&#xff0c;以 peaq 为代表的 DePIN 项目成功筹集了 1500 万美元用于生态系统的扩…

配置有效的防爬虫技术保护网站

本文主要介绍了防爬虫的概念、目的以及一些有效的防爬虫手段。防爬虫是指网站采取各种技术手段阻止爬虫程序对其数据进行抓取的过程。为了保护网站的数据和内容的安全性&#xff0c;防止经济损失和恶意竞争&#xff0c;以及减轻服务器负载&#xff0c;网站需要采取防爬虫机制。…

Python二进制文件转换为文本文件

&#x1f47d;发现宝藏 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。【点击进入巨牛的人工智能学习网站】。 在日常编程中&#xff0c;我们经常会遇到需要将二进制文件转换为文本文件的情况。这可能是因…

H5点击复制功能 兼容安卓、IOS

效果图 HTML代码 <div>链接&#xff1a;<span style"color: #FF8A21" click"CopyUrl" id"copyId"> https://blog.csdn.net/qq_51463650?spm1000.2115.3001.5343</span> </div>复制方法 const CopyUrl () > {let …

JS实现对用户名、密码进行正则表达式判断,按钮绑定多个事件,网页跳转

目标&#xff1a;使用JS实现对用户名和密码进行正则表达式判断&#xff0c;用户名和密码正确时&#xff0c;进行网页跳转。 用户名、密码的正则表达式检验 HTML代码&#xff1a; <button type"submit" id"login-btn" /*onclick"login();alidate…

2024年钉钉直播回放怎么下载

又到了2024年,最近钉钉迎来了一波更新,经过我的研究,总算研究出来了一个方法,并且做成了工具 首先&#xff0c;让我们了解一下钉钉直播回放的下载方法。 钉钉直播回放工具链接&#xff1a;https://pan.baidu.com/s/1oPWJOp8L2SBDlklt_t5WQQ?pwd1234 提取码&#xff1a;1234 -…

【redis】非关系型数据库——Redis介绍与安装(windows环境)

目录 数据库架构的演化单体架构缓存(Memcached)MySQL集群缓存(Memcached可以)MySQL集群垂直拆分&#xff08;主从复制&#xff0c;读写分离&#xff09;缓存(Redis)MySQL集群垂直拆分分库分表 NoSQLNoSQL产生的背景性能需求MySQL的扩展性瓶颈方面什么是NoSQLNoSQL的特点主流的N…