SpringCloud-Netflix

Spring Cloud 是一个相对比较新的微服务框架,2016 才推出 1.0 的 Release 版本. 但是其更新特别快,几乎每 1-2 个月就有一次更新,虽然 Spring Cloud 时间最短, 但是相比 Dubbo 等 RPC 框架, Spring Cloud 提供的全套的分布式系统解决方案。

Spring Cloud 为开发者提供了在分布式系统(配置管理,服务发现,熔断,路由,微代理,控制总线,一次性 Token,全局琐,Leader 选举,分布式 Session,集群状态)中快速构建的工具,使用 Spring Cloud 的开发者可以快速的启动服务或构建应用、同时能够快速和云平台资源进行对接。

配置统一依赖管理

创建方法,直接创建文件夹然后写pom.xml

首先创建一个依赖模块,直接创建文件夹hello-spring-cloud作为总的工程目录,然后再在hello-spring-cloud下创建hello-spring-cloud-dependencies作为所有模块的父类

<?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.0.3.RELEASE</version>
</parent> <groupId>com.outlook.liufei32</groupId>
<artifactId>hello-spring-cloud-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging> <name>hello-spring-cloud-dependencies</name>
<url>https://github.com/Swagger-Ranger</url>
<inceptionYear>2018-Now</inceptionYear> <properties>
<!-- Environment Settings -->
<java.version>1.11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- Spring Settings -->
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties> <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>
<!-- Compiler 插件, 设定 JDK 版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<showWarnings>true</showWarnings>
</configuration>
</plugin> <!-- 打包 jar 文件时,配置 manifest 文件,加入 lib 包的 jar 依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
<executions>
<execution>
<configuration>
<archive>
<manifest>
<!-- Add directory entries -->
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin> <!-- resource -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin> <!-- install -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
</plugin> <!-- clean -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
</plugin> <!-- ant -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
</plugin> <!-- dependency -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
</plugins> <pluginManagement>
<plugins>
<!-- Java Document Generate -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin> <!-- YUI Compressor (CSS/JS压缩) -->
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.5.1</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<encoding>UTF-8</encoding>
<jswarn>false</jswarn>
<nosuffix>true</nosuffix>
<linebreakpos>30000</linebreakpos>
<force>true</force>
<includes>
<include>**/*.js</include>
<include>**/*.css</include>
</includes>
<excludes>
<exclude>**/*.min.js</exclude>
<exclude>**/*.min.css</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</pluginManagement> <!-- 资源文件配置 -->
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build> <repositories>
<repository>
<id>aliyun-repos</id>
<name>Aliyun Repository</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository> <repository>
<id>sonatype-repos</id>
<name>Sonatype Repository</name>
<url>https://oss.sonatype.org/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>sonatype-repos-s</id>
<name>Sonatype Repository</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository> <repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> <pluginRepositories>
<pluginRepository>
<id>aliyun-repos</id>
<name>Aliyun Repository</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>

创建服务注册中心

1.在总目录hello-spring-cloud下新建hello-spring-cloud-eureka目录,eureka就是服务注册与发现模块

配置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.outlook.liufei32</groupId>
<artifactId>hello-spring-cloud-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath>
</parent> <artifactId>hello-spring-cloud-eureka</artifactId>
<packaging>jar</packaging> <name>hello-spring-cloud-eureka</name>
<url>https://github.com/Swagger-Ranger</url>
<inceptionYear>2018-Now</inceptionYear> <dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End --> <!-- Spring Cloud Begin -->
<dependency>
<!--<groupId>org.springframework.cloud</groupId>-->
<!--<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>-->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<!-- Spring Cloud End -->
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--指定启动的入口类,因为微服务是jar包运行,jar的运行方式:java -jar 入口类名-->
<mainClass>com.outlook.liufei32.hello.spring.cloud.eureka.EurekaApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

完善maven结构,并新建出启动目录和资源目录

创建application入口,即jar的启动类EurekaApplication注意命令格式为模块名+application;和配置文件application.yml配置服务注册

application.yml

