springcloud简介

Springcloud简介

简介

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

 

原有的单体项目最终会被演化成下面

 

这样的架构解决了单体项目几点问题:

1、zuul网关解决了服务调用安全性的问题

2、服务注册与发现(注册中心)eureka解决了各层服务耦合问题,它是微服务架构的核心,有它才能将单体项目拆解成微服务架构

3、Eureka集群解决了微服务中,注册中心宕机产生的问题

4、Ribbon负载均衡及Feign消费者调用服务,减小了各微服务服务器的访问压力,默认采用了经典的轮询机制

5、熔断器Hystrix解决了,微服务架构中服务器雪崩现象

6、服务监控(单机Dashboard与集群turbine),方便运维人员查看微服务架构项目运行时,各个服务器的运行状态

7、服务配置中心(springcloud config),用来通过github统一管理各个微服务的配置文件(yml文件)

 

创建父工程t226microservice

父工程是一个maven项目,一般创建方式即可,父工程的主要用途是锁定pom依赖包版本。由于springcloud2X停止更新,这里我们采用稳定的低版本,配套的springboot版本为1x版本。

Pom.xml配置如下

pom.xml 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5   <modelVersion>4.0.0</modelVersion>
 6 
 7   <groupId>com.yuan</groupId>
 8   <artifactId>t226microservice</artifactId>
 9   <version>1.0-SNAPSHOT</version>
10 
11   <name>t226microservice</name>
12   <properties>
13     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14     <maven.compiler.source>1.8</maven.compiler.source>
15     <maven.compiler.target>1.8</maven.compiler.target>
16     <druid.version>1.1.10</druid.version>
17   </properties>
18 
19   <!--锁定pom依赖jar包但是并不实际引入-->
20   <dependencyManagement>
21     <dependencies>
22       <dependency>
23         <groupId>org.springframework.cloud</groupId>
24         <artifactId>spring-cloud-dependencies</artifactId>
25         <version>Edgware.SR4</version>
26         <type>pom</type>
27         <scope>import</scope>
28       </dependency>
29       <dependency>
30         <groupId>org.springframework.boot</groupId>
31         <artifactId>spring-boot-dependencies</artifactId>
32         <version>1.5.13.RELEASE</version>
33         <type>pom</type>
34         <scope>import</scope>
35       </dependency>
36       <!--  连接池  -->
37       <dependency>
38         <groupId>com.alibaba</groupId>
39         <artifactId>druid-spring-boot-starter</artifactId>
40         <version>${druid.version}</version>
41       </dependency>
42     </dependencies>
43   </dependencyManagement>
44 </project>

 

创建通用模块microservice-common

通用模块主要存放实体类、工具包等被整个微服务框架所使用的代码。创建一个简单的springboot模块即可。相关代码如下

pom.xml 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.2.1.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.yuan</groupId>
12     <artifactId>microservice-common</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>microservice-eureka-server-2001</name>
15     <description>Demo project for Spring Boot</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19     </properties>
20 
21     <dependencies>
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter</artifactId>
25         </dependency>
26 
27         <dependency>
28             <groupId>org.springframework.boot</groupId>
29             <artifactId>spring-boot-starter-test</artifactId>
30             <scope>test</scope>
31             <exclusions>
32                 <exclusion>
33                     <groupId>org.junit.vintage</groupId>
34                     <artifactId>junit-vintage-engine</artifactId>
35                 </exclusion>
36             </exclusions>
37         </dependency>
38     </dependencies>
39 
40     <build>
41         <plugins>
42             <plugin>
43                 <groupId>org.springframework.boot</groupId>
44                 <artifactId>spring-boot-maven-plugin</artifactId>
45             </plugin>
46         </plugins>
47     </build>
48 
49 </project>

MicroserviceCommonApplication

 1 package com.yuan.microservicecommon;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 6 import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
 7 
 8 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
 9 public class MicroserviceCommonApplication {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(MicroserviceCommonApplication.class, args);
13     }
14 
15 }

