springcloud的介绍与Eureka的介绍

Springcloud简介

简介

    Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring Cloud并没有重复制造轮子,它只是将目前各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。

详细介绍: https://baike.so.com/doc/25751000-26884657.html

配套参考资料:
https://projects.spring.io/spring-cloud/ springcloud项目官方主页

https://springcloud.cc/ springcloud中文网 有很详细的翻译文档

http://springcloud.cn/ springcloud中文论坛

在这里插入图片描述
Springcloud版本pom文件生成可借助网站:
https://start.spring.io/

原有的单体项目最终会被演化成下面
在这里插入图片描述
这样的架构解决了单体项目几点问题:
1、zuul网关解决了服务调用安全性的问题
2、服务注册与发现(注册中心)eureka解决了各层服务耦合问题,它是微服务架构的核心,有它才能将单体项目拆解成微服务架构
3、Eureka集群解决了微服务中,注册中心宕机产生的问题
4、Ribbon负载均衡及Feign消费者调用服务,减小了各微服务服务器的访问压力,默认采用了经典的轮询机制
5、熔断器Hystrix解决了,微服务架构中服务器雪崩现象
6、服务监控(单机Dashboard与集群turbine),方便运维人员查看微服务架构项目运行时,各个服务器的运行状态
7、服务配置中心(springcloud config),用来通过github统一管理各个微服务的配置文件(yml文件)

入门案例

最简单的微服务架构会有四个工程

父工程:microservice
通用模块(M):microservice-common
服务提供者(C):microservice-student-provider-1001
服务消费者(C):microservice-student-consumer-80

微服务架构注意点:

  1. springboot、springcloud版本在父工程定义;
  2. 由于通用模块无需操作数据库,springboot启动默认会读取数据库,所以得添加以下注解 @SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class});
  3. 分布式jpa需要在启动类上添加@EntityScan("com.tang.*.*");
  4. 消费者需要添加配置类获取org.springframework.web.client.RestTemplate,springcloud底层是通过RestTemplate来调用提供者的服务的。

创建父工程microservice

     父工程是一个maven项目,一般创建方式即可,父工程的主要用途是锁定pom依赖包版本。由于springcloud2X停止更新,这里我们采用稳定的低版本,配套的springboot版本为1x版本。
在这里插入图片描述
Pom.xml配置如下
pom.xml

<?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.tang</groupId>
  <artifactId>microservice</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>microservice</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <druid.version>1.1.10</druid.version>
  </properties>
  <!--锁定pom依赖jar包但是并不实际引入-->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Edgware.SR4</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>1.5.13.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--  连接池  -->
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>${druid.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

创建通用模块microservice-common
     通用模块主要存放实体类、工具包等被整个微服务框架所使用的代码。创建一个简单的springboot模块即可。相关代码如下
在这里插入图片描述
在这里插入图片描述

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.tang</groupId>
    <artifactId>microservice</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservice-common</artifactId>


<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>


MicroserviceCommonApplication.java

package com.tang.microservicecommon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class MicroserviceCommonApplication {

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

}

Student.java

package com.tang.microservicecommon.entity;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name="t_springcloud_student")
public class Student implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Integer id;

    @Column(length=50)
    private String name;

    @Column(length=50)
    private String grade;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGrade() {
        return grade;
    }
    public void setGrade(String grade) {
        this.grade = grade;
    }



}

创建服务提供者microservice-student-provider-1001

    创建一个简单的springboot模块,这里服务提供者需要操作数据库并且被浏览器所访问,固需要添加相关配置如下

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.tang</groupId>
        <artifactId>microservice</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>microservice-student-provider-1001</artifactId>


    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.tang</groupId>
            <artifactId>microservice-common</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>
        <!--  修改后立即生效,热部署  -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.yml

server:
  port: 1001
  context-path: /
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/t243?useUnicode=true&characterEncoding=utf8
    username: root
    password:
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

MicroserviceStudentProvider1001Application.java

package com.tang.microservicestudentprovider1001;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;

@EntityScan("com.tang.*.*")
@SpringBootApplication
public class MicroserviceStudentProvider1001Application {

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

}

StudentRepository.java

package com.tang.microservicestudentprovider1001.repository;

import com.tang.microservicecommon.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

/**
 * @author CandyTang
 * @create  2020-12-12 16:33
 */
public interface StudentRepository extends JpaRepository<Student,Integer>,JpaSpecificationExecutor<Student> {
}

