惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
D
DataBreaches.Net
C
Check Point Blog
雷峰网
雷峰网
小众软件
小众软件
GbyAI
GbyAI
美团技术团队
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
WordPress大学
WordPress大学
MyScale Blog
MyScale Blog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
Apple Machine Learning Research
Apple Machine Learning Research
Y
Y Combinator Blog
Jina AI
Jina AI
爱范儿
爱范儿
Last Week in AI
Last Week in AI
MongoDB | Blog
MongoDB | Blog
I
InfoQ
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美

任霏博客

我将关闭服务器:AI彻底掐死了奄奄一息的个人博客 - 博客文章 - 任霏的个人博客网站 Vibe Coding 实现本地模型 Token 自由 IntelliJ IDEA + LM Studio + LM Link + Continue - 博客文章 - 任霏的个人博客网站 1Password涨价后,别急着退订1Password,这个操作能帮你省25% - 博客文章 - 任霏的个人博客网站 我,吃饱了撑的注册了个域名,Cloudflare账号没了,不建议将域名放在Cloudflare - 博客文章 - 任霏的个人博客网站 临时邮箱:保护隐私与免骚扰的新方式 - 博客文章 - 任霏的个人博客网站 价值4100万美元SOL被盗SwissBorg在Solana上遭遇安全事件超200万枚ETH排队退出质押 - 博客文章 - 任霏的个人博客网站 注意 Web3 钱包遭遇 NPM 超大规模供应链攻击投毒事件 - 博客文章 - 任霏的个人博客网站 我受到以太坊ERC-20假代币地址投毒攻击记录一下大家谨防上当受骗 - 博客文章 - 任霏的个人博客网站 在2025年使用显卡 NVIDIA RTX 2080 Ti 挖矿收益记录和分析还能不能挖矿 - 博客文章 - 任霏的个人博客网站 分享我是如何成功戒烟的经验(包含失败的经验) - 博客文章 - 任霏的个人博客网站 在 OpenWRT 中配置 PassWall2 插件的教程记录 - 博客文章 - 任霏的个人博客网站 Office Professional Plus 2019 VL 版下载与 KMS 激活 - 博客文章 - 任霏的个人博客网站 最近几天我的 CDN 流量受到来自电信[山东烟台]、[江苏扬州]两地家庭宽带的攻击 - 博客文章 - 任霏的个人博客网站 自建AI服务器使用PVE配置显卡直通虚拟机安装驱动、CUDA和cuDNN运行LLM大模型进行AI炼丹 - 博客文章 - 任霏的个人博客网站 各代英特尔Intel芯片组主板适配兼容的CPU和DDR内存数据统计 - 博客文章 - 任霏的个人博客网站 GitLab Global 国际站将在60天内删除中国大陆、香港、澳门地区的账号 - 博客文章 - 任霏的个人博客网站 Github Copilot Free 开放免费版所有人均可使用 OpenAI GPT-4o、Anthropic Claude 3.5 AI 代码生成服务 - 博客文章 - 任霏的个人博客网站 Cloudflare 更新了订阅协议明确禁止优选IP和搭建梯子的行为 - 博客文章 - 任霏的个人博客网站 Linux(systemd)手动离线安装二进制(binary)MairaDB数据库指定版本 - 博客文章 - 任霏的个人博客网站 流程引擎 Flowable/Activiti 无法启动报错:liquibase - Waiting for changelog lock.... - 博客文章 - 任霏的个人博客网站 Spring Boot 全局异常捕获 ControllerAdvice 无法捕获 过滤器(Filter)和拦截器(Interceptor)中的异常 - 博客文章 - 任霏的个人博客网站 Freenom 收回了全部免费域名(.tk/.cf/.gq/.ga/.ml) - 博客文章 - 任霏的个人博客网站 Alibaba Druid 数据库连接池 takeLast() AQS 死锁导致程序无响应 - 博客文章 - 任霏的个人博客网站 你的网站加入 HSTS preload 预加载列表了吗 - 博客文章 - 任霏的个人博客网站 我的博客网站接入使用 Cloudflare 的架构分享 - 博客文章 - 任霏的个人博客网站 在 Ubuntu 上的 Nginx 高并发配置实践 - 博客文章 - 任霏的个人博客网站 技术分析黑客敲诈勒索站长的新手法百度对此也无能为力 - 博客文章 - 任霏的个人博客网站 百度站长平台快速收录权限和sitemap提交权限被全部收回 - 博客文章 - 任霏的个人博客网站 极狐 GitLab 免费时代结束不升级付费账号将禁止登陆 - 博客文章 - 任霏的个人博客网站 免费.ml域名10年委托合同到期被马里共和国收回域名经营权 - 博客文章 - 任霏的个人博客网站
Spring Cloud 微服务入门教程(三):微服务的注册 - 博客文...
任霏 · 2020-02-21 · via 任霏博客

