版本对应:https://start.spring.io/actuator/info
Cloud官网:Spring Cloud
在官网可以查看 当前Cloud 推荐的Boot版本:
JDK版本改为8:
注解激活生效:新版的lonbok好像需要勾选:
字符编码集UTF-8:
父模块:
- "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.0modelVersion>
- <groupId>org.examplegroupId>
- <artifactId>alibaba2023artifactId>
- <version>1.0-SNAPSHOTversion>
- <packaging>pompackaging>
-
- <modules>
- <module>demomodule>
- modules>
-
- <properties>
- <maven.compiler.source>8maven.compiler.source>
- <maven.compiler.target>8maven.compiler.target>
- <spring-boot.version>2.3.12.RELEASEspring-boot.version>
- <spring-cloud-alibaba.version>2.2.10-RC1spring-cloud-alibaba.version>
- <spring-cloud.version>Hoxton.SR12spring-cloud.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>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-alibaba-dependenciesartifactId>
- <version>${spring-cloud-alibaba.version}version>
- <type>pomtype>
- <scope>importscope>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- <version>${spring-boot.version}version>
- dependency>
- dependencies>
- dependencyManagement>
- project>
子模块:
- "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.0modelVersion>
- <groupId>com.examplegroupId>
- <artifactId>demoartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <packaging>jarpackaging>
- <name>demoname>
- <description>Demo project for Spring Bootdescription>
-
- <parent>
- <artifactId>alibaba2023artifactId>
- <groupId>org.examplegroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
-
- <properties>
- <java.version>1.8java.version>
- properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-actuatorartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- <exclusions>
- <exclusion>
- <groupId>org.junit.vintagegroupId>
- <artifactId>junit-vintage-engineartifactId>
- exclusion>
- exclusions>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <optional>trueoptional>
- dependency>
-
-
- <dependency>
- <groupId>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
- dependency>
-
- <dependency>
- <groupId>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
- dependency>
-
- <dependency>
- <groupId>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-circuitbreaker-sentinelartifactId>
- dependency>
-
- <dependency>
- <groupId>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
- <version>2021.0.1.0version>
- dependency>
-
- <dependency>
- <groupId>com.alibaba.cspgroupId>
- <artifactId>sentinel-datasource-nacosartifactId>
- dependency>
-
- <dependency>
- <groupId>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-alibaba-sentinel-gatewayartifactId>
- dependency>
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <version>3.8.1version>
- <configuration>
- <source>1.8source>
- <target>1.8target>
- <encoding>UTF-8encoding>
- configuration>
- plugin>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- <version>${spring-boot.version}version>
- <configuration>
- <mainClass>com.example.demo.DemoApplicationmainClass>
- <skip>trueskip>
- configuration>
- <executions>
- <execution>
- <id>repackageid>
- <goals>
- <goal>repackagegoal>
- goals>
- execution>
- executions>
- plugin>
- plugins>
- build>
-
- project>
- #
-
-
UTF-8 -
1.8 -
1.8 -
4.12 -
1.2.17 -
1.16.18 -
5.1.47 -
1.1.16 -
1.3.0 -
-
-
-
- # 引用时
- #
标签 -
-
mysql -
mysql-connector-java -
${mysql.version} -
-
-
com.alibaba -
druid -
${druid.version} -
-
-
-
- #
-
com.zbx.springcloud //创建工程时候的完整包名 -
cloud-api-commons //工程的名字 (如下图) -
${project.version} //当前自己pom里面定义的版本号 (固定这样写就可以了)
使用 Run Dashboard (批量 Run):
所有的微服务,一定要有自己的微服务名称,在配置文件里面进行配置
什么是服务治理:
- 在传统rpc远程调用中,服务与服务依赖关系,管理比较复杂,所以需要使用服务治理,管理服务与服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册。
在服务注册与发现中,有一个注册中心,当服务器启动的时候,会把当前自已服务器的信息 比如 服务地址通讯地址等以别名方式注册到注册中心上。另一方(消费者|服务提供者),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后在实现本地rpc调用远程。
Spring Cloud Commons 提供的抽象
最早的时候服务发现注册都是通过DiscoveryClient来实现的,随着版本变迁把DiscoveryClient服务注册抽离出来变成了ServiceRegistry抽象,专门负责服务注册,DiscoveryClient专门负责服务发现。还提供了负载均衡的发现LoadBalancerClient抽象。
DiscoveryClient通过@EnableDiscoveryClient的方式进行启用。
- 从Spring Cloud Edgware开始,
@EnableDiscoveryClient
或@EnableEurekaClient
可省略。只需加上相关依赖,并进行相应配置,即可将微服务注册到服务发现组件上。@EnableDiscoveryClient和@EnableEurekaClient:
- 共同点:都是能够让注册中心能够发现,扫描到改服务。
- 不同点:
@
EnableEurekaClient只适用于Eureka作为注册中心,@EnableDiscoveryClient
可以是其他注册中心。
使用:
- @Resource
- private DiscoveryClient discoveryClient;
-
- @GetMapping(value = "/payment/discovery")
- public Object discovery() {
- List
services = discoveryClient.getServices(); - for (String element : services) {
- log.info("*****element: " + element);
- }
-
- List
instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE"); - for (ServiceInstance instance : instances) {
- log.info(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
- }
-
- return this.discoveryClient;
Ribbon:
主要作用:负载均衡 + RestTemplate调用
负载均衡设置:
配置方式一:配置类方式
配置方式二:配置文件
配置方式三:自定义实现
OpenFeign:
5.