SpringCloud 学习笔记

一、搭建微服务环境

  1. 创建父模块(SpringCloud_demo)

    1.1 导入坐标 

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-logging</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.4</version>
        <scope>provided</scope>
    </dependency>
</dependencies>


<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>http://repo.spring.io/libs-snapshot-local</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>http://repo.spring.io/libs-milestone-local</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>http://repo.spring.io/libs-release-local</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>http://repo.spring.io/libs-snapshot-local</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>http://repo.spring.io/libs-milestone-local</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

  2. 创建子模块(product_server,order_server)

    2.1 引入子模块坐标

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.32</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>

    2.2 编写子模块application.yml 配置文件

server:
  port: 9001
spring:
  application:
    name: service-product
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/netdisk?useUnicode=true&characterEncoding=utf8
    username: root
    password:
  jpa:
    database: MySQL
    show-sql: true
    open-in-view: true

    2.3 编写子模块启动类

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

  3. 创建子模块注册机(eureka_server)

    3.1 导入坐标

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

    3.2  编写application.yml

server:
  port: 9000
# 配置 eureka server
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false # 是否将自己注册到注册中心
    fetch-registry: false # 是否要从eureka中获取信息
    # 配置暴露给Eureka Client的请求地址
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

    3.3 编写启动类

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

   4.  将服务提供者注册到eureka_server上

    4.1 导入坐标

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

    4.2 application.yml中添加Eureka的配置

# 配置 Eureka
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
      instance:
        prefer-ip-address: true # 使用ip地址注册

    4.3 启动类添加注解@EnableDiscoveryClient(可选)

@SpringBootApplication
@EntityScan("com/gfy/eureka/entity")
@EnableDiscoveryClient
public class ProductApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProductApplication.class, args);
    }
}

  5. 服务调用者

    5.1 编写启动类

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("{id}")
    public Product findById(@PathVariable Long id){
        Product product = restTemplate.getForObject("http://localhost:9001/product/1", Product.class);
        return product;
    }
}

    5.2 编写服务调用者(order_server) 的controller 

@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;

@GetMapping("{id}")
public Product findById(@PathVariable Long id){
    List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
    ServiceInstance serviceInstance = instances.get(0);
    Product product = restTemplate.getForObject("http://"+serviceInstance.getHost()+":"+ serviceInstance.getPort() +"/product/1", Product.class);
    return product;
}

 

===============================================

Eureka细节

在Eureka控制台显示服务IP

# 配置 Eureka
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
      instance:
        prefer-ip-address: true # 使用ip地址注册
        instance-ip: ${spring.cloud.client.ip-address}:${server.port}  #向注册中心中注入服务IP

Eureka 自我保护机制

统计所有服务接收率,若在阈值以下,则不会剔除注册的服务

server:
  enable-self-preservation: false # 自我保护
  eviction-interval-timer-in-ms: 4000 # 剔除服务间隔

 Ribbon 负载均衡策略

1. 服务调用

eureka内部继承了rbbon

  1.1 创建RestTemplate的时候,声明@LoadBalanced

  2.2 使用restTemplate调用远程微服务:不再需要拼接微服务的URL,以待请求的服务名替换IP地址

2. 原理:

  2.1 服务端负载均衡:ngnix F5

  com.netflix.loadbalancer包下 RoundRobinRule(轮询)、RandomRule(随机)、RetryRule(重试)、WeightedResponseTimeRule(权重)等策略

  2.2 修改负载策略

#修改ribbon负载均衡策略 服务名:ribbon:NFLoadBalancerRuleClassName:策略全类名
service-product:
 ribbon:
  NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

 

 

 
posted @ 2022-10-21 12:59  小肥糕  阅读(36)  评论(0)    收藏  举报