本文介绍了一个最简单的SpringCloud项目,有注册中心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 https://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.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.qi</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>eureka-server project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RC2</spring-cloud.version>
</properties>
<dependencies>
<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-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
application.yml文件
server:
port: 8761
eureka:
instance:
hostname: eureka-server
client:
register-with-eureka: false #不把自己注册到注册中心,因为本身就是注册中心的存在
fetch-registry: false #不从eureka上获取注册信息
service-url:
defaultZone: http://localhost:8761/eureka #配置默认的启动路径
springboot启动类:EurekaServerApplication.java
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
二、生产者
application.yml文件
server:
port: 8082
spring:
application:
name: provider
rabbitmq: //配置RabbitMQ
host: localhost
port: 5672
username: guest
password: guest
datasource: //配置数据库
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
mybatis:
mapper-locations: classpath:mapping/*Mapper.xml
type-aliases-package: com.qi.entity
eureka:
instance:
prefer-ip-address: true #注册服务的时候使用服务的IP地址
client:
service-url:
defaultZone: http://localhost:8761/eureka
register-with-eureka: true
##检索服务信息
fetch-registry: true
启动类:ProviderApplication .java
@MapperScan("com.qi.mapper")
@SpringBootApplication
@EnableEurekaClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
三、消费者
application.yml文件
server:
port: 8083
spring:
application:
name: consumer
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
thymeleaf:
cache: false #开发时关闭缓存,不然没法看到实时页面
prefix: classpath:/templates/
suffix: .html
encoding: UTF-8
mode: HTML5
servlet:
content-type: text/html
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:8761/eureka
启动类:ConsumerApplication .java
@EnableDiscoveryClient // 开启发现服务
@SpringBootApplication
@EnableHystrix //启动容错保护
@ServletComponentScan("com.qi.servlet")
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@LoadBalanced // 启用负载均衡服务
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
一个简单的SpringCloud项目就这样就可以搭建完成,启动顺序是:注册中心eureka、生产者、消费者。项目详情见GitHub地址:https://github.com/Q631124/SpringCloud