前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Eureka快速体验

Eureka快速体验

作者头像
十毛
发布2019-04-17 17:48:47
6060
发布2019-04-17 17:48:47
举报

在Spring Cloud中一般都使用Eureka做服务治理,本文先快速体验一下基于Eureka完成一次远程调用

Eureka Server搭建


类似于Spring Cloud Config ServerEuraka Server的搭建也是非常简单

添加依赖

代码语言:javascript
复制
<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>

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

<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>

Spring Boot Application

使用注解@EnableEurekaServer

代码语言:javascript
复制
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

配置application.properties

代码语言:javascript
复制
eureka.client.registerWithEureka = false
eureka.client.fetchRegistry = false
server.port = 8761

浏览器查看

http://localhost:8761

Eureka Server部署成功

服务提供方接入


添加依赖pom.xml

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>

Spring Boot Application

  • 使用注解@EnableEurekaClient
代码语言:javascript
复制
@SpringBootApplication
@EnableEurekaClient
public class EurekaProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaProviderApplication.class, args);
    }
}

服务接口HomeController.java

代码语言:javascript
复制
@RestController
@RequestMapping
public class HomeController {
    @GetMapping("ping")
    public String ping() {
        return "success";
    }
}

配置文件application.properties

代码语言:javascript
复制
spring.application.name=tenmao-eureka-provider

# eureka配置
eureka.client.enabled=true
# 优先使用IP地址(防止hosts配置导致的不可访问问题)
eureka.instance.prefer-ip-address=true
# 心跳时间,即服务续约间隔时间(缺省为30s)
eureka.instance.lease-renewal-interval-in-seconds=5
# 发呆时间,即服务续约到期时间     (缺省为90s)
eureka.instance.lease-expiration-duration-in-seconds=15
eureka.client.registry-fetch-interval-seconds=10
# 拉取服务注册信息间隔(缺省为30s)
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

启动

启动后在Eureka Server上可以看到TENMAO-EUREKA-PROVIDER

image.png

服务使用方


添加依赖pom.xml

跟服务提供方的依赖和配置几乎一致

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>

Spring Boot Application

  • 使用注解@EnableEurekaClient
  • 使用EurekaClient
代码语言:javascript
复制
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
    @Resource
    private EurekaClient eurekaClient;
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {
            InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("tenmao-eureka-provider", false);
            System.out.println(instanceInfo);
            RestTemplate restTemplate = new RestTemplate();
            String result = restTemplate.getForObject(instanceInfo.getHomePageUrl() + "ping", String.class);
            System.out.println(result);
        };
    }
}

配置文件application.properties

代码语言:javascript
复制
spring.application.name=tenmao-eureka-client

server.port=8081
# eureka配置
eureka.client.enabled=true
# 优先使用IP地址(防止hosts配置导致的不可访问问题)
eureka.instance.prefer-ip-address=true
# 心跳时间,即服务续约间隔时间(缺省为30s)
eureka.instance.lease-renewal-interval-in-seconds=5
# 发呆时间,即服务续约到期时间     (缺省为90s)
eureka.instance.lease-expiration-duration-in-seconds=15
eureka.client.registry-fetch-interval-seconds=10
# 拉取服务注册信息间隔(缺省为30s)
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

启动

启动后就能从标准输出中看到远程调用结果了

参考

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.04.04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Eureka Server搭建
    • 添加依赖
      • Spring Boot Application
        • 配置application.properties
          • 浏览器查看
          • 服务提供方接入
            • 添加依赖pom.xml
              • Spring Boot Application
                • 服务接口HomeController.java
                  • 配置文件application.properties
                    • 启动
                      • 服务使用方
                        • 添加依赖pom.xml
                          • Spring Boot Application
                            • 配置文件application.properties
                              • 启动
                              • 参考
                              领券
                              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档