StudentService.java

package com.tang.microservicestudentprovider1001.service;

import com.tang.microservicecommon.entity.Student;

import java.util.List;

/**
 * @author CandyTang
 * @create  2020-12-12 16:42
 */
@Repository
public interface StudentService {
    public void save(Student student);

    public Student findById(Integer id);

    public List<Student> list();

    public void delete(Integer id);
}

StudentServiceImpl.java

package com.tang.microservicestudentprovider1001.service.impl;

import com.tang.microservicecommon.entity.Student;
import com.tang.microservicestudentprovider1001.repository.StudentRepository;
import com.tang.microservicestudentprovider1001.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author CandyTang
 * @create  2020-12-12 16:43
 */
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentRepository studentRepository;

    @Override
    public void save(Student student) {
        studentRepository.save(student);
    }

    @Override
    public Student findById(Integer id) {
        return studentRepository.findOne(id);
    }

    @Override
    public List<Student> list() {
        return studentRepository.findAll();
    }

    @Override
    public void delete(Integer id) {
        studentRepository.delete(id);
    }
}

StudentProviderController.java

package com.tang.microservicestudentprovider1001.controller;

import com.tang.microservicecommon.entity.Student;
import com.tang.microservicestudentprovider1001.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author CandyTang
 * @create  2020-12-12 16:45
 */
@RestController
@RequestMapping("/student")
public class StudentProviderController {
    @Autowired
    private StudentService studentService;

    @PostMapping(value="/save")
    public boolean save(Student student){
        try{
            studentService.save(student);
            return true;
        }catch(Exception e){
            return false;
        }
    }

    @GetMapping(value="/list")
    public List<Student> list(){
        return studentService.list();
    }

    @GetMapping(value="/get/{id}")
    public Student get(@PathVariable("id") Integer id){
        return studentService.findById(id);
    }

    @GetMapping(value="/delete/{id}")
    public boolean delete(@PathVariable("id") Integer id){
        try{
            studentService.delete(id);
            return true;
        }catch(Exception e){
            return false;
        }
    }
}

创建服务消费者microservice-student-consumer-80

    服务消费者主要是通过restful api来调用提供者的接口,固不需要不需要操作数据库,相关配置如下

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.tang</groupId>
        <artifactId>microservice</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>microservice-student-consumer-80</artifactId>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.tang</groupId>
            <artifactId>microservice-common</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <!--  修改后立即生效,热部署  -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.yml

server:
  port: 80
  context-path: /

MicroserviceStudentConsumer80Application.java

package com.tang.microservicestudentconsumer80;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class MicroserviceStudentConsumer80Application {

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

}

SpringCloudConfig.java

package com.tang.microservicestudentconsumer80.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @author CandyTang
 * @create  2020-12-12 16:55
 */
@Configuration
public class SpringCloudConfig {
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

StudentConsumerController.java

package com.tang.microservicestudentconsumer80.controller;

import com.tang.microservicecommon.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author CandyTang
 * @create  2020-12-12 16:56
 */
@RestController
@RequestMapping("/student")
public class StudentConsumerController {


    private final static String SERVER_IP_PORT = "http://localhost:1001";

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping(value="/save")
    private boolean save(Student student){
        return restTemplate.postForObject(SERVER_IP_PORT+"/student/save", student, Boolean.class);
    }

    @GetMapping(value="/list")
    public List<Student> list(){
        return restTemplate.getForObject(SERVER_IP_PORT+"/student/list", List.class);
    }

    @GetMapping(value="/get/{id}")
    public Student get(@PathVariable("id") Integer id){
        return restTemplate.getForObject(SERVER_IP_PORT+"/student/get/"+id, Student.class);
    }

    @GetMapping(value="/delete/{id}")
    public boolean delete(@PathVariable("id") Integer id){
        try{
            restTemplate.getForObject(SERVER_IP_PORT+"/student/delete/"+id, Boolean.class);
            return true;
        }catch(Exception e){
            return false;
        }
    }

}

关于如何配置RunDashboard
https://www.cnblogs.com/yangtianle/p/8818255.html

成功结果图如下

http://localhost:1001/student/list
在这里插入图片描述

http://localhost/student/list
在这里插入图片描述

初识eureka

Eureka简介:

    Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。

    Eureka包含两个组件:Eureka Server和Eureka Client。

    Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。

