微服务-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 进行调用

相关推荐
坐看风云变幻9 小时前
《系统架构师成长之路》第 8 期|微服务:单体拆分的时机、粒度与通信
微服务·架构·系统架构
Song_RsHt10 小时前
Redis 与微服务,什么场景下无可替代
java·redis·后端·学习·微服务
张彦峰ZYF18 小时前
拆得开不等于该拆开:微服务与模块化单体的边界判据、规模阈值、团队适配与一致性代价
后端·微服务·边界是否稳定·扩展天花板是否到顶·有无小组能独立值班·独立部署的自由会否被用上·这一刀是否切断不可降级的不变量
喵了几个咪2 天前
GoWind Admin 拆解:一个 Sender 接口接 39 个通知渠道
微服务·golang·go·1024程序员节·admin·gowind
RuoyiOffice2 天前
Flowable 状态通知设计:单体用本地事件,微服务如何回写业务状态?
spring boot·微服务·vue3·flowable·ruoyi office·redis stream·流程状态通知
GlobalInfo2 天前
上新 | 全球及中国起重机摄像系统行业研究报告(2026版):市场现状、行业前景与占有率分析
大数据·分布式·微服务·ai·相机
BioRunYiXue2 天前
使用docker分析高通量测序数据
java·数据库·人工智能·spring cloud·docker·tomcat·html
利威尔xu3 天前
Go IM 源码实战(2):WebSocket 握手 11 步,少 1 个参数连不上
后端·websocket·微服务·面试·golang
samforce3 天前
为什么微服务会退化成“分布式单体”?——软件架构里的嵌套率传导定律
人工智能·分布式·微服务·架构·软件架构·结构认知