微服务之SpringCloud(九)

8.6 Sleuth使用

1.下载jar

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
    <version>2.2.6.RELEASE</version>
</dependency>

2.实现配置

server:
  port: 9091
spring:
  application:
    name: LxVoteApi #项目名,服务名
  cloud:
    nacos:
      discovery: #注册中心
        server-addr: 127.0.0.1:8848
    sentinel:
      transport:
        port: 8719   #信息采集的通信端口
        dashboard: 127.0.0.1:8080
    sleuth:
      sampler:
        probability: 1  #设置采样率
    zipkin:
      sender:
        type: web #设置日志收集的方式
      base-url: http://localhost:9411/
      service:
        name: LxVoteApi

3.运行测试

http://localhost:9411/

九、SpringCloud之配置中心

9.1 配置中心

配置中心,就是将一些可能会改变的配置,单独存储到一个独立的系统中,可以实现不停机更新

实现配置变更,可以立即推送到各个服务器,不需要重新发布系统

9.2 配置中心的解决方案

1.SpringCloud Config+Spring Cloud Bus

目前不推荐,麻烦

2.Alibaba Nacos

推荐,性能、稳定

3.携程Apollo

推荐

9.3 基于Nacos实现配置中心

Nacos的核心作用:

1.注册中心 实现服务治理

2.配置中心 实现配置动态化

3.实现服务管理

基于Nacos实现配置中心:

配套的各个版本

1.服务中依赖jar

 <!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-config -->
<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  <version>2.2.5.RELEASE</version>
 </dependency>

 根项目的依赖:

<!--  父级项目-->
  <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.3.2.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>
<!--  属性声明-->
<properties>
  <java.version>1.8</java.version>
  <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<!--  依赖-公共 -->
<dependencies>
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>
<!--  限定版本-->
<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>

2.实现配置

必须实现boostarp类型的配置文件

bootstrap.propertie或者bootstrap.yml

spring.cloud.nacos.config.server-addr=127.0.0.1:8848

3.实现代码

使用注解:

@RefreshScope //实时刷新最新的动态配置

@Value("${配置名称}") 获取指定配置的内容

4.在nacos控制器实现动态配置

在 Nacos Spring Cloud 中,dataId的完整格式如下:

${prefix}-${spring.profiles.active}.${file-extension}

  • prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置。
  • spring.profiles.active 即为当前环境对应的 profile,详情可以参考 Spring Boot文档注意:当spring.profiles.active为空时,对应的连接符 - 也将不存在,dataId 的拼接格式变成 ${prefix}.${file-extension}
  • file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension来配置。目前只支持 properties和yaml 类型。

5.运行测试

访问接口,观察配置的值

在Nacos控制器实现配置的变更,再次观察接口的值

会跟着一起改变

ps一定要注意各个模块的版本号

附录:

1.Spring Data JPA

是什么:

Spring Data JPA:Spring体系开源的操作数据库的框架,类似于Mybatis

为什么:

Spring Data JPA:

1.更加高效 封装了常用的操作,屏蔽了基础的SQL语句

2.简洁,操作数据库更简单(接口)

3.操作多样式,支持原生SQL语句、JPQL(面向对象查询语句)、方法名解析查询、ID操作等等

目前企业中有人使用

怎么用:

1.依赖jar

2.实现配置

server:
  port: 8081
spring:
  application:
    name: LxVoteServer #项目名,服务名
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db_vote211?serverTimezone=Asia/Shanghai
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
  jpa:
    database: MySQL
    show-sql: true
    hibernate:
      ddl-auto: updateyaml

3.编写代码

实体层:

@Data
@Entity //实体类的标记位
@Table(name = "t_osslog")//标记类对应的表名
@NoArgsConstructor
@AllArgsConstructor
public class OssLog {
    @Id //标记这个是主键
    @GeneratedValue(strategy = GenerationType.IDENTITY)//主键生成策略,自增
    private Integer id;
    private Integer type;//类型
    @Column(length = 30,nullable = false) //设置属性和表中的字段的对应内容
    private String bname;
    @Column(length = 50)
    private String oname;
    @Column(length = 300)
    private String preurl;
    private Date ctime;
    private Date etime;
}

持久层:

public interface OssLogDao extends JpaRepository<OssLog,Integer> {

}

Keyword

Sample

JPQL snippet

Distinct

findDistinctByLastnameAndFirstname

select distinct … where x.lastname = ?1 and x.firstname = ?2

And

findByLastnameAndFirstname

… where x.lastname = ?1 and x.firstname = ?2

Or

findByLastnameOrFirstname

… where x.lastname = ?1 or x.firstname = ?2

Is,Equals

findByFirstname,findByFirstnameIs,findByFirstnameEquals

… where x.firstname = ?1

Between

findByStartDateBetween

… where x.startDate between ?1 and ?2

LessThan

findByAgeLessThan

… where x.age < ?1

LessThanEqual

findByAgeLessThanEqual

… where x.age <= ?1

GreaterThan

findByAgeGreaterThan

… where x.age > ?1

GreaterThanEqual

findByAgeGreaterThanEqual

… where x.age >= ?1

After

findByStartDateAfter

… where x.startDate > ?1

Before

findByStartDateBefore

… where x.startDate < ?1

IsNull,Null

findByAge(Is)Null

… where x.age is null

IsNotNull,NotNull

findByAge(Is)NotNull

… where x.age not null

Like

findByFirstnameLike

… where x.firstname like ?1

NotLike

findByFirstnameNotLike

… where x.firstname not like ?1

StartingWith

findByFirstnameStartingWith

… where x.firstname like ?1(parameter bound with appended%)

EndingWith

findByFirstnameEndingWith

… where x.firstname like ?1(parameter bound with prepended%)

Containing

findByFirstnameContaining

… where x.firstname like ?1(parameter bound wrapped in%)

OrderBy

findByAgeOrderByLastnameDesc

… where x.age = ?1 order by x.lastname desc

Not

findByLastnameNot

… where x.lastname <> ?1

In

findByAgeIn(Collection<Age> ages)

… where x.age in ?1

NotIn

findByAgeNotIn(Collection<Age> ages)

… where x.age not in ?1

True

findByActiveTrue()

… where x.active = true

False

findByActiveFalse()

… where x.active = false

IgnoreCase

findByFirstnameIgnoreCase

… where UPPER(x.firstname) = UPPER(?1)

2.微服务中专业术语

服务治理:就是进行服务的自动化管理,其核心是服务的自动注册与发现。

服务注册:服务实例将自身服务信息注册到注册中心。

服务发现:服务实例通过注册中心,获取到注册到其中的服务实例的信息,通过这些信息去请求它们提供的服务。

服务剔除:服务注册中心将出问题的服务自动剔除到可用列表之外,使其不会被调用到。

REST(Representational State Transfer):这是一种HTTP调用的格式,更标准,更通用,无论哪种语言都支持http协议

RPC(Remote Promote Call):一种进程间通信方式。允许像调用本地服务一样调用远程服务。RPC框架的主要目标就是让远程服务调用更简单、透明。RPC框架负责屏蔽底层的传输方式、序列化方式和通信细节。开发人员在使用的时候只需要了解谁在什么位置提供了什么样的远程服务接口即可,并不需要关心底层通信细节和调用过程。

SOA(Service Oriented Ambiguity):是一个组件模型,它将应用程序的不同功能服务进行拆分,并通过这些服务之间定义良好的接口和协议联系起来。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cjy_行业报告

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值