使用spring cloud
首先介绍一下spring cloud,spring cloud 是基于spring boot的分布式微服务框架,以划分服务为为分布式思想.集成了很多优秀的组件,其中主要组件有Netflix(包括 eureka:注册中心,ribbon 负载均衡,zull:网关,Hystrix:断路器)等 众多优秀的组件.此文我们以搭建框架展开.
首先介绍一下我的spring boot版本
我使用的是2.1.0 spring cloud 是 F版本
步入正题
创建maven项目 导入依赖(父依赖)
<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>Greenwich.M3</spring-cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</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>
然后在创建一个Model 作为注册中心
添加pom
<dependencies>
<!-- 引入eureka server依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
接着创建启动类
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
编写配置文件
server:
hostname: http://localhost #定义路径及端口号
port: 8000
spring:
application:
name: Eys-server-eureka //项目名字
eureka:
client:
#表示是否注册Eureka服务器,因为自身作为服务注册中心,所以为false
register-with-eureka: false
#是否从eureka上获取注册信息
fetch-registry: false
serviceUrl.defaultZone: ${server.hostname}:${server.port}/eureka/
这时我们注册中心已经搭建完成,然后启动项目浏览器输入 localhost:8080
这样说明了我们注册中心已经成功启动了.
接下来创建服务向注册中心注册
创建一个新的Model 添加pom依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
创建yml配置文件 添加以下配置
spring:
application:
name: Eys-service-company
jpa:#配置 jpa
database: MYSQL
show-sql: true
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
hibernate:
ddl-auto: none
datasource: #数据源
url: jdbc:mysql://127.0.0.1:3306/eys_jpa?characterEncoding=utf-8&serverTimezone=GMT
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.mchange.v2.c3p0.ComboPooledDataSource
server:
port: 8002
eureka: #配置向注册中心注册地址
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/
创建启动类
@EnableEurekaClient
@SpringBootApplication
public class CompanyApplication {
public static void main(String[] args) {
SpringApplication.run(CompanyApplication.class,args);
}
}
启动
一个简单的项目就这样搭建完成了 下一期再给大家详细介绍其他组件,再见