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

本文共 5784 字,大约阅读时间需要 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: 80
eureka:
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;
@Component
public 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;
@RestController
public 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;
@SpringBootApplication
public 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: 7001
eureka:
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;
@SpringBootApplication
public 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相关注解。代码示例如下:

@SpringBootApplication
public 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/

你可能感兴趣的文章
nrm —— 快速切换 NPM 源 (附带测速功能)
查看>>
nrm报错 [ERR_INVALID_ARG_TYPE]
查看>>
NS3 IP首部校验和
查看>>
NSDateFormatter的替代方法
查看>>
NSError 的使用方法
查看>>
nsis 安装脚本示例(转)
查看>>
NSJSON的用法(oc系统自带的解析方法)
查看>>
nslookup 的基本知识与命令详解
查看>>
NSOperation基本操作
查看>>
NSRange 范围
查看>>
NSSet集合 无序的 不能重复的
查看>>
NSURLSession下载和断点续传
查看>>
NSUserdefault读书笔记
查看>>
NS图绘制工具推荐
查看>>
NT AUTHORITY\NETWORK SERVICE 权限问题
查看>>
NT symbols are incorrect, please fix symbols
查看>>
ntelliJ IDEA 报错:找不到包或者找不到符号
查看>>
NTFS文件权限管理实战
查看>>
ntko web firefox跨浏览器插件_深度比较:2019年6个最好的跨浏览器测试工具
查看>>
ntko文件存取错误_苹果推送 macOS 10.15.4:iCloud 云盘文件夹共享终于来了
查看>>