您的位置:首页 > 编程语言 > Java开发

微服务笔记之Spring Cloud 服务的注册与发现(Eureka)

2018-11-05 16:40 731 查看

SpringCloud Eureka 是一个服务注册和发现模块。是SpringCloud Netflix服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的服务治理功能。

一、 创建maven主工程项目。

     主pom.xml文件配置如下:

[code]<?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-instan
4000
ce"
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>

<groupId>com.springcloud.lcl</groupId>
<artifactId>learnparent</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>eurekaserver</module>
<module>servicehi</module>
</modules>
<packaging>pom</packaging>

<name>learnparent</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>

<dependencies>
<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>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

</plugins>
</pluginManagement>
</build>
</project>

  其中:<parent>部分导入了版本2.0.X的spring-boot有关的依赖包;

            <dependencyManagement>部分导入了版本Finchley.RELEASE的spring-cloud有关的依赖包;

            此处的版本必须和2.0以上的spring-boot一起配置;

二、在主工程上创建一个module:名称为 eurekaserver, 用作服务注册中心,即注册中心服务端;

    2.1 pom.xml配置如下:

[code]<?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">
<parent>
<artifactId>learnparent</artifactId>
<groupId>com.springcloud.lcl</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.springcloud.lcl</groupId>
<artifactId>eurekaserver</artifactId>
<packaging>jar</packaging>

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

2.2  配置注册中心服务,在eurekaserver工程(springboot)的启动application类上,添加注解@EnableEurekaServer;

    代码如下:

[code]package com.springcloud.lcl;

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

/**
* @author lcl
* @version 0.1
* @date 2018/10/13
**/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

  2.3   spring boot的配置文件appication.yml 中配置 eureka server有关;

    配置文件如下:

[code]spring:
application:
name: eurka-server

server:
port: 8761

eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/

   默认情况下erureka server也是一个eureka client ,必须要指定一个 server; 

  其中eureka.client.registerWithEureka:false和fetchRegistry:false来说明自己是一个eureka server;

  2.4  启动 EurekaServerApplication,即启动了注册中心服务;

三、 在主工程上创建一个module:名称为 servicehi, 用作一个要向服务注册中心注册的服务,即eureka client服务;

  3.1  pom.xml文件配置如下:

[code]<?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">
<parent>
<artifactId>learnparent</artifactId>
<groupId>com.springcloud.lcl</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.springcloud.lcl</groupId>
<modelVersion>4.0.0</modelVersion>

<artifactId>servicehi</artifactId>
<packaging>jar</packaging>
<name>servicehi</name>

<dependencies>
<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>
<!--<version>2.0.1.RELEASE</version>-->
</dependency>
</dependencies>

</project>

3.2  编写servicehi工程(springboot)的启动application类,添加注解@EnableEurekaClient;

    代码如下:

[code]package com.springcloud.service;

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.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
* @author lcl
* @version 0.1
* @date 2018/10/13
**/
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication {

private static final Logger logger = Logger.getLogger(ServiceHiApplication.class.getName());

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

@Value("${server.port}")
String port;

@RequestMapping("/hi")
public String home(@RequestParam(value = "name", defaultValue = "test1") String name) {
String msg = "calling trace service-hi: "+ "hi " + name + " ,i am from port:" + port;
logger.log(Level.INFO,msg);
return msg;
}

@RequestMapping("/info")
public String info(){
logger.log(Level.INFO, "calling trace service-hi info .");
return "I'm service-hi";
}

}

3.3   spring boot的配置文件application.properties(也可用yml方式) 中配置 eureka有关;

    配置文件如下:

[code]server.port=8762

spring.application.name=service-hi

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

  其中spring.application.name就是向注册中心的注册的服务名,其他服务也是通过此名来访问调用;

   eureka.client.serviceUrl.defaultZone用来配置注册中心的服务地址;

3.4   启动 ServiceHiApplication,即启动了服务,会自动向注册中心注册此服务;

四、上述注册中心服务和服务提供者都启动后,可在浏览器中通过地址: http://localhost:8761/ 查看注册中心的状态;

图如下: 可看到 SERVICE-HI已在注册中心能够看到;

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: