Spring Cloud搭建注册中心、本地文件系统、服务环境搭建
父Pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
</parent>
<modules>
<module>os_configserve</module>
<module>os_eurekaserve</module>
<module>order</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
注册中心(EureKa配置)
Maven配置:基本上的错误为版本号错误,版本错误见前面博客
- pomx.ml
<parent>
<artifactId>OnlineShop</artifactId>
<groupId>com.li</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
启动类
@SpringBootApplication
@EnableEurekaServer
public class OsEurekaserveApplication {
public static void main(String[] args) {
SpringApplication.run(OsEurekaserveApplication.class, args);
}
}
application.yml
# 端口号
server:
port: 8001
spring:
application:
name: eureka-provider
eureka:
instance:
hostname: localhost # Eureka访问地址
client:
register-with-eureka: false # 禁止自己注册自己
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
本地文件系统
Maven配置
- pomx.ml
<parent>
<artifactId>OnlineShop</artifactId>
<groupId>com.li</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>os_configserve</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
启动类
@SpringBootApplication
@EnableConfigServer
public class OsConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(OsConfigServerApplication.class,args);
}
}
application.yml
server:
port: 8002
spring:
application:
name: config-provider
profiles:
active: native # 配置文件的获取方式本地读取
cloud:
config:
server:
native:
search-locations: classpath:/shared # 本地配置文件的存放位置
配置读取文件的路径,在resources文件夹下建/shared文件夹,并且创建order-dev.yml配置文件,和后面的服务互相对应。order-dev.yml的内容如下
server:
port: 8010 # order-dev的端口号
服务提供
pom.xml
<parent>
<artifactId>OnlineShop</artifactId>
<groupId>com.li</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
启动类
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
}
测试类
@RestController
@RequestMapping("/order")
public class OrderController {
@Value("${server.port}")
private String port;
@GetMapping("/index")
public String index(){
return "order的端口是"+this.port;
}
}
bootstrap.yml配置文件
spring:
application:
name: order #之前在配置文件中的文件名字order-dev.yml
profiles:
active: dev
cloud:
config:
uri: http://localhost:8002 # 配置configserver的端口号
fail-fast: true
依次启动注册中心、本地文件系统、服务
浏览器访问 localhost:8001 如下可以看到注册的服务
访问测试地址如下