一.Spring Cloud 简介

        Spring
Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁定,领导选举,分布式
会话,群集状态)。 分布式系统的协调导致锅炉板模式,使用Spring Cloud开发人员可以快速站出实现这些模式的服务和应用程序。
他们可以在任何分布式环境中运行良好,包括开发人员自己的笔记本电脑,裸机数据中心和托管平台,如Cloud Foundry[1]。

二.注册与发现



        由于应用的分解,微服务的引入,服务越来越多,业务系统与服务系统之间的调用,都需要有效管理。这时候需要有一个注册中心来管理所有的微服务,微服务启动之后便在注册中心中注册自己的信息,消费者只需要向注册中心索要某种服务的信息(包括host、port等)即可得到当前可用的服务。更多注册与发现理论可以参考简书中的一篇文章
服务注册与发现 <https://www.jianshu.com/p/c144a577f3d1>。

三.创建服务注册中心

  3.1 创建工程

        进入https://start.spring.io/ <https://start.spring.io/>
,选择MAVEN项目、Java语言、SpringBoot版本等。

        在右方的Search for dependencies 中输入eureka server,选择Eureka
server依赖,最后点击Generate Project按钮下载zip压缩包。


        解压压缩包到工作目录中,解压后的文件夹为Maven项目,在开发目录中导入该Maven项目。其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.ywp</groupId> <artifactId>EurekaServer</artifactId>
<version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging>
<name>EurekaServer</name> <description>Demo for EurekaServer</description>
<parent>         <groupId>org.springframework.boot</groupId>        
<artifactId>spring-boot-starter-parent</artifactId>        
<version>2.0.1.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>     </properties>     <dependencies>      
  <!--eureka server -->         <dependency>            
<groupId>org.springframework.cloud</groupId>            
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>        
</dependency>         <!-- spring boot test-->         <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>Finchley.M7</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>             <snapshots>          
      <enabled>false</enabled>             </snapshots>         </repository>  
  </repositories> </project>
3.2 添加注解

        在Spring Boot启动类上添加@EnableEurekaServer注解。
@SpringBootApplication @EnableEurekaServer public class
EurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args); } }
3.2 添加配置
        在application.properties文件中添加:#注册中心端口 server.port=7001 #主机名,会在控制页面中显示
eureka.instance.hostname=localhost
#通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka
server. eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
        若没有使用.properties文件,则修改appication.yml:server: port: 7001 eureka:
instance: hostname: localhost client: registerWithEureka: false fetchRegistry:
false serviceUrl: defaultZone:
http://${eureka.instance.hostname}:${server.port}/eureka/

3.3 启动服务

        启动Spring Boot项目,在浏览器中输入http://localhost:7001 即可进入Eureka主页面。






四.创建服务提供者



    提供者项目创建方式与注册中心服务器相同,并且做以下修改:

        1. 需要将@EnableEurekaServer改为@EnableEurekaClient。





        2.
spring-cloud-starter-netflix-eureka-server依赖修改为spring-cloud-starter-netflix-eureka-client。

        3. 修改application.properties,具体配置如下:
#服务端口 server.port=7002 #eureka主机名,会在控制页面中显示 eureka.instance.hostname=localhost
#eureka服务器页面中status的请求路径
eureka.instance.status-page-url=http://localhost:7002/index #eureka注册中心服务器地址
eureka.client.service-url.defaultZone=http://localhost:7001/eureka/ #服务名
spring.application.name=fileServer-01
    最后启动项目,再次进入注册中心主页面即可发现DS Replicas->Instances currently registered with
Eureka中有新的服务。









参考文献:

       [1]. 点融黑帮. 服务注册与发现[EB/OL],
https://www.jianshu.com/p/c144a577f3d1,2016.11.18/2018.5.22


其他相关文章:

1.SpringCloud服务注册与发现之服务调用-Feign
<https://blog.csdn.net/yy1098029419/article/details/80776050>


友情链接
ioDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信