微服务spring-cloud day1
参考:https://blog.csdn.net/moshowgame/article/details/80633969
git项目地址:https://gitee.com/zyf6303/spring-cloud-study
一.创建spring-cloud父工程
这里使用的工具是Spring Tool Suite 4
创建一个maven project
二.引入依赖
1.父工程pom依赖:
<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.springcloud.ploymer</groupId> <artifactId>spring-cloud-ploymer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.RELEASE</version> </parent> <properties> <!-- 指定编码 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- 指定jdk版本 --> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR3</spring-cloud.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> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.67</version> </dependency> <!-- 支持 @ConfigurationProperties 注解 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!-- swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version> </dependency> <!-- spring-data-jpa --> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> --> <!-- spring-boot-devtools依赖包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>compile</scope> <optional>true</optional> </dependency> <!-- spring热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.6.RELEASE</version> </dependency> <!-- Hutool超级工具类 http://hutool.mydoc.io/ --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.0.9</version> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> <modules> <module>spring-cloud-ploymer-center</module> <module>spring-cloud-ploymer-demo</module> <module>spring-cloud-ploymer-router</module> </modules> </project>
2.spring-cloud-ploymer-center pom依赖:
<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>com.springcloud.ploymer</groupId> <artifactId>spring-cloud-ploymer</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>spring-cloud-ploymer-center</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> </project>
3.spring-cloud-ploymer-router pom依赖:
<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>com.springcloud.ploymer</groupId> <artifactId>spring-cloud-ploymer</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>spring-cloud-ploymer-router</artifactId> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project>
4.spring-cloud-ploymer-demo pom依赖:
<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>com.springcloud.ploymer</groupId> <artifactId>spring-cloud-ploymer</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>spring-cloud-ploymer-demo</artifactId> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 服务中心暂时不启yong --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql --> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> </dependency> <!-- freemarker --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <!-- websocket --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> </dependencies> </project>
三.配置:
1.center:
server: port: 8080 spring: application: name: EUREKA-CENTER eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false serviceUrl: defaultZone: http://localhost:8080/eureka/
2.router:
server: port: 8081 spring: application: name: zuul eureka: client: serviceUrl: defaultZone: http://localhost:8080/eureka/ zuul: routes: apis1: path: /api1/** #serviceId: spring-cloud-study-demo serviceId: SPRING-CLOUD-PLOYMER-DEMO apis2: path: /api2/** url: http://127.0.0.1:8082/
3.demo
server: port: 8082 servlet: context-path: /demo tomcat: remote-ip-header: x-forward-for uri-encoding: UTF-8 max-threads: 10 background-processor-delay: 30 spring: http: encoding: force: true charset: UTF-8 application: name: spring-cloud-ploymer-demo freemarker: request-context-attribute: req #prefix: /templates/ suffix: .html content-type: text/html enabled: true cache: false charset: UTF-8 allow-request-override: false expose-request-attributes: true expose-session-attributes: true expose-spring-macro-helpers: true #template-loader-path: classpath:/templates/ eureka: client: serviceUrl: defaultZone: http://localhost:8080/eureka/ instance: ip-address: ture
四.启动类
1.center
@EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
2.router
@EnableZuulProxy @EnableEurekaClient @SpringBootApplication public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); } }
3.demo
@EnableEurekaClient @SpringBootApplication public class CheckCollectSysApplication { public static void main(String[] args) { SpringApplication.run(CheckCollectSysApplication.class,args); } }
五.编写测试接口
六.运行结果:
day1源码:https://gitee.com/zyf6303/spring-cloud-study/commit/9fa175cc8b0d31e45b02a37fb010a36ac2a1e87e