构建基于Spring Cloud和Consul的服务注册与发现系统

构建基于Spring Cloud和Consul的服务注册与发现系统

大家好,我是微赚淘客系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!

一、引言

在微服务架构中,服务注册与发现是关键组件。Spring Cloud与Consul的结合可以帮助我们轻松实现服务注册与发现功能。本文将介绍如何使用Spring Cloud和Consul构建一个高效的服务注册与发现系统。

二、环境准备

在开始之前,需要确保以下环境已准备好:

  1. 安装并运行Consul。
  2. Java 8或更高版本。
  3. Maven或Gradle构建工具。

三、创建Spring Boot应用

  1. 引入依赖

pom.xml中添加Spring Cloud Consul相关依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  1. 配置文件

src/main/resources目录下创建application.yml文件,添加Consul的配置:

spring:
  application:
    name: hello-service
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}
        health-check-interval: 10s
        health-check-path: /actuator/health

server:
  port: 8080
  1. 主应用程序类

cn.juwatech包下创建主应用程序类HelloServiceApplication

package cn.juwatech;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class HelloServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloServiceApplication.class, args);
    }
}
  1. 创建一个简单的控制器

cn.juwatech.controller包下创建HelloController类:

package cn.juwatech.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello from Consul!";
    }
}

四、运行Consul

确保Consul已经在本地运行,可以通过以下命令启动:

consul agent -dev

五、启动Spring Boot应用

运行HelloServiceApplication类,应用启动后,会自动向Consul注册服务。

六、验证服务注册

打开浏览器,访问http://localhost:8500/ui,可以看到服务已经注册到Consul。

七、创建另一个服务以验证服务发现

  1. 新建服务

创建另一个Spring Boot项目,命名为GreetingService

  1. 引入依赖

pom.xml中添加相同的Consul依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  1. 配置文件

src/main/resources目录下创建application.yml文件:

spring:
  application:
    name: greeting-service
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}
        health-check-interval: 10s
        health-check-path: /actuator/health

server:
  port: 8081
  1. 主应用程序类

cn.juwatech包下创建主应用程序类GreetingServiceApplication

package cn.juwatech;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class GreetingServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(GreetingServiceApplication.class, args);
    }
}
  1. 创建控制器

cn.juwatech.controller包下创建GreetingController类:

package cn.juwatech.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class GreetingController {

    private final RestTemplate restTemplate;

    public GreetingController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @GetMapping("/greet")
    public String greet() {
        String helloServiceUrl = "http://hello-service/hello";
        return restTemplate.getForObject(helloServiceUrl, String.class);
    }
}
  1. 配置RestTemplate

cn.juwatech包下创建配置类AppConfig

package cn.juwatech;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class AppConfig {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

八、启动GreetingService

运行GreetingServiceApplication类,服务启动后会向Consul注册,并可以通过http://localhost:8081/greet访问hello-service的接口。

九、验证服务发现

打开浏览器,访问http://localhost:8081/greet,应该可以看到Hello from Consul!的响应,这表明greeting-service成功发现并调用了hello-service

十、总结

通过本文的介绍,我们详细讲解了如何使用Spring Cloud和Consul构建服务注册与发现系统。从配置Spring Boot项目到实现服务注册和发现的具体步骤,希望能帮助你更好地理解和应用这一强大功能。

本文著作权归聚娃科技微赚淘客系统开发者团队,转载请注明出处!

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 JavaScript 编写的记忆游戏(附源代码)   项目:JavaScript 记忆游戏(附源代码) 记忆检查游戏是一个使用 HTML5、CSS 和 JavaScript 开发的简单项目。这个游戏是关于测试你的短期 记忆技能。玩这个游戏 时,一系列图像会出现在一个盒子形状的区域中 。玩家必须找到两个相同的图像并单击它们以使它们消失。 如何运行游戏? 记忆游戏项目仅包含 HTML、CSS 和 JavaScript。谈到此游戏的功能,用户必须单击两个相同的图像才能使它们消失。 点击卡片或按下键盘键,通过 2 乘 2 旋转来重建鸟儿对,并发现隐藏在下面的图像! 如果翻开的牌面相同(一对),您就赢了,并且该对牌将从游戏中消失! 否则,卡片会自动翻面朝下,您需要重新尝试! 该游戏包含大量的 javascript 以确保游戏正常运行。 如何运行该项目? 要运行此游戏,您不需要任何类型的本地服务器,但需要浏览器。我们建议您使用现代浏览器,如 Google Chrome 和 Mozilla Firefox, 以获得更好、更优化的游戏体验。要玩游戏,首先,通过单击 memorygame-index.html 文件在浏览器中打开游戏。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值