服务拆分注意事项1.不同微服务不要重复开发相同业务2微服务数据独立不要访问其它微服务的数据库3.微服务可以将自己的业务暴露为接口供其它微服务调用1.微服务需要根据业务模块拆分做到单一职责不要重复开发相同业务2微服务可以将业务暴露为接口供其它微服务使用3.不同微服务都应该有自己独立的数据库准备好初始文件服务远程调用雪花注册RestTemplate首先我们在order-service服务中的OrderApplication启动类中注册RestTemplate实例package cn.itcast.order; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; MapperScan(cn.itcast.order.mapper) SpringBootApplication public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } Bean public RestTemplate restTemplate() { return new RestTemplate(); } }实现远程调用修改order-service服务中的cn.itcast.order.service包下的OrderService类中的queryOrderById方法Eureka-提供者与消费者服务提供者一次业务中被其它微服务调用的服务。提供接口给其它微服务服务消费者一次业务中调用其它微服务的服务。调用其它微服务提供的接口服务提供者暴露接口给其它微服务调用服务消费者调用其它微服务提供的接口提供者与消费者角色其实是相对的一个服务可以同时是服务提供者和服务消费者Eureka注册中心-eureka原理分析搭建eureka服务引入SpringCloud为eureka提供的starter依赖dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-netflix-eureka-server/artifactId /dependency给eureka-server服务编写一个启动类一定要添加一个EnableEurekaServer注解开启eureka的注册中心功能package cn.itcast.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; SpringBootApplication EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }编写一个application.yml文件内容如下server: port: 10086 spring: application: name: eureka-server eureka: client: service-url: defaultZone: http://127.0.0.1:10086/eureka启动微服务然后在浏览器访问http://127.0.0.1:10086看到下面结果应该是成功了服务注册下面我们将user-service注册到eureka-server中去。1引入依赖在user-service的pom文件中引入下面的eureka-client依赖dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-netflix-eureka-client/artifactId /dependency2配置文件在user-service中修改application.yml文件添加服务名称、eureka地址spring: application: name: userservice eureka: client: service-url: defaultZone: http://127.0.0.1:10086/eureka3启动多个user-service实例为了演示一个服务有多个实例的场景我们添加一个SpringBoot的启动配置再启动一个user-service。首先复制原来的user-service启动配置然后在弹出的窗口中填写信息现在SpringBoot窗口会出现两个user-service启动配置不过第一个是8081端口第二个是8082端口。启动两个user-service实例查看eureka-server管理页面服务发现下面我们将order-service的逻辑修改向eureka-server拉取user-service的信息实现服务发现。1引入依赖之前说过服务发现、服务注册统一都封装在eureka-client依赖因此这一步与服务注册时一致。在order-service的pom文件中引入下面的eureka-client依赖dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-netflix-eureka-client/artifactId /dependency2配置文件服务发现也需要知道eureka地址因此第二步与服务注册一致都是配置eureka信息在order-service中修改application.yml文件添加服务名称、eureka地址spring: application: name: orderservice eureka: client: service-url: defaultZone: http://127.0.0.1:10086/eureka3服务拉取和负载均衡最后我们要去eureka-server中拉取user-service服务的实例列表并且实现负载均衡。不过这些动作不用我们去做只需要添加一些注解即可。在order-service的OrderApplication中给RestTemplate这个Bean添加一个LoadBalanced注解修改order-service服务中的cn.itcast.order.service包下的OrderService类中的queryOrderById方法。修改访问的url路径用服务名代替ip、端口spring会自动帮助我们从eureka-server端根据userservice这个服务名称获取实例列表而后完成负载均衡。