集群模式
-
项目结构
-
项目依赖管理(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> <modules> <module>springcloud-common</module> <module>springcloud-provider-8000</module> <module>springcloud-eureka-7000</module> <module>springcloud-eureka-7001</module> <module>springcloud-customer-80</module> <module>springcloud-provider-8001</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.10.RELEASE</version> </parent> <groupId>com.frank</groupId> <artifactId>SpringCloud</artifactId> <version>v1.0</version> <name>SpringCloud</name> <packaging>pom</packaging> <description>第一次搭建SpringCloud</description> <properties> <spring-cloud.version>Hoxton.SR11</spring-cloud.version> <mybatis-plus.version>3.3.2</mybatis-plus.version> <java.version>1.8</java.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>3.4.2</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
-
构建模块
公共模块
-
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>SpringCloud</artifactId> <groupId>com.frank</groupId> <version>v1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-common</artifactId> <dependencies> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> </dependencies> </project>
-
代码
package com.frank.common.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Getter; import lombok.Setter; import lombok.ToString; import java.io.Serializable; @TableName("user") @Setter @Getter @ToString public class User implements Serializable { @TableId(type = IdType.ASSIGN_ID) private Long id; private String name; private Integer age; private Integer sex; }
Eureka模块
-
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>SpringCloud</artifactId> <groupId>com.frank</groupId> <version>v1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-eureka-7000</artifactId> <dependencies> <!--服务端所需的Eureka依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> </project>
-
application.yml
首先修改C:\Windows\System32\drivers\etc中host文件,添加# eureka:Eureka集群要多台机器,所以我们只能将127.0.0.1配置多个地址名称,模拟多台机器 127.0.0.1 eureka-server-7000 127.0.0.1 eureka-server-7001
Eureka7000模块
```yml
server:
port: 7000spring: application: name: Eureka eureka: instance: # 注册中心名称(host中配置的名称) hostname: eureka-server-7000 client: # 是否向注册中心注册自己 register-with-eureka: false # false表示自己就是注册中心,自己的职责就是维护服务实例,并不需要检索服务实例 fetch-registry: false # 服务地址 service-url: # 与另一个Eureka7001注册中心相互守望 defaultZone: http://eureka-server-7001:7001/eureka/ ``` **Eureka7001模块** ```yml server: port: 7001 spring: application: name: Eureka eureka: instance: # 注册中心名称((host中配置的名称)) hostname: eureka-server-7001 client: # 是否向注册中心注册自己 register-with-eureka: false # false表示自己就是注册中心,自己的职责就是维护服务实例,并不需要检索服务实例 fetch-registry: false # 服务地址 service-url: # 与Eureka7000注册中心相互守望 defaultZone: http://eureka-server-7000:7000/eureka/ ```
-
主程序
package com.frank.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication // 开启注册中心配置 @EnableEurekaServer public class Eureka7000Application { public static void main(String[] args) { SpringApplication.run(Eureka7000Application.class, args); } }
生产者模块
-
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>SpringCloud</artifactId> <groupId>com.frank</groupId> <version>v1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-provider-8000</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>3.0.3</version> <exclusions> <exclusion> <artifactId>servlet-api</artifactId> <groupId>javax.servlet</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.frank</groupId> <artifactId>springcloud-common</artifactId> <version>v1.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <!-- maven 默认不会编译src/main/java目录下的配置文件,需要手动指定--> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
-
application.yml
Provider8000server: port: 8000 spring: application: # 服务名称 name: PROVIDER datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql:///springcloud?useUnicode=true&characterEncoding=utf8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&tinyInt1isBit=true&useAffectedRows=true&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true username: root password: root mybatis-plus: mapper-locations: classpath*:com/frank/provider/mapper/xml/*.xml type-aliases-package: com.frank.common.entity configuration: map-underscore-to-camel-case: true eureka: client: # 向注册中心注册自己 register-with-eureka: true # true代表自己是服务,不是注册中心 fetch-registry: true service-url: # 注册中心的地址 defaultZone: http://eureka-server-7000:7000/eureka/,http://eureka-server-7001:7001/eureka/
Provider8001
server: port: 8001 spring: application: # 服务名称 name: PROVIDER datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql:///springcloud?useUnicode=true&characterEncoding=utf8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&tinyInt1isBit=true&useAffectedRows=true&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true username: root password: root mybatis-plus: mapper-locations: classpath*:com/frank/provider/mapper/xml/*.xml type-aliases-package: com.frank.common.entity configuration: map-underscore-to-camel-case: true eureka: client: # 向注册中心注册自己 register-with-eureka: true # true代表自己是服务,不是注册中心 fetch-registry: true service-url: # 注册中心的地址 defaultZone: http://eureka-server-7000:7000/eureka/,http://eureka-server-7001:7001/eureka/
-
主程序
package com.frank.provider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication // 开启服务注册 @EnableEurekaClient public class Provider8001Application { public static void main(String[] args) { SpringApplication.run(Provider8001Application.class, args); } }
-
UserController.java
package com.frank.provider.controller; import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator; import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator; import com.frank.common.entity.User; import com.frank.provider.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/list") public List<User> all() { List<User> all = userService.all(); User user = new User(); // 标记:8001代表服务是8001端口生产者响应的 // 作用:为后面的负载均衡做标记 user.setId(8001L); all.add(user); return all; } @PostMapping("/add") public void add(@RequestBody User user) { IdentifierGenerator identifierGenerator = new DefaultIdentifierGenerator(); user.setId((Long) identifierGenerator.nextId(user)); userService.add(user); } }
消费者模块
-
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>SpringCloud</artifactId> <groupId>com.frank</groupId> <version>v1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-customer-80</artifactId> <dependencies> <dependency> <groupId>com.frank</groupId> <artifactId>springcloud-common</artifactId> <exclusions> <exclusion> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </exclusion> </exclusions> <version>v1.0</version> </dependency> <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> </dependencies> </project>
-
application.yml
server: port: 80 spring: application: name: customer eureka: client: # 向注册中心注册自己 register-with-eureka: true # true代表自己不是注册中心 fetch-registry: true service-url: # 注册中心地址 defaultZone: http://eureka-server-7000:7000/eureka/,http://eureka-server-7001:7001/eureka/
-
主启动类
package com.frank.customer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication // 开启服务注册 @EnableEurekaClient public class CustomerApplication { public static void main(String[] args) { SpringApplication.run(CustomerApplication.class, args); } }
-
BeanConfiguration.java
package com.frank.customer.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class BeanConfiguration { @Bean // 开启RestTemplate的负载均衡功能 @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
-
UserController.java
package com.frank.customer.controller; import com.frank.common.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.List; @RestController @RequestMapping("/user") public class UserController { // PROVIDER是要调用服务名称 private final String SERVICE_NAME = "http://PROVIDER"; @Autowired private RestTemplate restTemplate; @GetMapping("/list") public List<User> all() { // 调用注册中心里面的服务 return restTemplate.getForObject(SERVICE_NAME + "/user/list", List.class); } }
-
测试
守望相助
负载均衡(一次8000,一次8001)
- 项目至此,搭建测试成功!
完善信息提示
-
服务模块中添加以下依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
-
application.yml
eureka: instance: # 修改Eureka页面显示的服务名称 instance-id: true # 开启id地址提示 prefer-ip-address: true
-
测试
-
至此,测试成功!
详细总结(只需要看这个)
- 注册中心
- 依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
- 主启动类加上@EnableEurekaServer
- yml配置
spring: application: # 服务名称 name: Eureka eureka: client: # 是否向注册中心注册自己 register-with-eureka: false # 是否在注册中心检索服务 fetch-registry: false # 服务地址 service-url: # 单机版 defaultZone: http://127.0.0.1:7000/eureka/
- 依赖
- 服务
- 依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
- 主启动类加上@EnableEurekaClient
- yml配置
spring: application: # 服务名称 name: Provider eureka: client: # 向注册中心注册自己 register-with-eureka: true # 是否检索服务 fetch-registry: true service-url: # 注册中心的地址 defaultZone: http://127.0.0.1:7000/eureka/ instance: # 制定服务注册名,默认(主机名+服务名+端口号) instance-id: 127.0.0.1:8000 # 鼠标指向注册名左下角是否显示ip地址 prefer-ip-address: true
- 依赖