SpringCloud实现电影微服务,也注册到 EurekaServer 中,通过 Http 协议访问已注册到生态圈中的用户微服务

news/2025/1/21 9:42:31/

SpringCloud(第 005 篇)电影微服务,也注册到 EurekaServer 中,通过 Http 协议访问已注册到生态圈中的用户微服务

一、大致介绍

1、在 eureka 服务治理框架中,微服务与微服务之间通过 Http 协议进行通信;
2、用户微服务作为消费方、电影微服务作为提供方都注册到 EurekaServer 中;
3、在电影微服务Web层通过 application.yml 的硬编码配置方式实现服务之间的通信;

二、实现步骤

2.1 添加 maven 引用包

<?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"><modelVersion>4.0.0</modelVersion><artifactId>springms-consumer-movie</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>com.springms.cloud</groupId><artifactId>springms-spring-cloud</artifactId><version>1.0-SNAPSHOT</version></parent><dependencies><!-- web模块 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 监控和管理生产环境的模块 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- 客户端发现模块 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency></dependencies></project>

2.2 添加应用配置文件(springms-consumer-movie\src\main\resources\application.yml)

spring:application:name: springms-consumer-movie
server:port: 7901
user: userServicePath: http://localhost:7900/simple/
eureka:client:
#    healthcheck:
#      enabled: trueserviceUrl:defaultZone: http://admin:admin@localhost:8761/eurekainstance:prefer-ip-address: trueinstance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}

2.3 添加实体用户类User(springms-consumer-movie\src\main\java\com\springms\cloud\entity\User.java)

package com.springms.cloud.entity;import java.math.BigDecimal;public class User {private Long id;private String username;private String name;private Short age;private BigDecimal balance;public Long getId() {return this.id;}public void setId(Long id) {this.id = id;}public String getUsername() {return this.username;}public void setUsername(String username) {this.username = username;}public String getName() {return this.name;}public void setName(String name) {this.name = name;}public Short getAge() {return this.age;}public void setAge(Short age) {this.age = age;}public BigDecimal getBalance() {return this.balance;}public void setBalance(BigDecimal balance) {this.balance = balance;}}

2.4 添加电影Web访问层Controller(springms-consumer-movie\src\main\java\com\springms\cloud\controller\MovieController.java)

package com.springms.cloud.controller;import com.springms.cloud.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;@RestController
public class MovieController {@Autowiredprivate RestTemplate restTemplate;@Value("${user.userServicePath}")private String userServicePath;@GetMapping("/movie/{id}")public User findById(@PathVariable Long id) {return this.restTemplate.getForObject(this.userServicePath + id, User.class);}
}

2.5 添加电影微服务启动类(springms-consumer-movie\src\main\java\com\springms\cloud\MsConsumerMovieApplication.java)

package com.springms.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;/*** 电影微服务,也注册到 EurekaServer 中,通过 Http 协议访问已注册到生态圈中的用户微服务。** @author hmilyylimh** @version 0.0.1** @date 2017/9/17**/
@SpringBootApplication
@EnableEurekaClient
public class MsConsumerMovieApplication {@Beanpublic RestTemplate restTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(MsConsumerMovieApplication.class, args);System.out.println("【【【【【【 电影微服务 】】】】】】已启动.");}
}

三、测试

/****************************************************************************************一、电影微服务,也注册到 EurekaServer 中,通过 Http 协议访问已注册到生态圈中的用户微服务:1、启动 springms-discovery-eureka 模块服务,启动1个端口;2、启动 springms-provider-user 模块服务,启动1个端口;3、启动 springms-consumer-movie 模块服务,启动1个端口;4、在浏览器输入地址 http://localhost:7900/simple/1 可以看到信息成功的被打印出来,说明用户微服务正常;5、在浏览器输入地址 http://localhost:8761 并输入用户名密码 admin/admin 进入Eureka微服务显示在网页中,验证用户微服务、电影微服务确实注册到了 eureka 服务中;6、在浏览器输入地址http://localhost:7901/movie/1 可以看到用户信息成功的被打印出来;****************************************************************************************/


http://www.ppmy.cn/news/57275.html

相关文章

hitcontraining_uaf

1&#xff0c;三连 基本信息&#xff1a;x86-32-el,堆题思路&#xff1b; 保护&#xff1a;Partial RELRO。 堆题多看一个Libc&#xff1a; 2,IDA分析 main功能&#xff1a; add_note()功能&#xff1a; malloc了两次&#xff1a; 8字节填充&#xff08;利用点之一&#xf…

学习笔记:分支结构

✨博文作者&#xff1a;烟雨孤舟 &#x1f496; 喜欢的可以 点赞 收藏 关注哦~~ ✍️ 作者简介: 一个热爱大数据的学习者 文章目录 目录 文章目录 简介 if循环 if...else 语句 if...else if...else 语句 if语句嵌套 switch 语句 嵌套 switch 语句 简介 顺序结构&am…

快速搭建Electron+Vite3+Vue3+TypeScript5脚手架 (无需梯子,快速安装Electron)

一、介绍 &#x1f606; &#x1f601; &#x1f609; Electron是一个使用 JavaScript、HTML 和 CSS 构建桌面应用程序的框架。 嵌入 Chromium 和 Node.js 到 二进制的 Electron 允许您保持一个 JavaScript 代码代码库并创建 在Windows上运行的跨平台应用 macOS和Linux——不需…

【网络安全】红队基础免杀

引言 本文主要介绍“反射型 dll 注入”及“柔性加载”技术。 反射型 dll 注入 为什么需要反射型 dll 注入 常规的 dll 注入代码如下&#xff1a; int main(int argc, char *argv[]) {HANDLE processHandle;PVOID remoteBuffer;wchar_t dllPath[] TEXT("C:\\experime…

通信原理简答题

目录 1&#xff0e; 通信系统由哪些部分组成&#xff1f;各组成部分的作用是什么&#xff1f; 2&#xff0e; 当谐振功率放大器的输入激励信号为余弦波时&#xff0c;为什么集电极电流为余弦脉冲波形&#xff1f;但放大器为什么又能输出不失真的余弦波电压&#xff1f; 3&am…

如何完全卸载linux下通过rpm安装的mysql

卸载linux下通过rpm安装的mysql 1.关闭MySQL服务2.使用 rpm 命令的方式查看已安装的mysql3. 使用rpm -ev 命令移除安装4. 查询是否还存在遗漏文件5. 删除MySQL数据库内容 1.关闭MySQL服务 如果之前安装过并已经启动&#xff0c;则需要卸载前请先关闭MySQL服务 systemctl stop…

程序员为什么越来越内卷了?聊聊java架构师需要掌握的技巧

几年前&#xff0c;一个其他专业的本科生或者专科生&#xff0c;随便培训几个月&#xff0c;便可以轻易拿到1万以上的月薪。培训机构可以拿出很多强有力的证据来证明这个行业的前景非常可观&#xff0c;这个趋势吸引了越来越多的人争相涌入到这个行业。正是因为人员越来越趋于饱…

无人机集群路径规划:淘金优化算法(Gold rush optimizer,GRO)提供MATLAB代码

一、淘金优化算法GRO 淘金优化算法&#xff08;Gold rush optimizer&#xff0c;GRO&#xff09;由Kamran Zolf于2023年提出&#xff0c;其灵感来自淘金热&#xff0c;模拟淘金者进行黄金勘探行为。 参考文献&#xff1a; K. Zolfi. Gold rush optimizer: A new population-ba…