SpringCloud学习笔记(一)
程序员文章站
2022-03-20 12:19:38
...
声明:关于springcloud微服务的整套学习笔记都是基于开源项目AG-Admin的学习而整理的,本次文章中所涉及的包名或人名都沿用开源项目里面的名称,也算是给老A推广推广他的项目吧。项目地址:https://gitee.com/geek_qi/ace-security。感谢老A能够带头去搭建一个完整的spingcloud的微服务架构,这对于想要学习springcloud 的新手来说是莫大的福利。鉴于自己对这个项目的理解和学习,打算写一系列文章记录下来,方便自己以后查找使用,同时也能给需要的人提供一些参考。
首先大家需要对Eureka,Zuul、Ribbon等一些词语及它所对应的功能有一些了解,关于它们的作用我这里就不介绍了,下面主要想讲一**册中心和网关的搭建。
一、搭建注册中心
首先新建一个父工程,引入springcloud相关的依赖:
<?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>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<groupId>com.github.wxiaoqi.learning</groupId>
<artifactId>ag-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
<scope>provided</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.33</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.11</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
父工程的名称叫ag-parent,接下来我们创建一个子工程取名ag-eureka,也就是注册中心,配置文件如下:
spring:
application:
name: ag-eureka
eureka:
client:
register-with-eureka: false #不作为客户端注册到服务中心
fetch-registry: false #发现注册中心(否)
server:
port: 8761
依赖文件:
<?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>ag-parent</artifactId>
<groupId>com.github.wxiaoqi.learning</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ag-eureka</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>
</dependencies>
</project>
距离注册中心搭建完成还差最后一步:
新建一个包,在包中创建EurekaBootstrap启动类,内容如下:
package com.github.wxiaoqi.ag.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* <pre>
* author : lpf
* time : 2017/10/2911:14
* desc : 输入描述
* </pre>
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaBootstrap {
public static void main(String[] args) {
SpringApplication.run(EurekaBootstrap.class,args);
}
}
至此,注册中心就搭建完成了,怎么样?是不是觉得简单又快捷呢?启动后访问8761端口看到如下页面就说明注册中心搭建成功了。二、搭建网关
在ag-parent模块下面我们新建一个module叫ag-zuul,网关的搭建也分三部分:配置文件、依赖文件和启动类。
配置文件如下:
spring:
application:
name: ag-gate
zuul:
prefix: /api #拦截所有以api开头的请求都经过zuul路由
routes: #todo 路由定义
user-center:
path: /uc/**
serviceId: user-center
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
server:
port: 8765
依赖文件:
<?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>ag-parent</artifactId>
<groupId>com.github.wxiaoqi.learning</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ag-zuul</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
新建一个启动类叫APIGateBootstrap,代码如下:
package com.github.wxiaoqi.gate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
/**
* <pre>
* author : lpf
* time : 2017/10/2922:08
* desc : 输入描述
* </pre>
*/
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
@EnableDiscoveryClient
public class APIGateBootstrap {
public static void main(String[] args) {
SpringApplication.run(APIGateBootstrap.class,args);
}
}
这个时候我们启动网关微服务,它 就会注册到eureka注册中心上了,如下图:
下一篇我们再介绍配置微服务和用户微服务的搭建和访问。