Student

 1 package com.yuan.microservicecommon.entity;
 2 
 3 import javax.persistence.*;
 4 import java.io.Serializable;
 5 
 6 @Entity
 7 @Table(name = "t_springcloud_student")
 8 public class Student implements Serializable {
 9 
10     private static final long serialVersionUID = 1L;
11 
12     @Id
13     @GeneratedValue
14     private Integer id;
15 
16     @Column(length = 50)
17     private String name;
18 
19     @Column(length = 50)
20     private String grade;
21 
22     public Integer getId() {
23         return id;
24     }
25 
26     public void setId(Integer id) {
27         this.id = id;
28     }
29 
30     public String getName() {
31         return name;
32     }
33 
34     public void setName(String name) {
35         this.name = name;
36     }
37 
38     public String getGrade() {
39         return grade;
40     }
41 
42     public void setGrade(String grade) {
43         this.grade = grade;
44     }
45 
46 
47 }

 

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

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

pom.xml 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>com.yuan</groupId>
 7         <artifactId>t226microservice</artifactId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <artifactId>microservice-student-provider-1001</artifactId>
11 
12     <properties>
13         <java.version>1.8</java.version>
14     </properties>
15     <dependencies>
16         <dependency>
17             <groupId>org.springframework.boot</groupId>
18             <artifactId>spring-boot-starter-web</artifactId>
19         </dependency>
20         <dependency>
21             <groupId>org.springframework.boot</groupId>
22             <artifactId>spring-boot-starter-test</artifactId>
23             <scope>test</scope>
24         </dependency>
25         <dependency>
26             <groupId>org.springframework.boot</groupId>
27             <artifactId>spring-boot-starter-data-jpa</artifactId>
28         </dependency>
29         <dependency>
30             <groupId>mysql</groupId>
31             <artifactId>mysql-connector-java</artifactId>
32         </dependency>
33         <dependency>
34             <groupId>org.springframework.boot</groupId>
35             <artifactId>spring-boot-starter-tomcat</artifactId>
36         </dependency>
37         <dependency>
38             <groupId>com.alibaba</groupId>
39             <artifactId>druid-spring-boot-starter</artifactId>
40         </dependency>
41         <!--  修改后立即生效,热部署  -->
42         <dependency>
43             <groupId>org.springframework</groupId>
44             <artifactId>springloaded</artifactId>
45         </dependency>
46         <dependency>
47             <groupId>org.springframework.boot</groupId>
48             <artifactId>spring-boot-devtools</artifactId>
49         </dependency>
50         <dependency>
51             <groupId>com.yuan</groupId>
52             <artifactId>microservice-common</artifactId>
53             <version>1.0-SNAPSHOT</version>
54             <scope>compile</scope>
55         </dependency>
56         <!--添加注册中心Eureka相关配置-->
57         <dependency>
58             <groupId>org.springframework.cloud</groupId>
59             <artifactId>spring-cloud-starter-eureka</artifactId>
60         </dependency>
61         <dependency>
62             <groupId>org.springframework.cloud</groupId>
63             <artifactId>spring-cloud-starter-config</artifactId>
64         </dependency>
65         <!-- actuator监控引入 -->
66         <dependency>
67             <groupId>org.springframework.boot</groupId>
68             <artifactId>spring-boot-starter-actuator</artifactId>
69         </dependency>
70 
71     </dependencies>
72     <build>
73         <plugins>
74             <plugin>
75                 <groupId>org.springframework.boot</groupId>
76                 <artifactId>spring-boot-maven-plugin</artifactId>
77             </plugin>
78         </plugins>
79     </build>
80 
81 </project>

 

application.yml 

 1 server:
 2   port: 1001
 3   context-path: /
 4 spring:
 5   datasource:
 6     type: com.alibaba.druid.pool.DruidDataSource
 7     driver-class-name: com.mysql.jdbc.Driver
 8     url: jdbc:mysql://localhost:3306/j2ee?useUnicode=true&characterEncoding=utf8
 9     username: root
