CSDN博主:【java_wxid】
CSDN博主:点击【Java廖志伟】
CSDN社区:点击【幕后大佬】
码云:点击【互联网Java工程师知识扫盲】
随笔 - 882,  文章 - 0,  评论 - 1,  阅读 - 51800

服务注册中心 :eureka-server

新建一个springboot项目:eureka-server,其pom.xml配置如下:

  <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
          <java.version>1.8</java.version>
      </properties>
      <dependencies>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-eureka-server</artifactId>
         </dependency>
     </dependencies>
     <dependencyManagement>
         <dependencies>
             <dependency>
                 <groupId>org.springframework.cloud</groupId>
                 <artifactId>spring-cloud-dependencies</artifactId>
                 <version>Dalston.SR1</version>
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
         </dependencies>
     </dependencyManagement>

想要实现一个服务注册中心的功能非常简单,只需要在项目的启动类EurekaServerApplication上使用@EnableEurekaServer注解即可

1 @EnableEurekaServer
2 @SpringBootApplication
3 public class EurekaServerApplication{
4 
5     public static void main(String[] args) {
6         new SpringApplicationBuilder(EurekaServerApplication.class)
7                     .web(true).run(args);
8     }
9 }

默认情况下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,只需要在application.properties配置文件中增加如下信息:

1 spring.application.name=eureka-server
2 server.port=1001
3 eureka.instance.hostname=localhost
4 eureka.client.register-with-eureka=false
5 eureka.client.fetch-registry=false

启动EurekaServerApplication,访问 http://localhost:9001/可以看到Eureka的页面,从红框的位置可以看到没有任务服务实例注册到当前的服务注册中心

服务提供方 :eureka-client

     每一个实例注册之后需要向注册中心发送心跳,当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。

新建一个springboot项目:eureka-client,其pom.xml配置如下:

  <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
          <java.version>1.8</java.version>
      </properties>
      <dependencies>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-eureka</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
     </dependencies>
     <dependencyManagement>
         <dependencies>
             <dependency>
                 <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                 <version>Dalston.SR1</version>
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
         </dependencies>
     </dependencyManagement>

想要实现一个服务提供方也很简单,只要在项目的启动类EurekaClientApplication上使用@EnableEurekaClient注解即可

 1 @EnableEurekaClient
 2 @SpringBootApplication
 3 public class EurekaClientApplication {
 4 
 5      public static void main(String[] args) {
 6             new SpringApplicationBuilder(
 7                     EurekaClientApplication.class)
 8                 .web(true).run(args);
 9         }
10 }

在application.properties中进行如下配置

spring.application.name=eureka-client
server.port=9002
eureka.client.serviceUrl.defaultZone=http://localhost:9001/eureka/

通过spring.application.name属性,我们可以指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问。

eureka.client.serviceUrl.defaultZone属性对应服务注册中心的配置内容,指定服务注册中心的位置。

使用server.port属性设置不同的端口。

 

启动EurekaClientApplication类

刷新 http://localhost:9001/,可以看到咱们的服务提供方已经注册到了服务注册中心

 

在新建一个DiscoveryController

   使用discoveryClient.getServices()获取已经注册的服务名,使用@value将配置文件中的信息赋值到ip

 

@RestController
public class DiscoveryController {
    
    @Autowired
    private DiscoveryClient discoveryClient;
    @Value("${server.port}")
    private String ip;
    
    @GetMapping("/client")
    public String client() {
        String services = "Services: " + discoveryClient.getServices()+" ip :"+ip;
        
        System.out.println(services);
        return services;
    }
}

 

访问:http://localhost:9002/client

 

最后说明一下@EnableEurekaClient 与@EnableDiscoveryClient这两个注解

 首先这个两个注解都可以实现服务发现的功能,在spring cloud中discovery service有许多种实现(eureka、consul、zookeeper等等)

   @EnableEurekaClient基于spring-cloud-netflix。服务采用eureka作为注册中心,使用场景较为单一。

   @EnableDiscoveryClient基于spring-cloud-commons。服务采用其他注册中心。

 

 

posted on   我是廖志伟  阅读(2)  评论(0编辑  收藏  举报  
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

南北踏尘
点击右上角即可分享
微信分享提示