Eureka软件基础教学视频如何获取?

2026-05-23 15:362阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1331个文字,预计阅读时间需要6分钟。

Eureka软件基础教学视频如何获取?

目录+Eureka+基本教程+RestTemplate+使用Eureka+使用注册中心+提供者+消费者+Eureka+基本教程+RestTemplate+使用+学习Euraka的同学习者+直接滑到页面底部,这里先为初学者介绍RestTemplate。平时我们使用Ht...

目录
  • Eureka 基本教程
    • RestTemplate 使用
    • Eureka 使用
      • 注册中心
      • 提供者
      • 消费者

Eureka 基本教程 RestTemplate 使用

学习Euraka的同学直接滑到最下面, 这里先为初学者介绍 RestTemplate.

平时我们使用 Http 工具发送请求通常都会有两个步骤, 先调用请求拿到响应信息, 再将响应信息通过 JSON 工具解析并转换为实体类. 总的来说, RestTemplate 是 spring 提供的一个用来模拟浏览器发送请求和接收响应的一个类, 它能基于 Http 协议实现远程调用. 但是需要注意的是, 它发送的是同步请求.

父工程依赖:

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.4</version> </parent>

右键父工程创建新 maven 项目, 取名为 eurekademo.

项目引入依赖:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency>

EurekaDemoApplicationTest.java:

@SpringBootTest public class EurekaDemoApplicationTest { @Test public void testGet() { RestTemplate restTemplate = new RestTemplate(); //模拟浏览器的请求头, 很多国内免费的 api 都有这种限制, 一般我们会把以下配置请求头的代码写在一个拦截器中, 通过 restTemplate.setClientHttpRequestInitializers() 配置进去 HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"); HttpEntity<String> entity = new HttpEntity<>("parameters", headers); //节假日查询免费 api ResponseEntity<Holiday> forObject = restTemplate.exchange("timor.tech/api/holiday/info/2022-01-01", HttpMethod.GET, entity, Holiday.class); System.out.println(forObject.getBody()); /* //一般企业自己封装的接口不会对外开放, 所以不需要向上面那么麻烦, 下面的代码只是打个比方, 实际执行会报错 == start Holiday h1 = restTemplate.getForObject("timor.tech/api/holiday/info/2022-01-01", Holiday.class); Holiday h2 = restTemplate.postForObject("timor.tech/api/holiday/info/2022-01-01", null, Holiday.class); // == end */ } } Eureka 使用 注册中心

作用: 管理项目集群而暴露的接口服务, 提供服务注册与发现的功能.

  • 服务注册: 提供者 (暴露自己的服务给外部调用的角色) 向 Eureka 服务器注册自己.