10     password: 12345
11   jpa:
12     hibernate:
13       ddl-auto: update
14     show-sql: true

 

MicroserviceStudentProvider1001Application.java 

 1 package com.yuan.microservicestudentprovider1001;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.autoconfigure.domain.EntityScan;
 6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 7 
 8 @EntityScan("com.yuan.*.*")
 9 @EnableEurekaClient
10 @SpringBootApplication
11 public class MicroserviceStudentProvider1001Application {
12 
13     public static void main(String[] args) {
14         SpringApplication.run(MicroserviceStudentProvider1001Application.class, args);
15     }
16 
17 }

 

StudentRepository.java 

1 package com.yuan.microservicestudentprovider1001.repository;
2 
3 import com.yuan.microservicecommon.entity.Student;
4 import org.springframework.data.jpa.repository.JpaRepository;
5 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
6 
7 public interface StudentRepository extends JpaRepository<Student, Integer>, JpaSpecificationExecutor<Student> {
8  
9 }

 

StudentService.java 

 1 package com.yuan.microservicestudentprovider1001.service;
 2 
 3 import com.yuan.microservicecommon.entity.Student;
 4 
 5 import java.util.List;
 6 
 7 public interface StudentService {
 8  
 9     public void save(Student student);
10      
11     public Student findById(Integer id);
12      
13     public List<Student> list();
14      
15     public void delete(Integer id);
16      
17      
18 }

 

StudentServiceImpl.java 

 1 package com.yuan.microservicestudentprovider1001.service.impl;
 2 
 3 import com.yuan.microservicecommon.entity.Student;
 4 import com.yuan.microservicestudentprovider1001.repository.StudentRepository;
 5 import com.yuan.microservicestudentprovider1001.service.StudentService;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Service;
 8 
 9 import java.util.List;
10 
11 @Service
12 public class StudentServiceImpl implements StudentService {
13     @Autowired
14     private StudentRepository studentRepository;
15 
16     @Override
17     public void save(Student student) {
18         studentRepository.save(student);
19     }
20 
21     @Override
22     public Student findById(Integer id) {
23         return studentRepository.findOne(id);
24     }
25 
26     @Override
27     public List<Student> list() {
28         return studentRepository.findAll();
29     }
30 
31     @Override
32     public void delete(Integer id) {
33         studentRepository.delete(id);
34     }
35 }

 

StudentProviderController.java 

 1 package com.yuan.microservicestudentprovider1001.controller;
 2 
 3 import com.yuan.microservicecommon.entity.Student;
 4 import com.yuan.microservicestudentprovider1001.service.StudentService;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.web.bind.annotation.*;
 7 
 8 import java.util.List;
 9 
10 @RestController
11 @RequestMapping("/student")
12 public class StudentProviderController {
13  
14     @Autowired
15     private StudentService studentService;
16      
17     @PostMapping(value="/save")
18     public boolean save(Student student){
19         try{
20             studentService.save(student);  
21             return true;
22         }catch(Exception e){
23             return false;
24         }
25     }
26      
27     @GetMapping(value="/list")
28     public List<Student> list(){
29         return studentService.list();
30     }
31      
32     @GetMapping(value="/get/{id}")
33     public Student get(@PathVariable("id") Integer id){
34         return studentService.findById(id);
35     }
36      
37     @GetMapping(value="/delete/{id}")
38     public boolean delete(@PathVariable("id") Integer id){
39         try{
40             studentService.delete(id);
41             return true;
42         }catch(Exception e){
43             return false;
44         }
45     }
46 }

 

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

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

pom.xml 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>com.yuan</groupId>
 7         <artifactId>t226microservice</artifactId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <artifactId>microservice-student-consumer-80</artifactId>