spring:
application:
name: hello-spring-cloud-eureka server:
port: 8761 eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

EurekaApplication.java

package com.outlook.liufei32.hello.spring.cloud.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /*******************************************************************************
* @Copyright (C), 2018-2019,github:Swagger-Ranger
* @FileName: EurekaApplication
* @Author: liufei32@outlook.com
* @Date: 2019/4/2 11:01
* @Description: eureka入口类
* @Aha-eureka:
*******************************************************************************/
@SpringBootApplication
@EnableEurekaServer//开启eureka服务端,所有的服务提供者都要用这个注解注册到eureka服务端,而服务消费者则是@EnableDiscoveryClient注解
public class EurekaApplication { public static void main( String[] args ) {
SpringApplication.run(EurekaApplication.class);
}
}

启动多个服务提供者提供负载均衡

1.在IDE配置中设置多个实例并行

2.再修改配置里的端口



3.启动服务的启动类,就能看到多个实例并行

注意事项

注意maven依赖,常常版本不对应会导致服务注册无法启动,包括JDK的版本,我使用Java11无法启动,但使用Java8则正常

启动成功则在浏览器中访问配置的注册中心IP+端口就能访问

创建服务提供者

创建服务提供者,直接在总包下新建hello-spring-cloud-service-服务模块名,然后相同的步骤,配置pom.xml导入maven依赖,配置启动类Application,配置配置文件application.yml

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.outlook.liufei32</groupId>
<artifactId>hello-spring-cloud-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath>
</parent> <artifactId>hello-spring-cloud-service-admin</artifactId>
<packaging>jar</packaging> <name>hello-spring-cloud-service-admin</name>
<url>https://github.com/Swagger-Ranger</url>
<inceptionYear>2019-Now</inceptionYear> <dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End --> <!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- Spring Cloud End -->
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.outlook.liufei32.hello.spring.cloud.service.admin.ServiceAdminApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

application.yml

spring:
application:
name: hello-spring-cloud-service-admin server:
port: 8762 eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #注册到注册端

ServiceAdminApplication.java

package com.outlook.liufei32.hello.spring.cloud.service.admin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /*******************************************************************************
* @Copyright (C), 2018-2019,github:Swagger-Ranger
* @FileName: ServiceAdminApplication
* @Author: liufei32@outlook.com
* @Date: 2019/4/2 15:25
* @Description: admin服务的入口类
* @Aha-eureka:
*******************************************************************************/
@SpringBootApplication
@EnableEurekaClient
public class ServiceAdminApplication {
public static void main( String[] args ) {
SpringApplication.run(ServiceAdminApplication.class);
}
}

完成后可以新建一个controller测试返回,然后在浏览器中测试http://localhost:8762/hi?message=wagger-ranger

注册成功后在服务端的web页面上就能看到注册的服务,

当 Client 向 Server 注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka Server 从每个 Client 实例接收心跳消息。 如果心跳超时,则通常将该实例从注册 Server 中删除。

创建服务消费者

springcloud-Netflix创建服务消费者

