使用spring cloud 一

使用spring cloud

首先介绍一下spring cloud,spring cloud 是基于spring boot的分布式微服务框架,以划分服务为为分布式思想.集成了很多优秀的组件,其中主要组件有Netflix(包括 eureka:注册中心,ribbon 负载均衡,zull:网关,Hystrix:断路器)等 众多优秀的组件.此文我们以搭建框架展开.

首先介绍一下我的spring boot版本
我使用的是2.1.0 spring cloud 是 F版本
步入正题
创建maven项目 导入依赖(父依赖)

1 <properties> 2 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 3 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 4 <java.version>1.8</java.version> 5 <spring-cloud.version>Greenwich.M3</spring-cloud.version> 6 </properties> 7 8 <parent> 9 <groupId>org.springframework.boot</groupId> 10 <artifactId>spring-boot-starter-parent</artifactId> 11 <version>2.1.0.RELEASE</version> 12 <relativePath/> <!-- lookup parent from repository --> 13 </parent> 14 <dependencies> 15 <dependency> 16 <groupId>org.springframework.cloud</groupId> 17 <artifactId>spring-cloud-starter</artifactId> 18 </dependency> 19 <dependency> 20 <groupId>org.springframework.boot</groupId> 21 <artifactId>spring-boot-starter-web</artifactId> 22 <exclusions> 23 <exclusion> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-tomcat</artifactId> 26 </exclusion> 27 </exclusions> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-undertow</artifactId> 32 </dependency> 33 <dependency> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-starter-logging</artifactId> 36 </dependency> 37 <dependency> 38 <groupId>org.springframework.cloud</groupId> 39 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 40 </dependency> 41 <dependency> 42 <groupId>org.springframework.cloud</groupId> 43 <artifactId>spring-cloud-starter-netflix-zuul</artifactId> 44 </dependency> 45 <dependency> 46 <groupId>org.springframework.cloud</groupId> 47 <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> 48 </dependency> 49 <dependency> 50 <groupId>mysql</groupId> 51 <artifactId>mysql-connector-java</artifactId> 52 </dependency> 53 <dependency> 54 <groupId>com.mchange</groupId> 55 <artifactId>c3p0</artifactId> 56 <version>0.9.5.2</version> 57 </dependency> 58 </dependencies> 59 <dependencyManagement> 60 <dependencies> 61 <dependency> 62 <groupId>org.springframework.cloud</groupId> 63 <artifactId>spring-cloud-dependencies</artifactId> 64 <version>${spring-cloud.version}</version> 65 <type>pom</type> 66 <scope>import</scope> 67 </dependency> 68 </dependencies> 69 </dependencyManagement> 70 71

然后在创建一个Model 作为注册中心
添加pom

1 <dependencies> 2 <!-- 引入eureka server依赖 --> 3 <dependency> 4 <groupId>org.springframework.cloud</groupId> 5 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 6 </dependency> 7 </dependencies> 8 9

接着创建启动类

1@EnableEurekaServer 2@SpringBootApplication 3public class EurekaServerApplication { 4 public static void main(String[] args) { 5 SpringApplication.run(EurekaServerApplication.class,args); 6 } 7} 8 9

编写配置文件

1server: 2 hostname: http://localhost #定义路径及端口号 3 port: 8000 4 5spring: 6 application: 7 name: Eys-server-eureka //项目名字 8 9eureka: 10 client: 11 #表示是否注册Eureka服务器,因为自身作为服务注册中心,所以为false 12 register-with-eureka: false 13 #是否从eureka上获取注册信息 14 fetch-registry: false 15 serviceUrl.defaultZone: ${server.hostname}:${server.port}/eureka/ 16 17

这时我们注册中心已经搭建完成,然后启动项目浏览器输入 localhost:8080

这样说明了我们注册中心已经成功启动了.
接下来创建服务向注册中心注册
创建一个新的Model 添加pom依赖

1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.cloud</groupId> 4 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-data-jpa</artifactId> 9 </dependency> 10 </dependencies> 11 12

创建yml配置文件 添加以下配置

1spring: 2 application: 3 name: Eys-service-company 4 jpa:#配置 jpa 5 database: MYSQL 6 show-sql: true 7 database-platform: org.hibernate.dialect.MySQL5InnoDBDialect 8 hibernate: 9 ddl-auto: none 10 datasource: #数据源 11 url: jdbc:mysql://127.0.0.1:3306/eys_jpa?characterEncoding=utf-8&serverTimezone=GMT 12 username: root 13 password: 123456 14 driver-class-name: com.mysql.cj.jdbc.Driver 15 type: com.mchange.v2.c3p0.ComboPooledDataSource 16server: 17 port: 8002 18eureka: #配置向注册中心注册地址 19 client: 20 serviceUrl: 21 defaultZone: http://localhost:8000/eureka/ 22 23

创建启动类

1@EnableEurekaClient 2@SpringBootApplication 3public class CompanyApplication { 4 public static void main(String[] args) { 5 SpringApplication.run(CompanyApplication.class,args); 6 } 7} 8 9

启动

一个简单的项目就这样搭建完成了 下一期再给大家详细介绍其他组件, 再见

代码交流 2021