Spring Cloud 微服务入门教程(一):微服务介绍 Spring Cloud 微服务入门教程(二):服务注册与发现-Eureka Spring Cloud 微服务入门教程(三):微服务的注册 Spring Cloud 微服务入门教程(四):微服务间的调用消费-FeignClient Spring Cloud 微服务入门教程(五):统一配置中心-ConfigService Spring Cloud 微服务入门教程(六):Spring Cloud BUS 消息总线实现配置中心动态更新配置文件 Spring Cloud 微服务入门教程(七):Spring Cloud Stream 消息队驱动式的微服务 Spring Cloud 微服务入门教程(八):Spring Cloud Zuul 服务网关动态路由和Cookie头信息传递和跨域 Spring Cloud 微服务入门教程(九):网关 Zuul 整合 Swagger2 实现自动生成 RESTful API 文档 Spring Cloud 微服务入门教程(十):Spring Cloud Hystrix 服务熔断和服务降级 Spring Cloud 微服务入门教程(十一):Spring Cloud Sleuth zipkin 服务追踪链路监控 Spring Cloud 微服务入门教程(十二):Spring Cloud Docker 容器化部署

代码 将在https://github.com/NeilRen/SpringCloudDemo进行分享,按不同的分支分享不同的章节代码,Master分支就是最后所有的内容合集。本章节代码在:https://github.com/NeilRen/SpringCloudDemo/tree/feature/eureka-client

原创 本篇文章是原创文章,作者:任霏,转载请注明作者和出处

上一节我们讲了《Spring Cloud 微服务入门教程(二):服务注册与发现-Eureka》搭建了微服务的注册发现中心,这一节我们就讲一下如何新建一个微服务服务并且将服务注册到注册中心。

统一接口中心模块

在开始之前我先说明一下「统一接口中心」并不是微服务的标准架构,这个是我自己设计的架构,因为我觉得整个架构里会很多服务,服务之间要相互调用,而且团队合作中可能有很多人很多团队来写各自的服务模块,如果不规定好都有什么请求地址,什么样的请求对象和返回对象,就很难高效的协同,所以我会提前写好interface接口,定义好controller请求的地址、Request的结构、Result的结构,这样负责实现的服务的团队只需要实现interface接口就可以了,其他人调用的时候也之前要传什么类型的Request,收到什么样的Result。最后再次声明,统一接口中心是我自己设计的架构,并不包含在微服务体系里,仅供各位参考,可能并不适合你的业务形态,需要你自己设计,我只提供思路教程。

右击根项目名,新建一个模块叫api-center的maven子项目,就像上一节新建eureka模块那样,不再赘述,这里还需要新增一个依赖spring-boot-starter-web,因为这样可以把请求的URL地址也提前定义好,新建完成以后的POM文件是:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <parent>
        <artifactId>cloud</artifactId>
        <groupId>net.renfei</groupId>
        <version>1.0.0</version>
    </parent>
    
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.renfei</groupId>
    <artifactId>apicenter</artifactId>
    <version>1.0.0</version>
    <name>APICenter</name>
    <description>接口中心</description>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
</project>