  • 服务发现: 消费者 (去调用暴露出来的服务的角色) 从 Eureka 服务器获取提供者的地址列表.

其中 Eureka 服务器也可以集群, 和其他 Eureka 服务器相互共享自己的资源.

接下来就开始搭建一个 Eureka 服务器.

父工程 pom 添加配置:

<properties> <spring.cloud.version>2021.0.1</spring.cloud.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring.cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

eurekademo 项目中添加依赖:

Eureka软件基础教学视频如何获取?

<!-- mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>

application.yml:

server: port: 8080 spring: application: name: eureka-server # eureka服务器配置, 每个属性赋什么类型的值都可以按住 ctrl 点击属性名进去找到 set 操作 eureka: client: service-url: defaultZone: localhost:8080/eureka #默认服务注册中心地址, 多个使用 ',' 隔开 register-with-eureka: false #是否注册, 默认值 true, 由于这个项目作为 eureka 的注册中心使用, 注册自己会报错, 配置其他注册中心地址不会报错 fetch-registry: false #是否检索服务, 默认值 true, 基本上这个项目不会去处理业务功能, 所以也不需要检索其他服务, 集群注册中心的话就赋 true server: eviction-interval-timer-in-ms: 10000 #注册中心清理无效节点的时间间隔, 默认为 60000L , 单位毫秒 ms

创建启动类 EurekaDemoApplication , 注意组织名 groupId + .eurekademo 为启动类的父包, 我的 groupId 为 com.kent , 所以父包为 com.kent.eurekademo :

/** * @Title: EurekaDemoApplication * @Description: eureka 服务器注册中心 * @author: kent * @date: 2022/3/16 14:53 * @Version: 1.0 */ @SpringBootApplication @EnableEurekaServer //boot 的特点就是需要 Enable public class EurekaDemoApplication { public static void main(String[] args) { SpringApplication.run(EurekaDemoApplication.class, args); } }

ok , 直接 run 起来! 启动成功后访问 127.0.0.1:8080/ 就可以看到 eureka 展示的一些基本信息. 英文看不懂的话, 最好配备一个网易有道词典.

本文共计1331个文字,预计阅读时间需要6分钟。

Eureka软件基础教学视频如何获取?

目录+Eureka+基本教程+RestTemplate+使用Eureka+使用注册中心+提供者+消费者+Eureka+基本教程+RestTemplate+使用+学习Euraka的同学习者+直接滑到页面底部,这里先为初学者介绍RestTemplate。平时我们使用Ht...

目录
  • Eureka 基本教程
    • RestTemplate 使用
    • Eureka 使用
      • 注册中心
      • 提供者
      • 消费者

Eureka 基本教程 RestTemplate 使用

学习Euraka的同学直接滑到最下面, 这里先为初学者介绍 RestTemplate.

平时我们使用 Http 工具发送请求通常都会有两个步骤, 先调用请求拿到响应信息, 再将响应信息通过 JSON 工具解析并转换为实体类. 总的来说, RestTemplate 是 spring 提供的一个用来模拟浏览器发送请求和接收响应的一个类, 它能基于 Http 协议实现远程调用. 但是需要注意的是, 它发送的是同步请求.

父工程依赖:

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.4</version> </parent>

右键父工程创建新 maven 项目, 取名为 eurekademo.

项目引入依赖:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency>

EurekaDemoApplicationTest.java:

@SpringBootTest public class EurekaDemoApplicationTest { @Test public void testGet() { RestTemplate restTemplate = new RestTemplate(); //模拟浏览器的请求头, 很多国内免费的 api 都有这种限制, 一般我们会把以下配置请求头的代码写在一个拦截器中, 通过 restTemplate.setClientHttpRequestInitializers() 配置进去 HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"); HttpEntity<String> entity = new HttpEntity<>("parameters", headers); //节假日查询免费 api ResponseEntity<Holiday> forObject = restTemplate.exchange("timor.tech/api/holiday/info/2022-01-01", HttpMethod.GET, entity, Holiday.class); System.out.println(forObject.getBody()); /* //一般企业自己封装的接口不会对外开放, 所以不需要向上面那么麻烦, 下面的代码只是打个比方, 实际执行会报错 == start Holiday h1 = restTemplate.getForObject("timor.tech/api/holiday/info/2022-01-01", Holiday.class); Holiday h2 = restTemplate.postForObject("timor.tech/api/holiday/info/2022-01-01", null, Holiday.class); // == end */ } } Eureka 使用 注册中心

作用: 管理项目集群而暴露的接口服务, 提供服务注册与发现的功能.

  • 服务注册: 提供者 (暴露自己的服务给外部调用的角色) 向 Eureka 服务器注册自己.

  • 服务发现: 消费者 (去调用暴露出来的服务的角色) 从 Eureka 服务器获取提供者的地址列表.

其中 Eureka 服务器也可以集群, 和其他 Eureka 服务器相互共享自己的资源.

接下来就开始搭建一个 Eureka 服务器.

父工程 pom 添加配置:

<properties> <spring.cloud.version>2021.0.1</spring.cloud.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring.cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

eurekademo 项目中添加依赖:

Eureka软件基础教学视频如何获取?

<!-- mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>

application.yml:

server: port: 8080 spring: application: name: eureka-server # eureka服务器配置, 每个属性赋什么类型的值都可以按住 ctrl 点击属性名进去找到 set 操作 eureka: client: service-url: defaultZone: localhost:8080/eureka #默认服务注册中心地址, 多个使用 ',' 隔开 register-with-eureka: false #是否注册, 默认值 true, 由于这个项目作为 eureka 的注册中心使用, 注册自己会报错, 配置其他注册中心地址不会报错 fetch-registry: false #是否检索服务, 默认值 true, 基本上这个项目不会去处理业务功能, 所以也不需要检索其他服务, 集群注册中心的话就赋 true server: eviction-interval-timer-in-ms: 10000 #注册中心清理无效节点的时间间隔, 默认为 60000L , 单位毫秒 ms

创建启动类 EurekaDemoApplication , 注意组织名 groupId + .eurekademo 为启动类的父包, 我的 groupId 为 com.kent , 所以父包为 com.kent.eurekademo :

/** * @Title: EurekaDemoApplication * @Description: eureka 服务器注册中心 * @author: kent * @date: 2022/3/16 14:53 * @Version: 1.0 */ @SpringBootApplication @EnableEurekaServer //boot 的特点就是需要 Enable public class EurekaDemoApplication { public static void main(String[] args) { SpringApplication.run(EurekaDemoApplication.class, args); } }

ok , 直接 run 起来! 启动成功后访问 127.0.0.1:8080/ 就可以看到 eureka 展示的一些基本信息. 英文看不懂的话, 最好配备一个网易有道词典.