SpringCloud- 注册服务中心(Eureka)
1.SpirngCloud
微服务架构(属于分布式架构)是在传统的soa架构上升级,将业务逻辑拆分的更细,轻量级,在开发过程中例如电商项目中:
支付服务 订单服务 会员服务 (可以理解一个服务就是一个业务逻辑 一般一个服务连接单独的数据库,这些都是接口项目)
不同的工程 可能都需要调用到支付服务,或者订单服务 通过RPC远程调用接口:http+json+restful,这些接口只能在内部访问,即使和外网接口进行对接也是采用https
一个公司项目有很多接口,每个接口有自己的地址,对这些接口如何能快速系统的管理。这就是springcloud能做的事情
2.搭建SpirngCloud-Eureka例子:
Eureka是Netflix开源的一个RESTful服务,主要用于服务的注册发现。Eureka由两个组件组成:Eureka服务器和Eureka客户端。Eureka服务器用作服务注册服务器。Eureka客户端是一个java客户端,用来简化与服务器的交互、作为轮询负载均衡器,并提供服务的故障切换支持。Netflix在其生产环境中使用的是另外的客户端,它提供基于流量、资源利用率以及出错状态的加权负载均衡。
本次实现的例子:订单服务调用会员服务
步骤一 (引入所需要的依赖):
<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.jiahou</groupId> <artifactId>eureka-service</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--eureka server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!-- spring boot test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
步骤二(创建.yml文件或者.properties文件):
server:
port: 8888
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
步骤三(创建启动类):
package com; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaApp { public static void main(String[] args) { SpringApplication.run(EurekaApp.class, args); } }
步骤四(运行结果):输入127.0.0.1:8888
3.搭建接口提供服务:
步骤一:
创建member-server项目(pom导入所需要的依赖):
<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.jiahou</groupId> <artifactId>member-server</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--eureka server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!-- spring boot test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
步骤二:
创建yml配置文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/ //注册中心的地址
server:
port: 8762 //项目member-server的端口
spring:
application:
name: service-member //在注册服务中心的名字
步骤三:
package com.entity; public class User { private String userName; private String userAge; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserAge() { return userAge; } public void setUserAge(String userAge) { this.userAge = userAge; } }
package com.controller; import java.util.ArrayList; import java.util.List; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.entity.User; @RestController public class ReqController { @RequestMapping("/getMenberMsg") public List<User> getMemberMsg() { List<User> arrs = new ArrayList<>(); User user = new User(); user.setUserAge("15"); user.setUserName("Jason"); arrs.add(user); return arrs; } }
package com; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer//该注解的作用是将此服务注册到eureka服务中心 public class MemberApp { public static void main(String[] args) { SpringApplication.run(MemberApp.class, args); } }
步骤四:
启动member-server服务
请求地址栏输入地址:http://127.0.0.1:8762/getMenberMsg
(启动member-server服务的时候 需要先把eureka服务器启动好,否则member-server日志窗口会打印找不到注册服务的异常!但是并不妨碍 member-server的运行)
步骤五:
打开eureka-server服务,可以看到member-server已经注册到eureka注册服务中心
4.搭建消费接口服务:
步骤一:(创建order-server服务)
pom文件导入所需要的依赖:
<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.jiahou</groupId> <artifactId>order-server</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--eureka server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!-- spring boot test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
步骤二:(创建yml文件)
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
server:
port: 8764
spring:
application:
name: service-order
步骤三:远程调用member-server
远程调用的方法有三种:
1.Rest
2.Feign
3.ribbon(用作负载均衡)
使用RestTemplate 远程调用接口
package com.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class MemberService { @Autowired RestTemplate restTemplate; @SuppressWarnings("unchecked") public List<Object> getOrderByUserList() { //"http://service-member/getMenberMsg" 这个地址对应 return restTemplate.getForObject("http://service-member/getMenberMsg", List.class); } }
控制层调用service层
package com.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.service.MemberService; @RestController public class OrderController { @Autowired private MemberService memberService; @RequestMapping("/getOrderByUserList") public List<Object> getOrderByUserList() { return memberService.getOrderByUserList(); } }
步骤四:(启动order-server)
启动之前需要配置RestTemplate 模板
package com; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaServer public class OrderApp { public static void main(String[] args) { SpringApplication.run(OrderApp.class, args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
打开eureka服务中心:
步骤五:(测试远程调用)
5.Eureka注册服务中心调用原理:
(1)会员服务(member-server)注册到eureka服务中心的时候 将会员服务的ip地址和端口信息 都存放到了服务中心
server port:=8762
spring.application.name:=service-member
(2) 订单服务可以通过service-member在注册中心找到service-member对应的真是ip地址
(3) 订单服务 通过httpclient 调用会员服务