SpringCloud之服务注册与发现Spring Cloud Eureka实例代码

一、Spring Cloud简介

Spring Cloud是一个基千SpringBoot实现的微服务架构开发 工具。它为微服务架构中涉及的 配置管理、服务治理、 断路器、 智能路由、微代理、 控制总线、 全局锁、 决策竞选、分布式会话和集群状态管理等操作提供了一种简单的开发方式。
Spring Cloud包含了多个子项目(针对分布式系统中涉及的多个不同开源产品,还可能会新增),如下所述。

Spring Cloud Config: 配置管理工具、Spring Cloud Netflix: 核心组件、Spring Cloud Bus: 事件、消息总线等等。

二、Spring Cloud Eureka

Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件中的一部分, 它基于 NetflixEureka 做了二次封装, 主要负责完成微服务架构中的服务治理功能。 Spring Cloud 通过为Eureka 增加了 Spring Boot 风格的自动化配置,我们只需通过简单引入依赖和注解配置就能让 Spring Boot 构建的微服务应用轻松地与 Eureka 服务治理体系进行整合。

服务治理可以说是微服务架构中最为核心和基础的模块, 它主要用来实现各个微服务实例的自动化注册与发现。

三、实例

(1)工具:IntelliJ IDEA

(2)新建一个空项目


(3)新建一个Eureka Server,Module,名为:eurekaserver

工程右键->创建Module-> 选择spring initialir ->填好项目名,下一步->,如图选择Eureka Server:

(3-1)pom.xml

<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-web</artifactId> 
</dependency> 
 
<dependency> 
  <groupId>org.springframework.cloud</groupId> 
  <artifactId>spring-cloud-starter-eureka-server</artifactId> 
</dependency> 
 
<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-test</artifactId> 
  <scope>test</scope> 
</dependency> 

(3-2)application.yml

server: 
 port: 5555 
 
eureka: 
 instance: 
  hostname: localhost 
 client: 
  registerWithEureka: false 
  fetchRegistry: false 
  serviceUrl: 
   defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ 

备注:eureka.client.register-with-eureka: 由于该应用为注册中心,所以设置为 false, 代表不向注册中心注册自己。
eureka.client.fetch-registry: 由于注册中心的职责就是维护服务实例,它并不需要去检索服务, 所以也设置为 false。

(3-3)入口类

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 
 
@EnableEurekaServer 
@SpringBootApplication 
public class EurekaserverApplication { 
 
  public static void main(String[] args) { 
    SpringApplication.run(EurekaserverApplication.class, args); 
  } 
} 

(3-4)启动测试


在完成了上面的配置后,启动应用并访问 http://localhost:5555/。可以看到如下图所示的 Eureka 信息面板, 其中 Instances currently registered with Eureka 栏是空的, 说明该注册中心还没有注册任何服务。

(4)注册服务提供者在完成了服务注册中心的搭建之后,接下来我们尝试将一个既有的 Spring Boot 应用加入 Emeka 的服务治理体系中去。

(5)再新建一个Eureka Client,Module,名为:helloserver,这个helloserver作为eurekaserver的子model


(6)改造父model和子model的pom配置(6-1)eurekaserver的pom.xml配置:

<packaging>pom</packaging> 
<modules> 
  <module>../helloserver</module> 
</modules> 

(6-2)eurekaserver的全部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> 
  <groupId>com.example</groupId> 
  <artifactId>demoeurekaserver</artifactId> 
  <version>0.0.1-SNAPSHOT</version> 
  <packaging>pom</packaging> 
  <modules> 
    <module>../helloserver</module> 
  </modules> 
  <name>eurekaserver</name> 
  <description>Demo project for Spring Boot</description> 
 
  <parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.5.9.RELEASE</version> 
    <relativePath/> <!-- lookup parent from repository --> 
  </parent> 
 
  <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>Edgware.RELEASE</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-server</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> 

(6-3)helloserver的pom.xml配置:

<parent> 
  <groupId>com.example</groupId> 
  <artifactId>demoeurekaserver</artifactId> 
  <version>0.0.1-SNAPSHOT</version> 
  <relativePath>../eurekaserver/pom.xml</relativePath> 
</parent> 

(6-4)helloserver的全部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>com.example</groupId> 
    <artifactId>demoeurekaserver</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <relativePath>../eurekaserver/pom.xml</relativePath> 
  </parent> 
  <artifactId>helloserver</artifactId> 
  <packaging>jar</packaging> 
  <name>helloserver</name> 
  <description>Demo project for Spring Boot</description> 
  <properties> 
    <start-class>com.example.helloserver.HelloserverApplication</start-class> 
  </properties> 
  <dependencies> 
    <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> 

(6-5)helloserver的application.yml配置:

server: 
 port: 5556 
 
spring: 
 application: 
  name: helloserver 
eureka: 
 client: 
  serviceUrl: 
   defaultZone: http://localhost:5555/eureka/ 

(6-6)helloserver的启动类:

@EnableEurekaServer 
@SpringBootApplication 
@RestController 
public class HelloserverApplication { 
  private final Logger log = (Logger) LoggerFactory.getLogger(HelloserverApplication.class); 
  @Autowired 
  private DiscoveryClient client; 
 
  @RequestMapping(name = "/hello", method = RequestMethod.GET) 
  public String index() { 
    ServiceInstance instance = client.getLocalServiceInstance(); 
    log.info("/hello, host:" + instance.getHost() + ",service_id:" + instance.getServiceId()); 
    return "Hello SpringCloud~"; 
  } 
 
  public static void main(String[] args) { 
    SpringApplication.run(HelloserverApplication.class, args); 
  } 
} 

(7)分别启动eurekaserver和helloserver,并测试:

(7-1)访问eurekaserver:(可以很清楚的看到HELLOSERVER信息)


(7-2)访问helloserver:


(7-3)查看helloserver控制台信息:


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

代码技巧

转载请关注公众号:代码技巧 回复:授权

本文链接地址:https://www.oudahe.com/p/51996/