Spring Cloud(Greenwich.SR1) - 服务注册,注册中心

42 篇文章 5 订阅
13 篇文章 0 订阅

前言

现在很多公司都在推行目前流行的微服务(2014年)框架,Spring Cloud作为一个目前流行的微服务框架,基于Spring Boot开发而成,具有服务治理,负载均衡,熔断,网关,配置中心,监控,链路跟踪……Spring Cloud不是最完美的,但是一体系解决方案。

Spring Cloud的架构设计是建立在Spring Boot的基础骨架之上的,所以一般的Spring Cloud的教程都是Spring Boot项目上构建,但Spring Cloud的设计理念是跟基础框架无关的,体现了中心化设计思想。

1. 注册中心

Spring Cloud官方的默认的注册中心是eureka server,目前已经停止更新迭代,已有很多替代解决方案,比如:consul

1.1 构建一个eureka server

优先构建一个父项目,命名cloud-parent,引入Spring Cloud的依赖。这里以Greenwich.SR1版本为例。

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

    <groupId>com.feng.spring.cloud</groupId>
    <artifactId>cloud-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cloud-parent</name>
    <description>Demo project for Spring Boot</description>

    <packaging>pom</packaging>

    <modules>
        <module>eureka-server</module>
    </modules>

    <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-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>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>

1.2 构建eureka-server示例

在上面的父项目的基础之上构建Spring Boot的子项目eureka-server。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>com.feng.spring.cloud</groupId>
        <artifactId>cloud-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.feng.eureka.server</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>

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


</project>

需要在Spring boot的Main方法上,加上注解@EnableEurekaServer

package com.feng.eureka.server.eurekaserver;

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);
    }

}

eureka 注册中心需要暴露client注册的URL,因此需要配置application.yml文件(properties文件也可以,spring boot推荐yml文件), 

server:
  port: 8082

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

spring:
  application:
    name: eurka-server

其中

registerWithEureka: false 
fetchRegistry: false

表示自身不需要注册,eureka server可以通过多个服务端相互注册实现高可用。eureka server自身也是一个client,可以相互注册,实现双写模式。

spring:
  application:
    name: eurka-server

 application.name很重要,是Spring Cloud的标识ID。

http://${eureka.instance.hostname}:${server.port}/eureka/

这个地址就是暴露给client的注册地址。 

2. eureka server启动

启动main方法,即可实现一个eureka server注册中心。

访问:localhost:8082,端口自定义,但是避开特殊意义端口,笔者在使用8081的时候,在注册高可用eureka server,会拒绝服务。

3. 客服端注册

客服端需要向eureka server注册,客服端通过向eureka server发送心跳保持通信。新建client的子项目,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">
    <parent>
        <artifactId>cloud-parent</artifactId>
        <groupId>com.feng.spring.cloud</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-client</artifactId>

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

需要在main方法中加上@EnableEurekaClient或者@EnableDiscoveryClient即可

package com.feng.eureka.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
//@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientMain {

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

修改application.yml文件,向eureka-server注册 

server:
  port: 8201

spring:
  application:
    name: service-client

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8082/eureka/

定义名称ID,并向eureka server注册。

启动应用,访问eureka server的网址localhost:8082/

结果如下,标识注册成功:

红字标识没有安全加密,先可以不管他。

 

总结

注册中心使用非常简单,下一章准备consul注册和eureka server高可用模式。 


 


 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值