一主要讲了一些关于Eureka 的概念,这一篇用代码去实现
服务端 高可用
pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.3.5.RELEASE</version> </dependency>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
配置文件
spring.application.name=microservice-discovery-eureka server.port=8761 #设置为Eureka Server交互的地址,将8761这个Eureka服务端注册到8762上面 8762和8761一样 相互注册 eureka.client.service-url.defaultZone=http://localhost:8762/eureka/启动类:
@SpringBootApplication //开启Eureka 服务注册中心 @EnableEurekaServer public class MicroserviceDiscoveryEurekaApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceDiscoveryEurekaApplication.class, args); } }8762是一样的,这样就完成了一个服务的高可用
客户端:
pom.xml
<!--eureka的客户端--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.3.5.RELEASE</version> </dependency>配置文件:
server.port=8082 #Eureka spring.application.name=microservice-consumer-movie #向两个服务端注册自己 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/,http://localhost:8762/eureka/ eureka.instance.prefer-ip-address=true
启动类:
@EnableDiscoveryClient @SpringBootApplication public class MicroserviceSimpleConsumnerMovieApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceSimpleConsumnerMovieApplication.class, args); } }
这样就完成了一个服务的注册,服务名为microservice-consumer-movie。
另一个服务:
server.port=8081 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/springcloud?characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useUnicode=true spring.datasource.username=root spring.datasource.password=root #eureka #注册到Eureka Server 上的应用名称 spring.application.name=microservice-provider-user #Eureka Server的地址 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/,http://localhost:8762/eureka/ #默认为false true是将自己的IP注册到Eureka Server 如果为false 则表示注册微服务所在操作系统的hostName eureka.instance.prefer-ip-address=true
这样就完成了两个服务的注册,第二个服务名为microservice-provider-user
在未使用Eureka之前 服务consumer调用服务user
package com.yjp.microservicesimpleconsumnermovie.client.controller; import com.yjp.microservicesimpleconsumnermovie.client.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class MovieController { @Autowired private RestTemplate restTemplate; @GetMapping("/user/{id}") public User findById(@PathVariable int id) { return restTemplate.getForObject("http://localhost:8081/" + id, User.class); } }这样会有一个问题,我们是通过ip和端口号去调用的,就会有单点故障的问题,达不到我们的高可用要求
用Eureka之后:
我们可以用服务的名称去调用
package com.yjp.microservicesimpleconsumnermovie.client.controller; import com.yjp.microservicesimpleconsumnermovie.client.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class MovieController { @Autowired private RestTemplate restTemplate; @GetMapping("/user/{id}") public User findById(@PathVariable int id) { return restTemplate.getForObject("http://microservice-provider-user/" + id, User.class); } }但是发现这样调用是失败的,这就引出了第二个主键 Ribbon 。
在使用restTemplate的时候需要注意,首先需要在启动类中 将其实例化
@Bean public RestTemplate restTemplate() { return new RestTemplate(); }
努力吧,皮卡丘