11 
12     <properties>
13         <java.version>1.8</java.version>
14     </properties>
15     <dependencies>
16         <dependency>
17             <groupId>org.springframework.boot</groupId>
18             <artifactId>spring-boot-starter-web</artifactId>
19         </dependency>
20         <dependency>
21             <groupId>org.springframework.boot</groupId>
22             <artifactId>spring-boot-starter-test</artifactId>
23             <scope>test</scope>
24         </dependency>
25         <dependency>
26             <groupId>org.springframework.boot</groupId>
27             <artifactId>spring-boot-starter-data-jpa</artifactId>
28         </dependency>
29         <dependency>
30             <groupId>mysql</groupId>
31             <artifactId>mysql-connector-java</artifactId>
32         </dependency>
33         <dependency>
34             <groupId>org.springframework.boot</groupId>
35             <artifactId>spring-boot-starter-tomcat</artifactId>
36         </dependency>
37         <!--  修改后立即生效,热部署  -->
38         <dependency>
39             <groupId>org.springframework</groupId>
40             <artifactId>springloaded</artifactId>
41         </dependency>
42         <dependency>
43             <groupId>org.springframework.boot</groupId>
44             <artifactId>spring-boot-devtools</artifactId>
45         </dependency>
46         <dependency>
47             <groupId>com.yuan</groupId>
48             <artifactId>microservice-common</artifactId>
49             <version>1.0-SNAPSHOT</version>
50             <scope>compile</scope>
51         </dependency>
52     </dependencies>
53     <build>
54         <plugins>
55             <plugin>
56                 <groupId>org.springframework.boot</groupId>
57                 <artifactId>spring-boot-maven-plugin</artifactId>
58             </plugin>
59         </plugins>
60     </build>
61 
62 </project>

 

application.yml 

server:
  port: 81
  context-path: /

 

MicroserviceStudentConsumer80Application.java 

 1 package com.yuan.microservicestudentconsumer80;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 6 import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
 7 
 8 @SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
 9 public class MicroserviceStudentConsumer80Application {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(MicroserviceStudentConsumer80Application.class, args);
13     }
14 
15 }

SpringCloudConfig.java 

 1 package com.yuan.microservicestudentconsumer80.config;
 2 
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.Configuration;
 5 import org.springframework.web.client.RestTemplate;
 6 
 7 @Configuration
 8 public class SpringCloudConfig {
 9  
10     @Bean
11     public RestTemplate getRestTemplate(){
12         return new RestTemplate();
13     }
14 }

 

StudentConsumerController.java 

 1 package com.yuan.microservicestudentconsumer80.controller;
 2 
 3 import com.yuan.microservicecommon.entity.Student;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.web.bind.annotation.*;
 6 import org.springframework.web.client.RestTemplate;
 7 
 8 import java.util.List;
 9 
10 @RestController
11 @RequestMapping("/student")
12 public class StudentConsumerController {
13 
14     private final static String SERVER_IP_PORT = "http://localhost:1001";
15  
16      @Autowired
17      private RestTemplate restTemplate;
18       
19      @PostMapping(value="/save")
20      private boolean save(Student student){
21          return restTemplate.postForObject(SERVER_IP_PORT+"/student/save", student, Boolean.class);
22      }
23       
24     @GetMapping(value="/list")
25     public List<Student> list(){
26         return restTemplate.getForObject(SERVER_IP_PORT+"/student/list", List.class);
27     }
28      
29     @GetMapping(value="/get/{id}")
30     public Student get(@PathVariable("id") Integer id){
31         return restTemplate.getForObject(SERVER_IP_PORT+"/student/get/"+id, Student.class);
32     }
33      
34     @GetMapping(value="/delete/{id}")
35     public boolean delete(@PathVariable("id") Integer id){
36         try{
37             restTemplate.getForObject(SERVER_IP_PORT+"/student/delete/"+id, Boolean.class);
38             return true;
39         }catch(Exception e){
40             return false;
41         }
42     }
43 }

