人之所以痛苦,那是因为你在成长。--------magic_guo 最近一直在看java微服务电商项目视频,看的时候总觉得自己学会了,但是过了一段时间就会淡忘;尤其是一些配置问题;致力于此困扰,想按照视频的思路自己来搭建一套spring-cloud微服务环境;话不多说,开始吧!
说到微服务,肯定少不了一些组件: 配置中心:config server,nacos等 注册中心:eureka,zookeeper,nacos等… 服务调用:feign,rpc,dubbo等 降级熔断:Hystix,sentinel等 负载均衡:ribbon等 路由网关:zuul,gateway等
本环境使用的是zuul+config+eureka+ribbon+feign+Hystix的一套流程,基于这套流程,如果搞懂了,那么我觉得使用别的组件就会更加容易上手。
版本管理 首先创建一个父工程:名字为pom,来管理所有的模块: 刚开始搭建模块,并不需要太多的依赖,只需导入spring-cloud的依赖,mybatis-plus、mysql以及commons-beanutils这些必要的依赖,其他依赖在后面的开发中慢慢添加; 此模块只需要一个pom文件即可,不需要别的文件,都可以删除; 目录结构: pom文件:
<?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.3.3.RELEAS</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.guo</groupId>
<artifactId>shop-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>shop-pom</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR7</spring-cloud.version>
<mybatis-plus.version>2.3</mybatis-plus.version>
<mysql-version>5.1.6</mysql-version>
</properties>
<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>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>${mysql-version}</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
shop-pom模块搭建完成,接下来开始搭建配置中心:shop-config模块 创建过程: 此模块现在只需要一个config的组件,config组件已经包括了spring-cloud的一些依赖,因此spring-cloud的依赖不用再选择,点击next即可; 整理pom文件,将一些不必要的依赖删除;然后将shop-config的parent改成shop-pom; pom文件:
<?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>com.guo</groupId>
<artifactId>shop-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.guo</groupId>
<artifactId>shop-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>shop-config</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
因为此模块为配置中心,因此需要在启动类上加上@EnableConfigServer注解,这样才能让spring容器知道此模块为配置中心的server端;
package com.guo.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ShopConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ShopConfigApplication.class, args);
}
}
配置yml文件:对于配置中心,yml可以有两种方式来拉取资源: 1.将各模块的文件配置在本地 2.将各模块的文件配置在git 两种方式都可以采用,使用配置可以实现git和本地的切换: 目录结构: 内容: application.yml active可以采取两种方式:git或者native
spring:
profiles:
active: native
application-git.yml
server:
port: 9999
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/guohangshao/shop-2008-copy.git
search-paths: config
default-label: master
application-native.yml
server:
port: 9999
spring:
cloud:
config:
server:
native:
search-locations: classpath:/config
profiles:
active: native
此时在config文件夹下随便写个yml文件,启动shop-config,启动好后,查看端口是否一致;如果一致,则访问http://localhost:9999/application-eureka-server.yml,可以拉取到这个yml即为访问成功; 搭建eureka: 创建方式和上面一样,模块名称为shop-eureka,只是选择的依赖不同: 需要一个eureka的server的服务端依赖、一个config的client端的依赖; pom文件:
<?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>com.guo</groupId>
<artifactId>shop-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.guo</groupId>
<artifactId>shop-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>shop-eureka</name>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
在启动类加上@EnableEurekaClient注解:
package com.guo.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class ShopEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(ShopEurekaApplication.class, args);
}
}
然后编写yml文件:需要一个eureka-server.yml和eureka-client.yml(以便于后后面再创建模块的时候需要重复的写客户端的内容),然后放在配置中心shop-config的resources的config文件夹下; 目录结构: application-eureak-server.yml:
server:
port: 7777
spring:
application:
name: eureka-server
eureka:
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
register-with-eureka: false
fetch-registry: false
instance:
hostname: localhost
application-eureka-client.yml
eureka:
client:
service-url:
defaultZone: http://localhost:7777/eureka
此时要将shop-eureka下的yml更名为boorstrap.yml。原因是: bootstrap.yml(bootstrap.properties)用来程序引导时执行,应用于更加早期配置信息读取,如可以使用来配置application.yml中使用到参数等
application.yml(application.properties) 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。
加载顺序: bootstrap.yml > application.yml > application-dev(prod).yml bootstrap.yml内容:
spring:
cloud:
config:
uri: http://localhost:9999
profile: eureka-server
name: application
每次更改了静态资源,则需要重新启动一下config配置中心;(为保险起见) 然后启动shop-eureka: 看到端口号是自己定义的端口号,日志中是从config配置中心拉取得文件;则代表启动成功; 配置zuul路由网关: 其实每个模块的创建都大同小异,区别在于依赖的选择,yml的配置以及启动类所需要加注解: pom文件:
<?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>com.guo</groupId>
<artifactId>shop-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.guo</groupId>
<artifactId>shop-zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>shop-zuul</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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.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>
</project>
启动类:
package com.guo.zuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ShopZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ShopZuulApplication.class, args);
}
}
yml的配置与eureka相同,配置信息写好后,放在config的config文件夹,将自己的yml改成bootstrap.yml,里面配置拉取的信息和自己单独的配置以及要拉取的模块即可: config下的zuul.yml:
server:
port: 80
spring:
application:
name: shop-zuul
shop-zuul下的bootstrap.yml:
spring:
cloud:
config:
uri: http://localhost:9999
profile: zuul, eureka-client
name: application
zuul:
sensitive-headers:
ribbon:
ReadTimeout: 3000
ConnectTimeout: 3000
然后启动shop-zuul模块,看端口号,以及eureka注册中心是否已经注册上zuul模块;
本文章教学视频来自:https://www.bilibili.com/video/BV1tb4y1Q74E?p=3&t=125
静下心,慢慢来,会很快!
|