java 分布式微服务 springcould简单入门使用
本次以一个商场web项目为例介绍springCould 的环境搭建
** 拓扑图如下**
总共分为三个子系统:,门户,用户管理,订单管理且都分别分布到三个不同的服务器上+一个注册服务器
-
创建父工程
1.1这里笔者定义了两个父工程parent1 和parent2,定义俩个父工程是为了方便子系统直接继承里面的依赖,就不用没个子系统都重写依赖代码
创建这两个父工程的时候一定要记得选pom
parent1 里面是三个子系统和服务器都必须的公共的依赖(代码如下) 这里主要是springboot的一下依赖以及junit测试的依赖
<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.yc</groupId>
<artifactId>C71-S3-Pljq-ebuy-parent1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>
</project>
parent2继承自parent1,里面放置的是三个子系统共同的依赖(这里就不直接放代码了不然篇幅就太长)
- 开始建立子系统 (这里就以一个为例index 门户系统为例) ;
三个系统都是继承自parent2创建工程的时候就和创建普通的maven工程一样选jar
配置yaml文件这里只配置了三个端口8000和8003,8004即只有三个子系统
server:
port: 8000
#服务名配置(Eureka服务注册时候内部访问地址)
spring:
application:
name: ebuy-index
eureka:
client:
serviceUrl:
# 注册服务器的地址
defaultZone: http://127.0.0.1:8000/eureka
---
# 分区配置名称
spring:
profiles: index-8003
server:
port: 8003
---
# 分区配置名称
spring:
profiles: index-8004
server:
port: 8003
# 日志配置
logging:
level:
com:
yc:
ebuy:
dao: debug
web: warn
- 启动类的配置
package com.yc.ebuy;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.yc.ebuy.web.LoginInterceptor;
@SpringBootApplication
public class EbuyIndexAppcation {
public static void main(String[] args) {
SpringApplication.run(EbuyIndexAppcation.class, args);
}
}
-
创建Eureka服务器工程 ;
继承parent2加入eureka服务器依赖 -
配置yaml文件
# 工程配置 ==>name 默认的服务名
spring:
application:
name: ebuy-server
#服务器端口配置
server:
port: 8761
#注册集群配置
eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
# 注册服务器的地址
defaultZone: http://127.0.0.1:8761/eureka
- 启动类
*至此基本配置和部署完成,启动子系统启动,要先启动eureka服务
在浏览器上输入127.0.0.1:8761
看到如下就算完成了