关于java:Eureka-简单示例

4次阅读

共计 3739 个字符,预计需要花费 10 分钟才能阅读完成。

Eureka2.0 曾经闭源了,但也是注册核心的热门组件,咱们理解他的应用以及原理。

服务端

首先是 parent 的 pom 配置,这里应用的是 Hoxton.SR9 版本。

<properties>
    <spring.cloud-version>Hoxton.SR9</spring.cloud-version>
</properties>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</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>

而后是服务端的 pom 配置

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>

服务端代码,除了 @SpringBootApplication 注解,还须要 @EnableEurekaServer 注解。

@SpringBootApplication
@EnableEurekaServer
public class EurekaServer8888Application {public static void main(String[] args){SpringApplication.run(EurekaServer8888Application.class, args);
    }
}

yml 文件配置:

server:
  port: 8888
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka/
    # 单个注册核心服务的时候不注册本人
 register-with-eureka: false
 # 单个注册核心服务的时候不拉取数据
 fetch-registry: false
spring:
  application:
    name: eureka-server

下面配置完后,运行 EurekaServer8888Application 的 main 办法,拜访 http://127.0.0.1:8888,呈现以下界面,Eureka 服务端配置实现。

客户端

pom 的配置:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

提供方

yml 配置:

server:
  port: 7000
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka/
spring:
  application:
    name: eureka-provider

提供方 Application 代码:

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

Controller 代码:

@RestController
public class ProviderController {@RequestMapping("/getInfo")
    public String getInfo(String name) {return "provider-" + name;}
}

启动 ProviderApplication 后,查看服务端的地址,能够看到曾经注册到注册核心了。

此时的状态是这样的:

生产方

yml 配置

server:
  port: 7500
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka/
spring:
  application:
    name: eureka-consumer

生产方 Application 代码,留神这边有一个 @LoadBalanced 注解的 RestTemplate。

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

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {return new RestTemplate();
    }
}

Controller 代码,咱们这边拜访的地址是 eureka-provider,并没有拜访到具体的 ip 和端口。

@RestController
public class ConsumerController {
    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/getInfo")
    public String getInfo(String name) {ResponseEntity<String> forEntity = restTemplate.getForEntity("http://eureka-provider/getInfo?name=" + name, String.class);
        return forEntity.getBody();}
}

启动 ConsumerApplication 后,查看服务端的地址,能够看到曾经注册到注册核心了。

此时的状态是这样的:

通过地址调用生产方,能够看到调用胜利:

因为 Consumer 在调用 Provider 之前,会通过服务发现拿到注册核心的注册表信息,而后通过通过注册表信息去找到 Provider 的 IP 和端口,再进行调用。

服务端高可用

在生成过程中,为了高可用,咱们会搭建多个 Euraka Server,这里演示两个,再加一个 9999 端口的 Server。
首先是 8888 的 yml 批改,正文掉 register-with-eureka 和 fetch-registry,这样应用默认值 true,另外 defaultZone 的地址为另外一个 Server 的地址。

server:
  port: 8888
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9999/eureka/
    # 单个注册核心服务的时候不注册本人
    #register-with-eureka: false
    # 单个注册核心服务的时候不拉取数据
    #fetch-registry: false
spring:
  application:
    name: eureka-server

9999 的配置如下,跟下面差不多:

server:
  port: 9999
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka/
spring:
  application:
    name: eureka-server

Consumer 和 Provider 的 yml 的 defaultZone 都批改为:

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka/,http://localhost:9999/eureka/

四个 Application 启动后,不论拜访的是 8888 端口还是 9999 端口,都能够看到以下信息:

此时的状态是这样的:

Eureka 的简略示例就到这边,前面咱们看看他的源码是怎么实现这些性能的。

正文完
 0