二、Eureka注册中心

Eureka注册中心服务端

引入依赖

复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

编写启动类@EnableEurekaServer

复制代码
package com.gwf;

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

/**
 * @Author gwf
 * @Data 2024/2/17 下午5:24
 **/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServiceApplication.class, args);
    }
}

编写配置文件

复制代码
# 访问端口
server:
  port: 10086

#项目名
spring:
  application:
    name: eureka-server

# eureka-service配置
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka/

Eureka服务注册

引入依赖

复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>4.0.2</version>
</dependency>

配置

userservice
复制代码
server:
  port: 9000

spring:
  application:
    name: userservice

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
orderservice
复制代码
server:
    port: 9000

spring:
    application:
        name: orderservice
eureka:
    client:
        service-url:
            defaultZone: http://127.0.0.1:10086/eureka

多次启动userService

复制服务修改端口启动

远程调用

给RestTemplate这个Bean添加一个@LoadBalanced注解

复制代码
package com.gwf.orderservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class OrderServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderServiceApplication.class, args);
    }
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

修改调用

复制代码
package com.gwf.orderservice;

import com.gwf.orderservice.entity.Order;
import com.gwf.orderservice.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Author gwf
 * @Data 2024/2/17 下午3:39
 **/
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/{orderId}")
    public Order queryOrderByUserId(@PathVariable("orderId")Long orderId){
        Order order = new Order();
        order.setId(orderId);
        order.setUserId(1L);
        order.setName("手机");
        order.setNum(2);
        order.setPrice(1999L);

        String url ="http://userservice:8081/user/"+order.getUserId();
        User user = restTemplate.getForObject(url, User.class);
        order.setUser(user);
        return order;
    }
}
相关推荐
陈桴浮海11 小时前
Kustomize实战:从0到1实现K8s多环境配置管理与资源部署
云原生·容器·kubernetes
ShiLiu_mtx14 小时前
k8s - 7
云原生·容器·kubernetes
7哥♡ۣۖᝰꫛꫀꪝۣℋ15 小时前
Spring-cloud\Eureka
java·spring·微服务·eureka
MonkeyKing_sunyuhua17 小时前
docker compose up -d --build 完全使用新代码打包的方法
docker·容器·eureka
匀泪21 小时前
云原生(LVS NAT模式集群实验)
服务器·云原生·lvs
DolitD1 天前
云流技术深度剖析:国内云渲染主流技术与开源和海外厂商技术实测对比
功能测试·云原生·开源·云计算·实时云渲染
ghostwritten1 天前
春节前夕,运维的「年关」:用 Kubeowler 给集群做一次「年终体检」
运维·云原生·kubernetes
liux35282 天前
基于kubeadm部署Kubernetes 1.26.4 集群指南
云原生·容器·kubernetes
Zfox_2 天前
CANN Catlass 算子模板库深度解析:高性能 GEMM 融合计算、Cube Unit Tiling 机制与编程范式实践
docker·云原生·容器·eureka
农民工老王2 天前
K8s 1.31 私有化部署实战:从 Calico 崩溃到 NFS 挂载失败的排坑全记录
云原生·kubernetes