springCloud(3)---创建服务提供者Eureka Client

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_23490433/article/details/89185244

当服务提供者Eureka-client向服务注册中心Eureka-server注册时,它会提供一些元数据(如主机和端口、url、主页等)

eurekaServer注册中心从每个服务提供者实例接收心跳信息,如果心跳超时,则通常将该实例从注册server中删除

创建服务提供者步骤如下:

第一步:在maven工程下创建一个model工程,名为Eureka-client

第二步:修改pom.xml文件内容如下:

<?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.1.3.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <groupId>com.cn</groupId>
   <artifactId>eureka-client</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>eureka-client</name>
   <description>Demo project for Spring Boot</description>

   <properties>
      <java.version>1.8</java.version>
      <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka</artifactId>
         <version>1.3.5.RELEASE</version>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-netflix-eureka-client</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>

第三步、配置启动中心,代码如下:

package com.cn.eurekaclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient    //通过@EnableEurekaClient表明自己是一个eurekaClient
@ComponentScan("com.cn.controller")
public class EurekaClientApplication {
   public static void main(String[] args) {
      SpringApplication.run(EurekaClientApplication.class, args);
   }
}

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

第四步、修改application.yml配置如下:

#服务的端口号
server:
  port: 8762
#服务别名,服务注册到注册中心的名称
spring:
  application:
    name: service-provide
eureka:
  client:
    service-url:
      #服务注册到eureka的地址
      defaultZone: http://localhost:8761/eureka/

需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name。

第五步、创建Controller层,代码如下:

package com.cn.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "hello springCloud";
    }
}

第六步、测试

启动工程,打开http://localhost:8761,即eureka server的网址,如下图

通过图中,可以看到这个服务已经注册到服务中了,服务名为SERVICE-PROVIDE,端口为8762

猜你喜欢

转载自blog.csdn.net/sinat_23490433/article/details/89185244
今日推荐