Spring Cloud 微服务入门教程(三):微服务的注册
上一节我们讲了《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》的配置过程。
在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

可以看到“Instances currently registered with Eureka”中已经发现了一个服务,“DEMOSERVICE”。
商业用途请联系作者获得授权。
版权声明:本文为博主「任霏」原创文章,遵循 CC BY-NC-SA 4.0 版权协议,转载请附上原文出处链接及本声明。
相关推荐
猜你还喜欢这些内容,不妨试试阅读一下评论与留言
以下内容均由网友提交发布,版权与真实性无法查证,请自行辨别。微信订阅号
扫码关注「任霏博客」微信订阅号- 你好,我想问一下如果是分析型的数据库要怎么制作docker镜像呢 是修改V008R003C002B0320版本号吗
- 可以的,我也正在开发分享的程序,可以邮件或群联系我都可以,关于页面里有联系方式:https://www.renfei.net/page/about 。
- 有破解软件的需要可以私下联系您吗?
- 您好,手机APP只是个客户端,用于数据呈现展示,数据均保存在服务器上,只留个APP没有任何用处,无能为力哦。
- 老哥 看你弄了这么多软件好厉害啊。 我有个软件 我买过几个小会员 没用几天 然后商家跑路了,软件服务器关闭了,连不上去 用不了。 你能做成一个打补丁版本可以本地用的么? 方便看下么?https://haodezhe.lanzouw.com/iD0f30h9joza 谢谢老哥!
- 您好,由于版权投诉和我国知识产权法的完善,我已经下架所有破解软件的下载链接了。
- 请问怎么下载呀
- 我保存的License在:https://gitlab.com/renfei/KingbaseES-V8-R3/-/tree/master/License ,开发版是长期有效的,只不过限制连接数,现在官网好像已经下线 V8R3 的下载页面了,其他版本我也不确定是否过期
- 这个版本的license有没有
- 序列号长度不对呀
- 优雅的源代码管理(二):Git 的工作原理
- 优雅的源代码管理(一):版本控制系统 VCS(Version Control System)与软件配置管理 SCM(Software Configuration Management)
- ChatGPT 开发商 OpenAI 买下极品域名 AI.com
- 火爆的 AI 人工智能 ChatGPT 国内注册教程、使用方式和收费标准
- 解决 SpringCloud 中 bootstrap.yml 不识别 @[email protected] 参数
- Cron表达式书写教程搞定Linux、Spring、Quartz的定时任务
- 阿里云香港可用区C发生史诗级故障
- 国产统信UOS服务器操作系统V20提供免费使用授权
- 开源站长推送工具效果评测推荐(百度/必应/谷歌)
- 获取公网IP服务「ip.renfei.net」升级增加地理定位数据字段公示