Eureka服务注册与发现

引入Eureka注册中心

新增注册中心模块
导入依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

编写配置文件

server:
  port: 7001

# Eureka配置
eureka:
  instance:
    hostname: eureka7001.com
  client:
    register-with-eureka: false #表示是否向Eureka注册中心注册自己
    fetch-registry: false #如果为false则表示自己为注册中心
    service-url: #注册地址
      # 单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      # 集群(关联) defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

开启相关功能,添加@EnableEurekaServer注解,将服务置为Eureka服务端

// 启动之后,访问 http://localhost:7001/
@SpringBootApplication
@EnableEurekaServer //服务端的启动类,可以接收别人注册进来
public class EurekaServer_7001 {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_7001.class, args);
    }

}

服务提供者改造

导入Eureka依赖

        <!--Eureka-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <!--actuator完善监控信息-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

编写配置文件

# spring配置
spring:
  application:
    name: springcloud-provider-dept #集群服务名要一致

# Eureka的配置,服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: springcloud-provider-dept-8001 #修改Eureka上的默认描述信息!

# info配置
info:
  app.name: xiaoming-SpringCloud
  company.name: blog.song.com

开启相关功能,增加@EnableEurekaClient,将服务注册到Eureka

// 启动类
@SpringBootApplication
@EnableEurekaClient//在服务启动后自动注册到Eureka中!
@EnableDiscoveryClient//服务发现
public class DeptProvider_8001 {

    public static void main(String[] args) {
        SpringApplication.run(DeptProvider_8001.class, args);
    }

}

可以通过DiscoveryClient,获取一些配置的信息,得到具体的微服务列表。

// 提供Restful服务
@RestController
public class DeptController {

    // 获取一些配置的信息,得到具体的微服务!
    @Autowired
    private DiscoveryClient client;

    // 注册进来的微服务,获取一些消息
    @GetMapping("/dept/discovery")
    public Object discovery() {
        // 获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery=>services:" + services);

        // 得到一个具体的微服务信息,通过具体的微服务id,applicationName
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");

        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost() + "\t" +
                    instance.getPort() + "\t" +
                    instance.getUri() + "\t" +
                    instance.getServiceId() + "\t"
            );
        }
        return this.client;
    }

}

服务消费方改造

消费方无明显变动,集成Eureka后可通过服务名访问服务,无需再用ip+port的形式

//    private static final String REST_URL_PREFIX = "http://127.0.0.1:8001";
    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT";
posted @   不写代码想写诗的虫子  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
点击右上角即可分享
微信分享提示