微服务-springcloud(eureka实践, nacos实践)

Spring 体系图

版本关系

eureka 实践

1 父工程依赖

bash 复制代码
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.14</version>
</parent>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2 搭建Eureka Server

bash 复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
yml 复制代码
server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: 

3 搭建服务提供者

bash 复制代码
<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>
yml 复制代码
eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/

spring:
  application:
    name: eureka-provider
server:
  port: 9000

4 搭建消费者

bash 复制代码
<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>
yml 复制代码
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: eureka-consumer
server:
  port: 9001

5 服务消费方

5.1 使用RestTemplate 进行RPC调用

java 复制代码
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
    return new RestTemplate();
}

5.2 真实调用

java 复制代码
@GetMapping("/hello/{id}")
public String hello(@PathVariable String id){
    return restTemplate.getForObject("http://EUREKA-PROVIDER/hello/"+id, String.class);
}

注意:

@LoadBalanced这个注解一定要加上(org.springframework.cloud.client.loadbalancer)

集群模式:

https://cloud.tencent.com/developer/article/1730733

Naco 实践

1 首先我们需要安装 Nacos Server

可以选用Docker

2 父POM

xml 复制代码
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <!--这里的版本一定要对应上 和下面的spring-cloud-alibaba-dependencies里的spring boot 版本 -->
  <version>2.4.2</version>
</parent>
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-alibaba-dependencies</artifactId>
      <version>2021.1</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

3 服务提供者

yml 复制代码
server:
  port: 8088
spring:
  application:
    name: ddd-provider
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
xml 复制代码
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

4 服务消费者

yml 复制代码
server:
  port: 8089
  application:
    name: ddd-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
xml 复制代码
<dependency>
       <groupId>com.alibaba.cloud</groupId>
       <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
   </dependency>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-loadbalancer</artifactId>
       <version>3.0.1</version>
   </dependency>

5 调用方式和上面的一样,使用负载均衡的RestTemplate 进行调用

相关推荐
stark张宇5 天前
微服务架构必备:Gin + gRPC + Consul + Nacos + GORM 打造用户服务
微服务·gin·grpc
阿里云云原生8 天前
MSE Nacos Prompt 管理:让 AI Agent 的核心配置真正可治理
微服务·云原生
阿里云云原生9 天前
阿里云微服务引擎 MSE 及 API 网关 2026 年 1 月产品动态
微服务
1candobetter9 天前
Docker Compose Build 与 Up 的区别:什么时候必须重建镜像
docker·容器·eureka
追风筝的人er9 天前
企业管理系统如何实现自定义首页与千人千面?RuoYi Office 给出了完整方案
vue.js·spring boot·spring cloud
麦聪聊数据9 天前
统一 Web SQL 平台如何收编企业内部的“野生数据看板”?
数据库·sql·低代码·微服务·架构
云司科技codebuddy9 天前
技术支持过硬Trae核心代理
大数据·运维·python·微服务
一次旅行9 天前
Docker安全总结
安全·docker·eureka
递归尽头是星辰9 天前
微服务事务分级治理:从 Seata 全模式到 TDSQL 实战
微服务·云原生·架构·分布式事务·事务分级治理
没有bug.的程序员9 天前
订单系统重构史诗:从单体巨兽到微服务矩阵的演进、数据一致性内核与分布式事务
java·微服务·矩阵·重构·分布式事务·数据一致性·订单系统