然后在api-center子项目的src/main/java中新建三个包:net.renfei.apicenter.service、net.renfei.apicenter.request、net.renfei.apicenter.result,在request这个包下新建一个class叫BaseRquest,作为请求体的基类并且实现Serializable序列化接口,再新建一个叫DemoRquest的类继承BaseRquest,里面有个成员字段String msg:

package net.renfei.apicenter.request;

public class DemoRquest extends BaseRquest implements Serializable {
    private static final long serialVersionUID = 1L;
    private String msg;
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}

在net.renfei.apicenter.result包下我们新建一个Result类,这将来是我们统一的返回格式:

package net.renfei.apicenter.result;

import java.io.Serializable;

public class Result implements Serializable {
    private static final long serialVersionUID = 1L;
    private int code;
    private String message;
    private Object object;
    
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public Object getObject() {
        return object;
    }
    public void setObject(Object object) {
        this.object = object;
    }
}

然后我们在net.renfei.apicenter.service包下新建一个interface叫DemoService的接口:

package net.renfei.apicenter.service;

import net.renfei.apicenter.request.DemoRquest;
import net.renfei.apicenter.result.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

public interface DemoService {
    @GetMapping("/")
    Result index();
    
    @PostMapping("/sayMsg")
    Result sayMsg(DemoRquest demoRquest);
}

这样通过interface约定好了服务的地址、服务的名称、请求的类型和返回额类型,这就是DemoService的接口。

新建一个服务生产者

右击项目根目录,新建一个名为 demoservice 的空白maven项目,作为服务的提供者,并修改POM文件,这里依赖spring-cloud-starter-netflix-eureka-client、spring-boot-starter-web和apicenter模块:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <parent>
        <artifactId>cloud</artifactId>
        <groupId>net.renfei</groupId>
        <version>1.0.0</version>
    </parent>
    
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.renfei</groupId>
    <artifactId>demoservice</artifactId>
    <version>1.0.0</version>
    <name>demo-service</name>
    <description>演示服务</description>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>net.renfei</groupId>
            <artifactId>apicenter</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
</project>

然后新建包名:net.renfei.demoservice.controller,在resources中新建application.yml文件:

server:
  port: 18080
spring:
  application:
    name: DemoService
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

application.yml里设置了启动的端口、应用的名称和eureka的注册中心地址,其中http://localhost:8761/eureka/就是我们eureka注册中心的地址,见《[Spring Cloud 微服务入门教程(二):服务注册与发现-Eureka](https://blog.renfei.net/posts/1003320)》的配置过程。

在net.renfei.demoservice.controller包下新增一个DemoController并实现DemoService:

package net.renfei.demoservice.controller;

import net.renfei.apicenter.request.DemoRquest;
import net.renfei.apicenter.result.Result;
import net.renfei.apicenter.service.DemoService;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController implements DemoService {
    @Override
    public Result index() {
        Result result = new Result();
        result.setCode(200);
        result.setMessage("You're visiting DemoService.");
        return result;
    }
    
    @Override
    public Result sayMsg(@RequestBody DemoRquest demoRquest) {
        Result result = new Result();
        result.setCode(200);
        result.setMessage("This is DemoService, Your Mag is: " + demoRquest.getMsg());
        return result;
    }
}

然后在net.renfei.demoservice包下新建一个DemoServiceApplication类作为模块的启动入口类

package net.renfei.demoservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class DemoServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoServiceApplication.class, args);
    }
}

与SpringBoot不同的是,新增了一个@EnableEurekaClient注解,这个就是Eureka的客户端,会向Eureka注册中心注册自己,到这里一个服务就完成了。

运行微服务注册

至此,微服务的注册发现就完成了,先启动eureka,然后启动demoservice,我们来测试一下吧。全部启动以后,并访问我们配置的eureka地址,这个就是注册中心,可以打开就是成功了,我这里的案例是:http://localhost:8761

微服务Eureka注册中心

可以看到“Instances currently registered with Eureka”中已经发现了一个服务,“DEMOSERVICE”。