使用SpringCloud-Netflix的更多相关文章

  1. Netflix OSS 和 SpringCloud Netflix简介

    Netflix OSS Netflix是一家互联网流媒体播放商,是美国视频巨头,随着Netflix转型为一家云计算公司,它也开始积极参与开源项目. Netflix OSS(Open Source)就是 ...

  2. SpringCloud学习笔记(三、SpringCloud Netflix Eureka)

    目录: 服务发现简介 SpringCloud Netflix Eureka应用 Eureka高可用 Eureka源码分析 >>> Eureka Client初始化(客户端定时获取服务 ...

  3. 二、springcloud Netflix 注册中心

    Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现.也是springcloud体系中最重要最核心的组 ...

  4. SpringCloud Netflix Ribbon(负载均衡)

    ⒈Ribbon是什么? Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡工具. Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负 ...

  5. SpringCloud Netflix Eureka(服务注册/发现)

    ⒈Eureka是什么? Eureka是Netflix的一个子模块,也是核心模块之一,Eureka是一个基于REST的服务,用于定位服务以实现云端中间层服务发现和故障转移,服务注册与发现对于微服务架构来 ...

  6. SpringCloud学习笔记(六、SpringCloud Netflix Feign)

    目录: feign简介 feign应用 feign简介: feign是一款Netflix开源的声明式.模板化的http客户端,它可以更加便捷.优雅的调用http api:SpringCloud对Net ...

  7. SpringCloud学习笔记(四、SpringCloud Netflix Ribbon)

    目录: Ribbon简介 Ribbon的应用 RestTemplate简介 Ribbon负载均衡源码分析 Ribbon简介: 1.负载均衡是什么 负载均衡,根据其字面意思来说就是让集群服务具有共同完成 ...

  8. SpringCloud Netflix Hystrix

    Hystrix的一些概念 Hystrix是一个容错框架,可以有效停止服务依赖出故障造成的级联故障. 和eureka.ribbon.feign一样,也是Netflix家的开源框架,已被SpringClo ...

  9. SpringCloud Netflix Feign

    调用其它机器上的服务(远程调用)有2种技术:REST.RPC. REST 注入RestTempalte,服务提供者的url要写成RESTful风格,在url中传递参数. 如果参数很多,url会有一长串 ...

  10. SpringCloud Netflix Eureka

    Eureka是Netflix开源的服务发现组件,基于REST,SpringCloud将它集成在子项目Spring Cloud Netflix中,从而实现服务的注册.发现. Eureka包含Server ...

随机推荐

  1. python print 字体颜色

    例子: print '\033[35;43m(1)ip转换成数字\033[0m' \033[35;43m    ===>35列属于字颜色,43列属于背景颜色 字背景颜色范围: 40--49  4 ...

  2. windows下的套接字IO模型

    一般情况下,IO操作的行为受两种因素的影响: IO操作对象的类型(阻塞还是非阻塞) 获取IO操作结果的方式(同步还是异步). 同步就是指操作的发起和操作结果的获取由调用者完成. 异步指操作发起由调用方 ...

  3. Day01:Python入门

    一.编程与编程语言 编程的目的是将人类的思想流程按照某种能够被计算机识别的表达方式传递给计算机,从而让计算机能像人脑一样自动执行工作. 能被计算机所识别的表达方式是编程语言,python就是一门编程语 ...

  4. linux下printf函数为什么不加\n就不能输出相关的内容 ?

    转载请注明出处:http://blog.csdn.net/qq_26093511/article/details/53255970 原因:  输出缓冲区的问题. unix上标准输入输出都是带有缓存的, ...

  5. ASP.NET Core会议管理平台实战_2、基本概念的理解

    id Token携带用户的信息 AccessToken:是否有权限访问资源 看数据库的表,Client相关的表,api的相关的表 Resources把用户的简介抽象出来到IdentityClaims表 ...

  6. Fitnesse-The Slim Tables

    Fitnesse 中Slim支持的表格类型 下表内容路径 Decision Table Supplies the inputs and outputs for decisions. This is s ...

  7. Spring Boot 学习系列(09)—自定义Bean的顺序加载

    此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. Bean 的顺序加载 有些场景中,我们希望编写的Bean能够按照指定的顺序进行加载.比如,有UserServ ...

  8. E20190308-hm

    sweep vt. 扫除; 打扫,清理; 彻底搜索; 掠过; vi. 打扫; 扫过; 蜿蜒; 大范围伸展; n. 打扫; 延伸; 挥动; 全胜;

  9. lightoj1066【BFS】

    题意: 就是按照A->B->C->D....去拿,求步数: 思路: 有一个注意点:如果碰到合法字母吃掉,再以后的某次可以重新到改点: BFS的因为标记而减少了重复位置的到达,但是按照 ...

  10. bzoj 4541: [Hnoi2016]矿区【平面图转对偶图+生成树】

    首先平面图转对偶图,大概思路是每条边存正反,每个点存出边按极角排序,然后找每条边在它到达点的出边中极角排序的下一个,这样一定是这条边所属最小多边形的临边,然后根据next边找出所有多边形,用三角剖分计 ...