SpringCloud环境配置以及搭建

一、环境的搭建——服务注册中心

创建聚合父工程

 <!-- JDK的版本管理  -->
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    
    <!-- SpringBoot的依赖版本管理  -->
	<parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.7.2-SNAPSHOT</version>
    </parent>

    <!-- SpringCloud的依赖版本管理  -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2021.0.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

创建子工程——服务注册中心server1

  <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>3.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

配置文件

spring:
  application:
    name: server1
server:
  port: 8080
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://127.0.0.1:${server.port}/eureka/

启动类

@SpringBootApplication
@EnableEurekaServer
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

二、搭建环境——服务的提供者

1、添加依赖

<?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>sk-springcloud</artifactId>
        <groupId>com.buba</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
​
    <artifactId>client1</artifactId>
​
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
​
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>3.1.3</version>
        </dependency>
​
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
​
    </dependencies>
​
</project>

2、配置文件

spring:
  application:
    name: client1
  datasource:
    druid:
      username: root
      password: 123456
      url: jdbc:mysql://127.0.0.1:3306/mydb?serverTimezone=Asia/Shanghai
      driver-class-name: com.mysql.cj.jdbc.Driver
server:
  port: 7001
eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8080/eureka/
​
mybatis:
  mapper-locations: classpath:mappers/*.xml

3、启动类

@SpringBootApplication
//@EnableEurekaClient
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}
​

4、集成spring+mybatis+mysql

 <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.29</version>
        </dependency>
​
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.8</version>
        </dependency>
​
        <dependency>
            <groupId>com.buba</groupId>
            <artifactId>Common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

5、配置文件

mybatis:
  mapper-locations: classpath:mappers/*.xml
  
  
  spring:
    datasource:
        druid:
            username: root
            password: 123456
            url: jdbc:mysql://127.0.0.1:3306/mydb?serverTimezone=Asia/Shanghai
            driver-class-name: com.mysql.cj.jdbc.Driver

6、写逻辑

resources/mappers

mappers

service

controller

集成工共模块 common

三、微服务之间调用

1、单点调用及其原理  

 @Bean
    public RestTemplate restTemplate(){
       return new RestTemplate();
    }
    @GetMapping("/findUserById")
    public ResponseEntity<User> findUserById(){
        List<ServiceInstance> list = discoveryClient.getInstances("client1");
        if(list.isEmpty()){
            return null;
        }
        ServiceInstance instanceInfo = list.get(0);
        String instanceId = instanceInfo.getInstanceId();
​
        String url = instanceId+"/user/selectUserById?id=2";
​
        ResponseEntity<User> reps = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity(null,null), new ParameterizedTypeReference<User>() {
        });
        return reps;
    }

2、负载均衡调用

方式1:

 @GetMapping("/findUserById2")
    public ResponseEntity<User> findUserById2(){
​
        ServiceInstance serviceInstance = loadBalanced.choose("client1");
​
        String instanceId = serviceInstance.getInstanceId();
​
        String url = instanceId+"/user/selectUserById?id=2";
​
        ResponseEntity<User> reps = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity(null,null), new ParameterizedTypeReference<User>() {
        });
        return reps;
    }

方式2:

 @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
       return new RestTemplate();
    }
    @GetMapping("/findUserById3")
    public ResponseEntity<User> findUserById3(){
​
        ResponseEntity<User> reps = restTemplate.exchange("http://client1/user/selectUserById?id=2", HttpMethod.GET, new HttpEntity(null,null), new ParameterizedTypeReference<User>() {
        });
        return reps;
    }
 

四、服务注册中心的配置详解

配置项默认状态作用
eureka.server.enable-self-preservationfalse关闭注册中心的保护机制,Eureka 会统计15分钟之内心跳失败的比例低于85%将会触发保护机制,不剔除服务提供者,如果关闭服务注册中心将不可用的实例正确剔除
eureka.server.eviction-interval-timer-in-ms60000# Eureka服务器清理无效节点的时间间隔,单位:毫秒,缺省:60000,即60秒

五、服务提供者、消费者 配置详解

 

配置项默认状态作用
eureka.instance.prefer-ip-addressfalse使用使用IP地址的形式来定义注册中心的地址
eureka.client.register-with-eurekatrue启动服务注册
eureka.client.eureka-server-connect-timeout-seconds5连接 Eureka Server 的超时时间,单位:秒
eureka.client.eureka-server-read-timeout-seconds8读取 Eureka Server 信息的超时时间,单位:秒
eureka.client.filter-only-up-instancestrue获取实例时是否过滤,只保留UP状态的实例
eureka.instance.ip-addressIP地址
eureka.instance.hostname设置当前实例的主机名称
eureka.instance.appname服务名,默认取 spring.application.name 配置值,如果没有则为 unknown
eureka.instance.lease-renewal-interval-in-seconds30定义服务续约任务(心跳)的调用间隔,单位:秒
eureka.instance.lease-expiration-duration-in-seconds90定义服务失效的时间,单位:秒
eureka.client.service-url.指定服务注册中心地址,类型为 HashMap,并设置有一组默认值,默认的Key为 defaultZone;默认的Value为 http://localhost:8761/eureka ,如果服务注册中心为高可用集群时,多个注册中心地址以逗号分隔。 如果服务注册中心加入了安全验证,这里配置的地址格式为: http://username:password@localhost:8761/eureka 其中 <username> 为安全校验的用户名;<password> 为该用户的密码
eureka.client.fetch-registerytrue检索服务
eureka.client.registery-fetch-interval-seconds30客户端拉取服务注册信息间隔,单位:秒,缺省:30
eureka.client.health-check.enabled = true# 是否启用客户端健康检查
eureka.client.eureka-server-read-timeout-seconds = 8# 从Eureka服务器读取信息的超时时间,单位:秒,缺省:8
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

白鱼塘

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

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

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

打赏作者

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

抵扣说明:

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

余额充值