[TOC]
# **1. 创建聚合工程 springcloud-parent-test**
> 创建父工程用于版本管理
~~~
<?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>
<groupId>com.tuna</groupId>
<artifactId>springcloud-parent-test</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>provider</module>
</modules>
<properties>
<spring-boot.version>2.3.4.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.3.RELEASE</spring-cloud-alibaba.version>
</properties>
<!-- 依赖声明 -->
<dependencyManagement>
<dependencies>
<!-- SpringCloud 微服务 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringCloud Alibaba 微服务 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringBoot 依赖配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
~~~
# **2. 服务提供者整合服务发现**
* springboot也可以直接结合nacos
## **2.1 创建工程provider**
![](https://img.kancloud.cn/d7/d2/d7d2d2aa049c6c93740dea6d6d1a0ce3_379x254.png)
## **2.2 pom**
> 1. 注意`spring-cloud-context`依赖,引入bootstrap.xml才会生效
> 2. 引入服务发现依赖
~~~
<?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-parent-test</artifactId>
<groupId>com.tuna</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>provider</artifactId>
<dependencies>
<!--springboot web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--需要引入该jar才能使bootstrap配置文件生效-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
<!--服务发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
~~~
## 2.3 启动类
~~~
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
~~~
## 2.4 controller
~~~
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("")
public Object say(){
return "hello cloud - provider";
}
}
~~~
## 2.5 bootstrap.yml
~~~
## Spring
spring:
application:
# 应用名称
name: provider
profiles:
# 环境配置
active: dev
~~~
## 2.6 application-dev.yml
~~~
server:
port: 8000
spring:
application:
name: provider
cloud:
nacos:
discovery:
# 服务注册地址
server-addr: 192.168.56.10:8848
~~~
* 测试
![](https://img.kancloud.cn/af/cc/afcc1c70cd2c40d30ec5a891009b7f9a_554x149.png)
* provider注册到nacos
![](https://img.kancloud.cn/54/78/547845d188d56f845c9e5dfdb7f9aae1_923x277.png)
# 3. 消费者整合服务发现
## 3.1 整合Feign
cloud-api模块,统一fein接口调用,ribbon轮询和hettrix
![](https://img.kancloud.cn/64/2f/642f48839571c19e8052e1316f71521e_368x186.png)
**1. 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-parent-test</artifactId>
<groupId>com.tuna</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-api</artifactId>
<dependencies>
<!--feign 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
~~~
**2. 定义接口**
~~~
@FeignClient(contextId = "provider-test",value = "provider") //指定调用的微服务名称
public interface ProviderClientService {
@RequestMapping(value = "/test", method = RequestMethod.GET)
String say();
}
~~~
## 3.2 Consumer模块
![](https://img.kancloud.cn/8f/4c/8f4ca8051922c49c18b9af7203fbb552_392x237.png)
**1. pom需要引入api**
~~~
<dependency>
<groupId>com.tuna</groupId>
<artifactId>cloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
~~~
**2. 配置文件和provider一样**
**3. controller**
引入foreign
```
@Autowired
ProviderClientService providerClientService;
```
~~~
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
@Autowired
ProviderClientService providerClientService;
@GetMapping("")
public Object say(){
return "consumer::" + providerClientService.say();
}
}
~~~
**4. 启动类开启服务发现和foreign**
~~~
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "com.tuna.cloud.api")
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
~~~
测试:Consumer成功调用provider
![](https://img.kancloud.cn/d7/92/d7929b23fdb7acd99c7582649f9440a2_535x129.png)
# 动态修改名称空间
![](https://img.kancloud.cn/21/ca/21ca88c74aa72e6a67c15e213033edc1_1185x318.png)
添加到dev名称空间,修改配置保存
![](https://img.kancloud.cn/1f/98/1f98c6eea792f739b50dbda251ad6d31_895x417.png)
自动更新
![](https://img.kancloud.cn/b9/55/b955641a2d8252946a62ed245e04c0b7_867x319.png)
- springcloud
- springcloud的作用
- springboot服务提供者和消费者
- Eureka
- ribbon
- Feign
- feign在微服务中的使用
- feign充当http请求工具
- Hystrix 熔断器
- Zuul 路由网关
- Spring Cloud Config 分布式配置中心
- config介绍与配置
- Spring Cloud Config 配置实战
- Spring Cloud Bus
- gateway
- 概念讲解
- 实例
- GateWay
- 统一日志追踪
- 分布式锁
- 1.redis
- springcloud Alibaba
- 1. Nacos
- 1.1 安装
- 1.2 特性
- 1.3 实例
- 1. 整合nacos服务发现
- 2. 整合nacos配置功能
- 1.4 生产部署方案
- 环境隔离
- 原理讲解
- 1. 服务发现
- 2. sentinel
- 3. Seata事务
- CAP理论
- 3.1 安装
- 分布式协议
- 4.熔断和降级
- springcloud与alibba
- oauth
- 1. abstract
- 2. oauth2 in micro-service
- 微服务框架付费
- SkyWalking
- 介绍与相关资料
- APM系统简单对比(zipkin,pinpoint和skywalking)
- server安装部署
- agent安装
- 日志清理
- 统一日志中心
- docker安装部署
- 安装部署
- elasticsearch 7.x
- logstash 7.x
- kibana 7.x
- ES索引管理
- 定时清理数据
- index Lifecycle Management
- 没数据排查思路
- ELK自身组件监控
- 多租户方案
- 慢查询sql
- 日志审计
- 开发
- 登录认证
- 链路追踪
- elk
- Filebeat
- Filebeat基础
- Filebeat安装部署
- 多行消息Multiline
- how Filebeat works
- Logstash
- 安装
- rpm安装
- docker安装Logstash
- grok调试
- Grok语法调试
- Grok常用表达式
- 配置中常见判断
- filter提取器
- elasticsearch
- 安装
- rpm安装
- docker安装es
- 使用
- 概念
- 基础
- 中文分词
- 统计
- 排序
- 倒排与正排索引
- 自定义dynamic
- 练习
- nested object
- 父子关系模型
- 高亮
- 搜索提示
- kibana
- 安装
- docker安装
- rpm安装
- 整合
- 收集日志
- 慢sql
- 日志审计s
- 云
- 分布式架构
- 分布式锁
- Redis实现
- redisson
- 熔断和降级