其他分享
首页 > 其他分享> > eureka-client

eureka-client

作者:互联网

上节完成了服务注册中心的搭建之后,接下来我们尝试将一个Springboot应用加入Eureka的服务治理体系中去。

创建一个基础的springboot工程

我的pom文件是

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.maple</groupId>
    <artifactId>eureka-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-client</name>
    <description>Demo project for Eureka Client</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Brixton.SR5</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

接着,写一个HelloController,通过注入DiscoveryClient对象,在日志中打印出服务的相关内容。

package com.maple.eurekaclient.controller;

import org.apache.log4j.Logger;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class HelloController {

    private final Logger logger = Logger.getLogger(getClass());

    @Resource
    private DiscoveryClient client;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String index() {
        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("/hello,host:" + instance.getHost() + ",service_id:" + instance.getServiceId());
        return "Hello World";
    }
}

 然后,在主类中通过加上@EnableDiscoveryClient注解,激活Eureka中的DiscoveryClient实现(自动化配置,创建DiscoveryClient接口针对Eureka客户端的EurekaDiscoveryClient实例),才能实现上述Controller中对服务信息的输出。

package com.maple.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {

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

}

最后,我们需要在application.properties配置文件中,通过spring.application.name属性来为服务命名,比如命名为 hello-service。再通过eureka.client.serviceUrl.defaultZone属性来指定服务注册中心的地址,这里我们指定为上节构建的服务注册中心地址,完整配置如下所示:

spring.application.name=hello-service
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

下面我们分别启动服务注册中心以及这里的hello-service服务。在hello-service服务控制台中,Tomcat启动 之后,com.netflix.discovery.DiscoveryClient对象打印了该服务的注册信息,表示服务注册成功。

s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
c.n.e.EurekaDiscoveryClientConfiguration : Updating port to 8080
c.m.e.EurekaClientApplication            : Started EurekaClientApplication in 5.931 seconds (JVM running for 7.239)
com.netflix.discovery.DiscoveryClient    : DiscoveryClient_HELLO-SERVICE/wanghongbodeair:hello-service - registration status: 204

 

标签:DiscoveryClient,springframework,eureka,client,import,org,cloud
来源: https://blog.csdn.net/qq_33101675/article/details/88867112