基于spring-cloud实现eureka注册服务小案例

本案例来自于spring官网:http://spring.io/guides/gs/service-registration-and-discovery/
JDK要求1.8或以上版本。

首先创建两个项目,eureka-service和eureka-client。
eureka-server作为eureka的服务端,提供注册服务,eureka-client作为eureka的客户端,属于一个应用,注册到eureka注册中心。

eureka-service的配置文件pom.xml如下:


<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.0modelVersion>

    <groupId>com.examplegroupId>
    <artifactId>eureka-serviceartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.3.5.RELEASEversion>   
        <relativePath/> 
    parent>

    <properties>   
        
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eureka-serverartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-parentartifactId>
                <version>Brixton.SR3version>   
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

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


project>

eureka-service项目中创建包,自定,本例为:com.spring
在该包下创建类用于启动服务EurekaServiceApplication.java

package com.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer         //开启eureka服务
@SpringBootApplication      //springBoot注解,spring在springBoot基础之上来构建项目
public class EurekaServiceApplication {

    //spirng boot的标准入口
    public static void main(String[] args) {
        SpringApplication.run(EurekaServiceApplication.class, args);
    }
}

关于eureka-service的配置文件 applicaiton.yml 或者使用application.properties,两者格式不同。
application.yml

server:
  port: 8761     #指定服务端口
eureka:
  client:
    registerWithEureka: false  #是否将eureka自身作为应用注册到eureka注册中心
    fetchRegistry: false       #为true时,可以启动,但报异常:Cannot execute request on any known server

简单的eureka-service就写好了,运行下EurekaServiceApplication.java
访问:http://localhost:8761/

关于eureks-client 的pom.xml文件如下,因为不提供其他服务,这里基本与eureka-service一样:

<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.0modelVersion>
  <groupId>com.huawei.itgroupId>
  <artifactId>eureka-clientartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>jarpackaging>

  <parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>1.3.5.RELEASEversion>
    <relativePath/>
  parent>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <java.version>1.8java.version>
  properties>

  <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-parentartifactId>
            <version>Brixton.SR3version>
            <type>pomtype>
            <scope>importscope>
        dependency>
    dependencies>
  dependencyManagement>

  <dependencies>
      
      <dependency>
          <groupId>org.springframework.cloudgroupId>
          <artifactId>spring-cloud-starter-eurekaartifactId>
      dependency>
      <dependency>
          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-starter-testartifactId>
          <scope>testscope>
      dependency>
  dependencies>

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

project>

同样创建入口类EurekaClientApplication.java

package com.spring;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Administrator
 *
 */
@EnableDiscoveryClient            //通过该注解,实现服务发现,注册
@SpringBootApplication
public class EurekaClientApplication {

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

}

@RestController
class ServiceInstanceRestController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/service-instances/{applicationName}")
    public List serviceInstancesByApplicationName(
            @PathVariable String applicationName) {
        return this.discoveryClient.getInstances(applicationName);
    }

    @RequestMapping("/")
    public String sayhello() {
        return "hello";
    }
}

关于eureka-client的配置文件bootstrap.yml和application.yml,
bootstrap.yml先于application.yml加载,一般不变的东西卸载bootstrap里,使用一样:
application.yml

server:
  port: 7070
spring:
  application:
    name: cloud-client    #为你的应用起个名字,该名字将注册到eureka注册中心

好了,现在启动eureka-client,再次访问http://localhost:8761,可能需要等一会
基于spring-cloud实现eureka注册服务小案例_第1张图片

访问 http://localhost:7070/service-instances/cloud-client/ ,这里我把应用名改了
会出现关于应用的信息:
基于spring-cloud实现eureka注册服务小案例_第2张图片

这里因为两个都部署在同一台机器上,所以可以直接发现服务,并注册,如果在不同的机器上不部署,application.yml配置中需要加上eureka的地址如,多个eureka服务地址,可以用,号隔开:

eureka:
  client:
    serviceUrl:
      defaultZone: http://${eureka-serviceIP}:8761/eureka/

你可能感兴趣的:(spring,boot)