基于微服务进行开发,微服务是目前比较热门的架构方式,具有以下特点:
基于Spring Cloud Hoxton.SR8搭建。
<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.0modelVersion>
<groupId>com.fengzigroupId>
<artifactId>food-social-contact-parentartifactId>
<packaging>pompackaging>
<modules>
<module>ms-registrymodule>
modules>
<version>1.0-SNAPSHOTversion>
<properties>
<spring-boot-version>2.3.5.RELEASEspring-boot-version>
<spring-cloud-version>Hoxton.SR8spring-cloud-version>
<lombok-version>1.18.16lombok-version>
<commons-lang-version>3.11commons-lang-version>
<mybatis-starter-version>2.1.3mybatis-starter-version>
<mysql-version>8.0.22mysql-version>
<swagger-starter-version>2.1.5-RELEASEswagger-starter-version>
<hutool-version>5.4.7hutool-version>
<guava-version>20.0guava-version>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<springfox.version>3.0.0springfox.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot-version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud-version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>${lombok-version}version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
<version>${commons-lang-version}version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>${mybatis-starter-version}version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-boot-starterartifactId>
<version>${springfox.version}version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql-version}version>
dependency>
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>${hutool-version}version>
dependency>
<dependency>
<groupId>com.google.guavagroupId>
<artifactId>guavaartifactId>
<version>${guava-version}version>
dependency>
dependencies>
dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
pluginManagement>
build>
project>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
dependencies>
server:
port: 8080
spring:
application:
name: ms-registry
# 配置 Eureka Server 注册中心
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8080/eureka/
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-gatewayartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
dependencies>
server:
port: 80
spring:
application:
name: ms-gateway
cloud:
gateway:
discovery:
locator:
enabled: true # 开启配置注册中心进行路由功能
lower-case-service-id: true # 将服务名称转小写
routes:
- id: ms-diners # 这里的id是标识这个路由规则的唯一标识符
uri: lb://ms-diners #lb:负载均衡
predicates:
- Path=/hello/** #路径为 /hello/** 的请求转发到名为 ms-diners 的微服务
# 配置 Eureka Server 注册中心
eureka:
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
client:
service-url:
defaultZone: http://localhost:8080/eureka/
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
server:
port: 8081 # 端口
spring:
application:
name: ms-diners # 应用名
@RestController
@RequestMapping("hello")
public class HelloController {
@GetMapping
public String hello(String name) {
return "hello " + name;
}
}
访问localhost/hello?name=fengzi
因为网关端口80,80端口是浏览器默认端口,所以可以省略端口号
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-M2lWyR1H-1692857461427)(image-20230824114757524.png)]
public String hello(String name) {
return "hello " + name;
}
}
### 第五步:启动测试
访问[localhost/hello?name=fengzi](http://localhost/hello?name=fengzi)
> 因为网关端口80,80端口是浏览器默认端口,所以可以省略端口号
[外链图片转存中...(img-M2lWyR1H-1692857461427)]