    Eureka Client是一个java客户端,用于简化与Eureka Server的交互,客户端同时也就别一个内置的、使用轮询(round-robin)负载算法的负载均衡器。

    在应用启动后,将会向Eureka Server发送心跳,默认周期为30秒,如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个服务节点移除(默认90秒)。

    Eureka Server之间通过复制的方式完成数据的同步,Eureka还提供了客户端缓存机制,即使所有的Eureka Server都挂掉,客户端依然可以利用缓存中的信息消费其他服务的API。综上,Eureka通过心跳检查、客户端缓存等机制,确保了系统的高可用性、灵活性和可伸缩性。

    类似zookeeper,Eureka也是一个服务注册和发现组件,是SpringCloud的一个优秀子项目,不过比较坑的是,Eureka2版本已经停止更新了。但是Eureka1版本还是很稳定,功能足够用,所以还是有必要学习下。

    但是这里有几个常用的服务注册与发现组件比对;
在这里插入图片描述
服务注册与发现原理。
在这里插入图片描述

Eureka的使用

    前面说过eureka是c/s模式的 server服务端就是服务注册中心,其他的都是client客户端,服务端用来管理所有服务,客户端通过注册中心,来调用具体的服务;
我们先来搭建下服务端,也就是服务注册中心;

新建 module microservice-eureka-server-2001;
在pom文件中加入eureka-server依赖;
yml文件中添加Eureka服务端相关信息;
在启动类中添加@EnableEurekaServer注解;

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.tang</groupId>
        <artifactId>microservice</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>microservice-eureka-server-2001</artifactId>


    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--  修改后立即生效,热部署  -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.yml

server:
  port: 2001
  context-path: /
eureka:
  instance:
    hostname: localhost #eureka\u6CE8\u518C\u4E2D\u5FC3\u5B9E\u4F8B\u540D\u79F0
  client:
    register-with-eureka: false     #false \u7531\u4E8E\u8BE5\u5E94\u7528\u4E3A\u6CE8\u518C\u4E2D\u5FC3\uFF0C\u6240\u4EE5\u8BBE\u7F6E\u4E3Afalse,\u4EE3\u8868\u4E0D\u5411\u6CE8\u518C\u4E2D\u5FC3\u6CE8\u518C\u81EA\u5DF1\u3002
    fetch-registry: false     #false \u7531\u4E8E\u6CE8\u518C\u4E2D\u5FC3\u7684\u804C\u8D23\u5C31\u662F\u7EF4\u62A4\u670D\u52A1\u5B9E\u4F8B\uFF0C\u5B83\u5E76\u4E0D\u9700\u8981\u53BB\u68C0\u7D22\u670D\u52A1\uFF0C\u6240\u4EE5\u4E5F\u8BBE\u7F6E\u4E3Afalse
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #\u8BBE\u7F6E\u4E0EEureka\u6CE8\u518C\u4E2D\u5FC3\u4EA4\u4E92\u7684\u5730\u5740\uFF0C\u67E5\u8BE2\u670D\u52A1\u548C\u6CE8\u518C\u670D\u52A1\u7528\u5230

MicroserviceEurekaServer2001Application.java

package com.tang.microserviceeurekaserver2001;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class MicroserviceEurekaServer2001Application {

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

}

测试连接
http://localhost:2001/

搭建成功截图如下
在这里插入图片描述

向Eureka中注册服务提供者

    这里我们在原来的服务提供者项目 microservice-student-provider-1001 上面直接修改:

首先pom.xml修改,加上eureka客户端依赖

!--添加注册中心Eureka相关配置-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

然后application.yml上加上配置

ureka:
  instance:
    hostname: localhost  #eureka客户端主机实例名称
    appname: microservice-student  #客户端服务名
    instance-id: microservice-student:1001 #客户端实例名称
    prefer-ip-address: true #显示IP
  client:
    service-url:
      defaultZone: http://localhost:2001/eureka   #把服务注册到eureka注册中心

这里的defaultZone要和前面服务注册中心的暴露地址一致;

最后 启动类加上一个注解 @EnableEurekaClient

package com.tang.microservicestudentprovider1001;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EntityScan("com.javaxl.*.*")
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceStudentProvider1001Application {

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

}

然后我们测试下,先启动服务注册中心,再启动这个服务提供者;

然后运行:http://localhost:2001/
服务提供者成功注册截图如下
在这里插入图片描述

点击实例状态结果如下
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值