关于java:Eureka-简单示例

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的简略示例就到这边,前面咱们看看他的源码是怎么实现这些性能的。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理