博客
关于我
SpringCloud入门实战二
阅读量:333 次
发布时间:2019-03-03

本文共 5709 字,大约阅读时间需要 19 分钟。

Spring Cloud微服务构建之部门服务消费者与Eureka服务注册

一:部门服务消费者模块构建

1. 创建微服务消费者模块

创建microservicecloud-consumer-dept-80微服务消费者模块,为实现部门服务的消费功能奠定基础。

2. 修改POM文件

在模块的pom.xml中添加必要的依赖项,确保能够正常构建并引用部门服务提供者的接口。关键修改如下:

com.topcheer
microservicecloud-api
${project.version}
org.springframework.boot
spring-boot-starter-web
org.springframework
springloaded
1.2.5
org.springframework.boot
spring-boot-devtools
3.8.4

3. 修改配置文件

application.yml中设置基础配置,确保微服务能够正常运行并连接服务注册中心。关键配置如下:

server:  port: 80eureka:  client:    register-with-eureka: false    fetch-registry: false  service-url:    defaultZone: http://localhost:8001/eureka/

4. 创建配置类信息

开发一个ConfigBean类,用于集中管理微服务消费者的配置信息。代码示例如下:

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import com.topcheer.springcloud.entities.Dept;@Componentpublic class ConfigBean {    @Autowired    private RestTemplate restTemplate;        // 定义调用服务端接口的基础路径    private static final String REST_URL_PREFIX = "http://localhost:8001";}

5. 开发消费者控制器

创建DeptController_Consumer类,实现部门信息的增、删、改、查功能。代码示例如下:

import com.topcheer.springcloud.controller.DeptController_Consumer;import com.topcheer.springcloud.entities.Dept;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestControllerpublic class DeptController_Consumer {    @Autowired    private RestTemplate restTemplate;        // 定义调用服务端接口的基础路径    private static final String REST_URL_PREFIX = "http://localhost:8001";        // 调用服务端新增部门信息    @RequestMapping(value="/consumer/dept/add")    public boolean add(Dept dept) {        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);    }        // 调用服务端查询部门信息    @RequestMapping(value="/consumer/dept/get/{id}")    public Dept get(@PathVariable("id") Long id) {        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);    }        // 调用服务端查询部门列表    @RequestMapping(value="/consumer/dept/list")    public List
list() { return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class); }}

6. 创建启动类

开发启动类DeptConsumer80_App,启动微服务消费者模块。代码示例如下:

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class DeptConsumer80_App {    public static void main(String[] args) {        SpringApplication.run(DeptConsumer80_App.class, args);    }}

二:Eureka服务注册与发现

1. Eureka概述

Eureka是一款基于RESTful风格的服务注册与发现平台,广泛应用于微服务架构中。其核心功能包括服务注册、服务发现、心跳检查等。

2. Eureka架构与原理

Eureka采用中心化的架构模式,主要包含以下角色:

  • Eureka Server:负责服务的注册与发现
  • Service Provider:将自身服务注册至Eureka Server
  • Service Consumer:从Eureka Server中获取服务列表并进行消费

3. 部分配置与创建

3.1 创建Eureka服务注册中心

创建microservicecloud-eureka-7001模块,配置Eureka Server。关键修改如下:

org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework
springloaded
1.2.5
org.springframework.boot
spring-boot-devtools
3.8.4

3.2 配置Eureka Server

修改application.yml文件,设置Eureka Server的相关配置。关键内容如下:

server:  port: 7001eureka:  instance:    hostname: localhost  client:    register-with-eureka: false    fetch-registry: false  service-url:    defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

3.3 创建Eureka服务启动类

开发启动类EurekaServer7001_App,启动Eureka服务注册中心。代码示例如下:

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class EurekaServer7001_App {    public static void main(String[] args) {        SpringApplication.run(EurekaServer7001_App.class, args);    }}

4. 将部门微服务注册至Eureka

4.1 修改部门微服务提供者的POM文件

microservicecloud-provider-dept-8001模块中添加Eureka相关依赖。关键修改如下:

org.springframework.cloud
spring-cloud-starter-eureka-client
org.springframework
springloaded
1.2.5
org.springframework.boot
spring-boot-devtools
3.8.4

4.2 修改部门微服务提供者的配置文件

application.yml中添加Eureka相关配置,确保微服务提供者能够注册至Eureka服务中心。关键内容如下:

eureka:  client:    register-with-eureka: true    fetch-registry: true

4.3 修改部门微服务提供者的启动类

DeptProvider8001_App类中添加Eureka相关注解。代码示例如下:

@SpringBootApplicationpublic class DeptProvider8001_App {    public static void main(String[] args) {        SpringApplication.run(DeptProvider8001_App.class, args);    }}

5. 启动服务并测试

5.1 启动Eureka服务中心

运行EurekaServer7001_App类,启动Eureka服务注册中心。访问http://localhost:7001查看服务注册情况。

5.2 启动部门微服务提供者

运行DeptProvider8001_App类,启动部门微服务提供者服务。确保服务注册中心能够正确反映服务状态。

5.3 测试部门微服务消费者

运行DeptConsumer80_App类,启动部门微服务消费者服务。通过浏览器访问http://localhost:80/consumer/dept/add进行测试,确保能够正常调用服务端接口。

总结

通过以上步骤,我们成功构建了一个基于Spring Cloud的微服务架构,实现了部门服务的消费功能,并集成了Eureka服务注册与发现机制。未来可以进一步优化服务发现机制,添加健康检查、负载均衡等功能,以提升微服务架构的稳定性和可扩展性。

转载地址:http://fyql.baihongyu.com/

你可能感兴趣的文章
mysql 通过查看mysql 配置参数、状态来优化你的mysql
查看>>
mysql 里对root及普通用户赋权及更改密码的一些命令
查看>>
Mysql 重置自增列的开始序号
查看>>
mysql 锁机制 mvcc_Mysql性能优化-事务、锁和MVCC
查看>>
MySQL 错误
查看>>
mysql 随机数 rand使用
查看>>
MySQL 面试题汇总
查看>>
MySQL 面试,必须掌握的 8 大核心点
查看>>
MySQL 高可用性之keepalived+mysql双主
查看>>
MySQL 高性能优化规范建议
查看>>
mysql 默认事务隔离级别下锁分析
查看>>
Mysql--逻辑架构
查看>>
MySql-2019-4-21-复习
查看>>
mysql-5.6.17-win32免安装版配置
查看>>
mysql-5.7.18安装
查看>>
MySQL-Buffer的应用
查看>>
mysql-cluster 安装篇(1)---简介
查看>>
mysql-connector-java.jar乱码,最新版mysql-connector-java-8.0.15.jar,如何愉快的进行JDBC操作...
查看>>
mysql-connector-java各种版本下载地址
查看>>
mysql-EXPLAIN
查看>>