美文网首页
Spring Cloud 03 -- 服务提供者 Eureka

Spring Cloud 03 -- 服务提供者 Eureka

作者: 半碗鱼汤 | 来源:发表于2019-08-11 17:12 被阅读0次

    一、修改 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>2.0.3.RELEASE</version>
            <relativePath/>
        </parent>
        <groupId>com.dhsg.sc</groupId>
        <artifactId>eureka-client</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>eureka-client</name>
        <description>eureka-client</description>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
        </properties>
    
        <dependencies>
            <!-- eureka client -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</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>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    二、启动类添加注解

    通过注解@EnableEurekaClient 表明自己是一个eurekaclient

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    /**
     * 添加服务提供者所需注解 @EnableEurekaClient
     */
    
    @SpringBootApplication
    @EnableEurekaClient
    public class EurekaClientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaClientApplication.class, args);
        }
    
    }
    

    三、修改配置文件 application.yml

    server:
      port: 8762
    
    spring:
      application:
        name: eureka-client
    
    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/
    #为了在服务注册中心里显示实际的 IP 地址,需添加下面的配置
      instance:
        instance-id: ${spring.cloud.client.ip-address}:${server.port}
        prefer-ip-address: true
    
    

    四、编写一个接口提供服务

    新建一个 controller 包,再新建一个 HelloController 类

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
    
        //该注解可以读取配置文件中的值赋予下面的变量
        @Value("${server.port}")
        private String port;
    
        @RequestMapping(value = "/hello", method = RequestMethod.GET)
        public String Hello(@RequestParam(value = "name", defaultValue = "dhsg") String name) {
            return "Hello " + name + " ,I am from port:" + port;
        }
    }
    

    五、启动项目

    访问服务注册中心,发现多了一个服务

    服务注册中心

    访问 http://localhost:8762/hello

    Eureka Client

    相关文章

      网友评论

          本文标题:Spring Cloud 03 -- 服务提供者 Eureka

          本文链接:https://www.haomeiwen.com/subject/mijsjctx.html