简介
Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring Cloud并没有重复制造轮子,它只是将目前各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。
配套参考资料:
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文件)
微服务架构注意点:
1、springboot、springcloud版本在父工程定义;
2、由于通用模块无需操作数据库,springboot启动默认会读取数据库,所以得添加以下注解 @SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class});
3、分布式jpa需要在启动类上添加@EntityScan("com.javaxl.*.*");
4、消费者需要添加配置类获取org.springframework.web.client.RestTemplate,springcloud底层是通过RestTemplate来调用提供者的服务的。
创建父工程microservice
父工程是一个maven项目,一般创建方式即可,父工程的主要用途是锁定pom依赖包版本。由于springcloud2X停止更
配置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.javaqi</groupId> 8 <artifactId>qimicroservice</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>war</packaging> 11 12 13 <properties> 14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 15 <maven.compiler.source>1.8</maven.compiler.source> 16 <maven.compiler.target>1.8</maven.compiler.target> 17 <druid.version>1.1.10</druid.version> 18 </properties> 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 45 </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>com.javaqi</groupId> 7 <artifactId>qimicroservice</artifactId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <artifactId>microservice-common</artifactId> 11 12 13 <properties> 14 <java.version>1.8</java.version> 15 </properties> 16 17 <dependencies> 18 <dependency> 19 <groupId>org.springframework.boot</groupId> 20 <artifactId>spring-boot-starter-web</artifactId> 21 </dependency> 22 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-test</artifactId> 26 <scope>test</scope> 27 </dependency> 28 29 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-data-jpa</artifactId> 33 </dependency> 34 35 <dependency> 36 <groupId>mysql</groupId> 37 <artifactId>mysql-connector-java</artifactId> 38 </dependency> 39 </dependencies> 40 41 <build> 42 <plugins> 43 <plugin> 44 <groupId>org.springframework.boot</groupId> 45 <artifactId>spring-boot-maven-plugin</artifactId> 46 </plugin> 47 </plugins> 48 </build> 49 50 </project>
MicroserviceCommonApplication
1 package com.javaqi.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.javaqi.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 public void setId(Integer id) { 26 this.id = id; 27 } 28 public String getName() { 29 return name; 30 } 31 public void setName(String name) { 32 this.name = name; 33 } 34 public String getGrade() { 35 return grade; 36 } 37 public void setGrade(String grade) { 38 this.grade = grade; 39 } 40 41 42 43 }
创建服务提供者microservice-student-provider-1001
创建一个简单的springboot模块,这里服务提供者需要操作数据库并且被浏览器所访问,
配置pom
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.javaqi</groupId> 7 <artifactId>qimicroservice</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 16 <dependencies> 17 <dependency> 18 <groupId>com.javaqi</groupId> 19 <artifactId>microservice-common</artifactId> 20 </dependency> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-web</artifactId> 24 </dependency> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-test</artifactId> 28 <scope>test</scope> 29 </dependency> 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-data-jpa</artifactId> 33 </dependency> 34 <dependency> 35 <groupId>mysql</groupId> 36 <artifactId>mysql-connector-java</artifactId> 37 </dependency> 38 <dependency> 39 <groupId>org.springframework.boot</groupId> 40 <artifactId>spring-boot-starter-tomcat</artifactId> 41 </dependency> 42 <dependency> 43 <groupId>com.alibaba</groupId> 44 <artifactId>druid-spring-boot-starter</artifactId> 45 </dependency> 46 <!-- 修改后立即生效,热部署 --> 47 <dependency> 48 <groupId>org.springframework</groupId> 49 <artifactId>springloaded</artifactId> 50 </dependency> 51 <dependency> 52 <groupId>org.springframework.boot</groupId> 53 <artifactId>spring-boot-devtools</artifactId> 54 </dependency> 55 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 <dependency> 66 <groupId>com.javaqi</groupId> 67 <artifactId>microservice-common</artifactId> 68 <version>1.0-SNAPSHOT</version> 69 <scope>compile</scope> 70 </dependency> 71 72 <!-- actuator监控引入 --> 73 <dependency> 74 <groupId>org.springframework.boot</groupId> 75 <artifactId>spring-boot-starter-actuator</artifactId> 76 </dependency> 77 </dependencies> 78 79 <build> 80 <plugins> 81 <plugin> 82 <groupId>org.springframework.boot</groupId> 83 <artifactId>spring-boot-maven-plugin</artifactId> 84 </plugin> 85 </plugins> 86 </build> 87 88 </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/xufanqi?useUnicode=true&characterEncoding=utf8 9 username: root 10 password: 123 11 jpa: 12 hibernate: 13 ddl-auto: update 14 show-sql: true 15
MicroserviceStudentProvider1001Application
1 package com.javaqi.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.javaqi.*.*") 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 }
Repository层
1 package com.javaqi.microservicestudentprovider1001.repository; 2 3 import com.javaqi.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 }
Controller层
package com.javaqi.microservicestudentprovider1001.controller; import com.javaqi.microservicecommon.entity.Student; import com.javaqi.microservicestudentprovider1001.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @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; } } }
Service层
1 package com.javaqi.microservicestudentprovider1001.service; 2 3 import com.javaqi.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
1 package com.javaqi.microservicestudentprovider1001.service.impl; 2 3 import com.javaqi.microservicecommon.entity.Student; 4 import com.javaqi.microservicestudentprovider1001.repository.StudentRepository; 5 import com.javaqi.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 14 @Autowired 15 private StudentRepository studentRepository; 16 17 @Override 18 public void save(Student student) { 19 studentRepository.save(student); 20 } 21 22 @Override 23 public Student findById(Integer id) { 24 return studentRepository.findOne(id); 25 } 26 27 @Override 28 public List<Student> list() { 29 return studentRepository.findAll(); 30 } 31 32 @Override 33 public void delete(Integer id) { 34 studentRepository.delete(id); 35 } 36 37 }
创建服务消费者microservice-student-consumer-80
服务消费者主要是通过restful api来调用提供者的接口,固不需要不需要操作数据库
配置pom
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.javaqi</groupId> 7 <artifactId>qimicroservice</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>com.javaqi</groupId> 18 <artifactId>microservice-common</artifactId> 19 </dependency> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-web</artifactId> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-test</artifactId> 27 <scope>test</scope> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-data-jpa</artifactId> 32 </dependency> 33 <dependency> 34 <groupId>mysql</groupId> 35 <artifactId>mysql-connector-java</artifactId> 36 </dependency> 37 <dependency> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-starter-tomcat</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.javaqi</groupId> 52 <artifactId>microservice-common</artifactId> 53 <version>1.0-SNAPSHOT</version> 54 <scope>compile</scope> 55 </dependency> 56 </dependencies> 57 <build> 58 <plugins> 59 <plugin> 60 <groupId>org.springframework.boot</groupId> 61 <artifactId>spring-boot-maven-plugin</artifactId> 62 </plugin> 63 </plugins> 64 </build> 65 66 </project>
Config层
1 package com.javaqi.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 }
Controller层
StudentConsumerController
1 package com.javaqi.microservicestudentconsumer80.controller; 2 3 import com.javaqi.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 }
application.yml
1 server: 2 port: 80 3 context-path: /
MicroserviceStudentConsumer80Application
1 package com.javaqi.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 }
浏览器访问
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
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.javaqi</groupId> 7 <artifactId>qimicroservice</artifactId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <artifactId>microservice-eureka-server-2001</artifactId> 11 12 <properties> 13 <java.version>1.8</java.version> 14 </properties> 15 16 <dependencies> 17 <dependency> 18 <groupId>org.springframework.cloud</groupId> 19 <artifactId>spring-cloud-starter-eureka-server</artifactId> 20 </dependency> 21 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-test</artifactId> 25 <scope>test</scope> 26 </dependency> 27 28 <!-- 修改后立即生效,热部署 --> 29 <dependency> 30 <groupId>org.springframework</groupId> 31 <artifactId>springloaded</artifactId> 32 </dependency> 33 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-devtools</artifactId> 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>
配置application.yml
1 server: 2 port: 2001 3 context-path: / 4 eureka: 5 instance: 6 hostname: localhost #eurekau6CE8u518Cu4E2Du5FC3u5B9Eu4F8Bu540Du79F0 7 client: 8 register-with-eureka: false #false u7531u4E8Eu8BE5u5E94u7528u4E3Au6CE8u518Cu4E2Du5FC3uFF0Cu6240u4EE5u8BBEu7F6Eu4E3Afalse,u4EE3u8868u4E0Du5411u6CE8u518Cu4E2Du5FC3u6CE8u518Cu81EAu5DF1u3002 9 fetch-registry: false #false u7531u4E8Eu6CE8u518Cu4E2Du5FC3u7684u804Cu8D23u5C31u662Fu7EF4u62A4u670Du52A1u5B9Eu4F8BuFF0Cu5B83u5E76u4E0Du9700u8981u53BBu68C0u7D22u670Du52A1uFF0Cu6240u4EE5u4E5Fu8BBEu7F6Eu4E3Afalse 10 service-url: 11 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #u8BBEu7F6Eu4E0EEurekau6CE8u518Cu4E2Du5FC3u4EA4u4E92u7684u5730u5740uFF0Cu67E5u8BE2u670Du52A1u548Cu6CE8u518Cu670Du52A1u7528u5230
MicroserviceEurekaServer2001Application
1 package com.javaqi.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客户端依赖:.
<!--添加注册中心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>
然后microservice-student-provider-1001的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.javaqi.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.javaqi.*.*") 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 }
启动顺序:
启动服务注册中心,再启动这个服务提供者
结果
解决小红框点击出现Whitelabel Error Page问题
首先在服务提供者项目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
结果如下