运行结果

 

 

 

 

 

 

 

 

初识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 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>com.yuan</groupId>
 7         <artifactId>t226microservice</artifactId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <artifactId>microservice-eureka-server-2001</artifactId>
11 
12 
13     <properties>
14         <java.version>1.8</java.version>
15     </properties>
16 
17     <dependencies>
18         <dependency>
19             <groupId>org.springframework.cloud</groupId>
20             <artifactId>spring-cloud-starter-eureka-server</artifactId>
21         </dependency>
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter-test</artifactId>
25             <scope>test</scope>
26         </dependency>
27         <!--&lt;!&ndash;  修改后立即生效,热部署  &ndash;&gt;-->
28         <dependency>
29             <groupId>org.springframework</groupId>
30             <artifactId>springloaded</artifactId>
31         </dependency>
32         <dependency>
33             <groupId>org.springframework.boot</groupId>
34             <artifactId>spring-boot-devtools</artifactId>
35         </dependency>
36     </dependencies>
37     <build>
38         <plugins>
39             <plugin>
40                 <groupId>org.springframework.boot</groupId>
41                 <artifactId>spring-boot-maven-plugin</artifactId>
42             </plugin>
43         </plugins>
44     </build>
45 
46 </project>

 

application.yml 

 1 server:
 2   port: 2001
 3   context-path: /
 4 eureka:
 5   instance:
 6     hostname: localhost #eureka注册中心实例名称
 7   client:
 8     register-with-eureka: false     #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
 9     fetch-registry: false     #false 由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
10     service-url:
11       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka注册中心交互的地址,查询服务和注册服务用到

MicroserviceEurekaServer2001Application.java 

 1 package com.yuan.microserviceeurekaserver2001;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 6 
 7 @EnableEurekaServer
 8 @SpringBootApplication
 9 public class MicroserviceEurekaServer2001Application {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(MicroserviceEurekaServer2001Application.class, args);
13     }
14 
15 }

 

Eureka中注册服务提供者

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

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

1 <!--添加注册中心Eureka相关配置-->
2 <dependency>
3     <groupId>org.springframework.cloud</groupId>
4     <artifactId>spring-cloud-starter-eureka</artifactId>
5 </dependency>
6 <dependency>
7     <groupId>org.springframework.cloud</groupId>
8     <artifactId>spring-cloud-starter-config</artifactId>
9 </dependency>

 

然后application.yml上加上配置:

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

 

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

 

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

 1 package com.yuan.microservicestudentprovider1001;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.autoconfigure.domain.EntityScan;
 6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 7 
 8 @EntityScan("com.yuan.*.*")
 9 @EnableEurekaClient
10 @SpringBootApplication
11 public class MicroserviceStudentProvider1001Application {
12 
13     public static void main(String[] args) {
14         SpringApplication.run(MicroserviceStudentProvider1001Application.class, args);
15     }
16 
17 }

 

启动顺序: 1、端口2001  注册中心, 2、端口80或者81服务提供者,最后启动1001端口项目

 

访问2001结果

 

点击上图中status绿色实例状态结果如下

解决方案如下

1、首先在服务提供者项目pom.xml里加入actuator监控依赖:

1 <!-- actuator监控引入 -->
2 <dependency>
3     <groupId>org.springframework.boot</groupId>
4     <artifactId>spring-boot-starter-actuator</artifactId>
5 </dependency>

 

2、最后服务提供者项目application.yml加上info配置:

1 info:
2   groupId: com.yuan.t226Springcloud
3   artifactId: microservice-student-provider-1001
4   version: 1.0-SNAPSHOT
5   userName: http://yuan.com
6   phone: 123456

 

解决后显示结果为

 

 

谢谢观看!!!

 

posted @ 2019-12-05 20:59  Me*源  阅读(259)  评论(0编辑  收藏  举报