本文共 5784 字,大约阅读时间需要 19 分钟。
创建microservicecloud-consumer-dept-80
微服务消费者模块,为实现部门服务的消费功能奠定基础。
在模块的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
在application.yml
中设置基础配置,确保微服务能够正常运行并连接服务注册中心。关键配置如下:
server: port: 80eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://localhost:8001/eureka/
开发一个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";}
创建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 Listlist() { return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class); }}
开发启动类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是一款基于RESTful风格的服务注册与发现平台,广泛应用于微服务架构中。其核心功能包括服务注册、服务发现、心跳检查等。
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
修改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/
开发启动类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); }}
在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
在application.yml
中添加Eureka相关配置,确保微服务提供者能够注册至Eureka服务中心。关键内容如下:
eureka: client: register-with-eureka: true fetch-registry: true
在DeptProvider8001_App
类中添加Eureka相关注解。代码示例如下:
@SpringBootApplicationpublic class DeptProvider8001_App { public static void main(String[] args) { SpringApplication.run(DeptProvider8001_App.class, args); }}
运行EurekaServer7001_App
类,启动Eureka服务注册中心。访问http://localhost:7001
查看服务注册情况。
运行DeptProvider8001_App
类,启动部门微服务提供者服务。确保服务注册中心能够正确反映服务状态。
运行DeptConsumer80_App
类,启动部门微服务消费者服务。通过浏览器访问http://localhost:80/consumer/dept/add
进行测试,确保能够正常调用服务端接口。
通过以上步骤,我们成功构建了一个基于Spring Cloud的微服务架构,实现了部门服务的消费功能,并集成了Eureka服务注册与发现机制。未来可以进一步优化服务发现机制,添加健康检查、负载均衡等功能,以提升微服务架构的稳定性和可扩展性。
转载地址:http://fyql.baihongyu.com/