简单的springcloud搭建
注册中心:eureka
接口服务:server
web服务:web
网关:zuul
配置中心:config
pom参数统一管理:common
1、创建一个空的maven项目为springcloud
2、修改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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xiaostudy</groupId> <artifactId>springcloudTest</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> </modules> <build> <plugins> <!--参考文章:https://blog.csdn.net/liupeifeng3514/article/details/80236077--> <plugin> <!-- 指定maven编译的jdk版本,如果不指定,maven3默认用jdk 1.5 maven2默认用jdk1.3 --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <!-- 源代码使用的JDK版本 --> <source>1.8</source> <!-- 需要生成的目标class文件的编译版本 --> <target>1.8</target> <!-- 字符集编码 --> <encoding>UTF-8</encoding> <!-- 跳过测试 --> <skip>true</skip> </configuration> </plugin> </plugins> </build> </project>
3、创建注册中心Eureka
3.1、配置注册中心eureka的配置文件application.yml
1 server: 2 port: 9900 3 4 eureka: 5 instance: 6 hostname: localhost 7 client: 8 # 声明是否将自己的信息注册到 Eureka 服务器上 9 registerWithEureka: false 10 # 是否到 Eureka 服务器中抓取注册信息 11 fetchRegistry: false 12 serviceUrl: 13 defaultZone: http://@eureka.user.name@:@eureka.user.password@@${eureka.instance.hostname}:${server.port}/eureka/ 14 15 16 spring: 17 application: 18 name: eurka-service 19 security: 20 user: 21 name: @eureka.user.name@ 22 password: @eureka.user.password@
<?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.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xiaostudy</groupId> <artifactId>eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR3</spring-cloud.version> <eureka.user.name>admin</eureka.user.name> <eureka.user.password>xiaostudy.test</eureka.user.password> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!--为eureka配置账号密码--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</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> <resources> <resource> <!--指定resources插件处理哪个目录下的资源文件--> <directory>src/main/resources</directory> <!--打开资源过滤功能--> <filtering>true</filtering> </resource> </resources> </build> </project>
3.2、添加@EnableEurekaServer注解
3.3、启动注册中心
注:如果启动报读取不了yml文件,可能是编码问题。把编码设置成utf-8就可以了。
3.4、访问
注:如果启动后登陆不了,可以试试添加配置类(必须,不然其他服务发现不了注册中心)
1 package com.xiaostudy.eureka.config; 2 3 import org.springframework.security.config.annotation.web.builders.HttpSecurity; 4 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 5 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 6 7 @EnableWebSecurity 8 public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 9 10 @Override 11 protected void configure(HttpSecurity http) throws Exception { 12 //关闭csrf 13 http.csrf().disable(); 14 super.configure(http); 15 } 16 }
4、创建一个server模块,模拟发布接口
4.1、添加注解@EnableDiscoveryClient和@EnableFeignClients
server的yml文件
1 server: 2 port: 9901 3 4 eureka: 5 instance: 6 preferIpAddress: true 7 instance-id: ${spring.cloud.client.ip-address}:${server.port} 8 client: 9 serviceUrl: 10 defaultZone: http://@eureka.user.name@:@eureka.user.password@@localhost:9900/eureka/ 11 12 spring: 13 application: 14 name: server-service 15 cloud: 16 loadbalancer: 17 retry: 18 enabled: false
<?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.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xiaostudy</groupId> <artifactId>server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>server</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR3</spring-cloud.version> <eureka.user.name>admin</eureka.user.name> <eureka.user.password>xiaostudy.test</eureka.user.password> </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-openfeign</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> <resources> <resource> <!--指定resources插件处理哪个目录下的资源文件--> <directory>src/main/resources</directory> <!--打开资源过滤功能--> <filtering>true</filtering> </resource> </resources> </build> </project>
4.2、创建services及实现类、controller和接口
services接口
package com.xiaostudy.server.services; public interface TestServices { public String get(String name); }
services实现类
package com.xiaostudy.server.services.impl; import com.xiaostudy.server.services.TestServices; import org.springframework.stereotype.Service; @Service public class TestServicesImpl implements TestServices { @Override public String get(String name) { return "参数name:" + name; } }
controller
package com.xiaostudy.server.controller; import com.xiaostudy.server.services.TestServices; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test") public class TestController { @Autowired private TestServices testServices; @RequestMapping("/get") public String get(String name) { return testServices.get(name); } }
接口
package com.xiaostudy.server.apis; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; @FeignClient(name = "server-service", contextId = "TestServicesApis") public interface TestServicesApis { @RequestMapping("/test/get") public String get(String name); }
4.3、启动server
5、添加一个web模块,通过接口调用服务
<?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.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xiaostudy</groupId> <artifactId>web</artifactId> <version>0.0.1-SNAPSHOT</version> <name>web</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR3</spring-cloud.version> <eureka.user.name>admin</eureka.user.name> <eureka.user.password>xiaostudy.test</eureka.user.password> </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-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>com.xiaostudy</groupId> <artifactId>server</artifactId> <version>0.0.1-SNAPSHOT</version> </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> <resources> <resource> <!--指定resources插件处理哪个目录下的资源文件--> <directory>src/main/resources</directory> <!--打开资源过滤功能--> <filtering>true</filtering> </resource> </resources> </build> </project>
web的配置文件application.yml
server: port: 9902 eureka: instance: preferIpAddress: true instance-id: ${spring.cloud.client.ip-address}:${server.port} client: serviceUrl: defaultZone: http://@eureka.user.name@:@eureka.user.password@@localhost:9900/eureka/ spring: application: name: web-service cloud: loadbalancer: retry: enabled: false
web的controller
package com.xiaostudy.web.controller; import com.xiaostudy.server.apis.TestServicesApis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test") public class TestWebController { @Autowired private TestServicesApis testServicesApis; @RequestMapping("/getTest") public String getTest() { return testServicesApis.get("通过web调用的"); } }
启动web
6、添加网关zuul
<?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.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xiaostudy</groupId> <artifactId>zuul</artifactId> <version>0.0.1-SNAPSHOT</version> <name>zuul</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR3</spring-cloud.version> <eureka.user.name>admin</eureka.user.name> <eureka.user.password>xiaostudy.test</eureka.user.password> </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-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> <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> <resources> <resource> <!--指定resources插件处理哪个目录下的资源文件--> <directory>src/main/resources</directory> <!--打开资源过滤功能--> <filtering>true</filtering> </resource> </resources> </build> </project>
server:
port: 9903
eureka:
instance:
preferIpAddress: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
client:
serviceUrl:
defaultZone: http://@eureka.user.name@:@eureka.user.password@@localhost:9900/eureka/
spring:
application:
name: zuul-service
cloud:
loadbalancer:
retry:
enabled: false
zuul:
routes:
server:
path: /server/**
serviceId: server-service
sensitiveHeaders: "*"
web:
path: /web/**
serviceId: web-service
sensitiveHeaders: "*"
添加注解@EnableDiscoveryClient和@EnableZuulProxy
启动zuul
7、添加配置中心
config的application.yml
server:
port: 9904
eureka:
instance:
preferIpAddress: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
client:
serviceUrl:
defaultZone: http://@eureka.user.name@:@eureka.user.password@@localhost:9900/eureka/
spring:
application:
name: config-service
cloud:
loadbalancer:
retry:
enabled: false
profiles:
active: native
添加注解@EnableDiscoveryClient和@EnableConfigServer
或者
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency>
server:
port: 9902
eureka:
instance:
preferIpAddress: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
client:
serviceUrl:
defaultZone: http://@eureka.user.name@:@eureka.user.password@@localhost:9900/eureka/
spring:
application:
name: web-service
cloud:
loadbalancer:
retry:
enabled: false
config:
discovery:
# 开启配置中心服务发现支持
enabled: true
# 配置中心服务的id
service-id: config-service
# 那个文件夹,config模块resources/config下的那个文件夹
label: dev
# 读取后缀名称
profile: dev
# 配置文件名称
name: config
# fail-fast 如果访问配置中心失败,则停止启动服务
fail-fast: true
# retry 访问配置中心失败时的重试策略
retry:
initial-interval: 2000
# 最多重试次数
max-attempts: 6
# 最大重试间隔
max-interval: 4000
# 每次重试时间是之前的倍数
multiplier: 1.2
启动顺序,注册中心(eureka)、配置中心(config)、server、web、zuul(后面三个没有顺序)
8、添加一个common模块,用于各个模块的父模块,统一管理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 http://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.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xiaostudy</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> <name>common</name> <packaging>pom</packaging> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR3</spring-cloud.version> <eureka.user.name>admin</eureka.user.name> <eureka.user.password>xiaostudy.test</eureka.user.password> <springCloudTest.version>0.0.1-SNAPSHOT</springCloudTest.version> </properties> <dependencies> <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> <resources> <resource> <!--指定resources插件处理哪个目录下的资源文件--> <directory>src/main/resources</directory> <!--打开资源过滤功能--> <filtering>true</filtering> </resource> </resources> </build> </project>
配置中心config也可以修改成
<?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.xiaostudy</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../common/pom.xml</relativePath> </parent> <groupId>com.xiaostudy</groupId> <artifactId>config</artifactId> <version>${springCloudTest.version}</version> <name>config</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project>
server也可以修改成
<?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.xiaostudy</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../common/pom.xml</relativePath> </parent> <groupId>com.xiaostudy</groupId> <artifactId>server</artifactId> <version>${springCloudTest.version}</version> <name>server</name> <description>Demo project for Spring Boot</description> <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-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
网关zuul也可以修改成
<?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.xiaostudy</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../common/pom.xml</relativePath> </parent> <groupId>com.xiaostudy</groupId> <artifactId>zuul</artifactId> <version>${springCloudTest.version}</version> <name>zuul</name> <description>Demo project for Spring Boot</description> <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-zuul</artifactId> </dependency> </dependencies> </project>
web也可以修改成
<?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.xiaostudy</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../common/pom.xml</relativePath> </parent> <groupId>com.xiaostudy</groupId> <artifactId>web</artifactId> <version>${springCloudTest.version}</version> <name>web</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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-config-dependencies</artifactId> <version>2.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> <dependency> <groupId>com.xiaostudy</groupId> <artifactId>server</artifactId> <version>${springCloudTest.version}</version> </dependency> </dependencies> </project>
后续在此基础上完善
把feign从server从抽取出来