引言:这一篇博客是将上一篇spring-cloud-eureka-server的单机模式改为集群模式,体现eureka的高可用特性。生产环境无论是Eureka注册中心还是Client客户端大多是部署在多台机器上,也就是集群模式,只有采用集群模式才能体现eureka的高可用。这篇博客本人采用集群模式实现了spring-cloud-eureka服务端和客户端的高可用,同时使用RestTemplate和Fengin实现了微服务的调用。下面呈上干货代码:

1. 新建maven子项目cloud-eureka-server2,cloud-eureka-client2

    1.1 cloud-eureka-server2.pom:

<?xml version="1.0" encoding="UTF-8"?>
<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"><parent><artifactId>spring-boot-samples</artifactId><groupId>com.hsf</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-eureka-server1</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><description>spring-cloud-netflix-eureka-server cluster project demo</description><dependencies><dependency><groupId>com.oracle</groupId><artifactId>ojdbc6</artifactId><version>11.2.0.3</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-netflix-eureka-server</artifactId><version>2.0.2.RELEASE</version></dependency><dependency><groupId>com.netflix.eureka</groupId><artifactId>eureka-core</artifactId><version>1.9.3</version><exclusions><exclusion><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId><version>2.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId><version>2.0.1.RELEASE</version></dependency><!--<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency> --></dependencies><build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

1.2 cloud-eureka-client2.pom:

<?xml version="1.0" encoding="UTF-8"?>
<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"><parent><artifactId>spring-boot-samples</artifactId><groupId>com.hsf</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-eureka-client2</artifactId><groupId>com.hsf</groupId><version>0.0.1-SNAPSHOT</version><description>spring-cloud-eureka client2 demo</description><packaging>jar</packaging><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId><version>2.0.2.RELEASE</version><exclusions><exclusion><groupId>com.sun.jersey</groupId><artifactId>jersey-client</artifactId></exclusion><exclusion><groupId>com.sun.jersey</groupId><artifactId>jersey-core</artifactId></exclusion><exclusion><groupId>com.sun.jersey.contribs</groupId><artifactId>jersey-apache-client4</artifactId></exclusion></exclusions></dependency><!-- mysql驱动依赖--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.0.2.RELEASE</version><exclusions><exclusion><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></exclusion></exclusions></dependency><!--Json转换工具 --><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.8.4</version></dependency></dependencies><build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

1.3 修改cloud-eureka-server1和cloud-eureka-server2配置文件

 cloud-eureka-server1与cloud-eureka-server2互相注册对方的地址

1) cloud-eureka-server1项目的application-yaml文件

eureka:server:enable-self-preservation: false  #关闭自我保护机制eviction-interval-timer-in-ms: 5000 #默认 60*1000 单位:毫秒instance:hostname: peer1client:fetchRegistry: falseserviceUrl:defaultZone: http://peer2:8762/eureka/   #注册到peer2节点的地址上registerWithEureka: false  #禁用自己注册
spring:profiles:active: devapplication:name: eurekaServer1main:allow-bean-definition-overriding: true#必须配置freemark模板引擎参数,否则启动报错freemarker:template-loader-path: classpath:/templates/prefer-file-system-access: false

2) cloud-eureka-server2项目的application.yaml文件

spring:profiles:active: devapplication:name: eurekaServermain:allow-bean-definition-overriding: truefreemarker:template-loader-path: classpath:/templates/prefer-file-system-access: falseeureka:server:enable-self-preservation: false  #关闭自我保护机制eviction-interval-timer-in-ms: 5000 #默认 60*1000 单位:毫秒instance:hostname: peer2client:fetchRegistry: falseserviceUrl:defaultZone: http://peer1:8761/eureka/   #注册到pper1服务节点的地址上registerWithEureka: false  #禁用自己注册

注意:peer1和peer2域名都需要在 C:\Windows\System32\drivers\etc 目录下的hosts文件中配置对应的域名解析

# localhost name resolution is handled within DNS itself.127.0.0.1       localhost::1             localhost127.0.0.1       peer1127.0.0.1       peer2

3) cloud-eureka-server2项目的application-dev.yaml文件

server:port: 8762address: 127.0.0.1spring:datasource:type: com.zaxxer.hikari.HikariDataSourceurl: jdbc:oracle:thin:@localhost:1521:ORCLdriverClassName: oracle.jdbc.driver.OracleDriverusername: SYSTEMpassword: password

1.4 修改cloud-eureka-client1和cloud-eureka-client2项目的yaml配置文件

1) cloud-eureka-client1项目的application.yaml文件

server:port: 9090connection-timeout: 30000sservlet:context-path: /eurekaClientspring:profiles:active: devapplication:name: eurekaClientdatasource:type: com.zaxxer.hikari.HikariDataSourcedriverClassName: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/test?useSSL=falseusername: rootpassword: password  #mysql数据库root用户连接密码jpa:show-sql: truehibernate:ddl-auto: updateproperties:hibernate:dialect: org.hibernate.dialect.MySQL57Dialectlogging:level:org:hibernate:SQL: debugtype:descriptor:sql: trace

cloud-eureka-client2项目的application.yaml文件只需要修改上述的server.port=9091即可,其他与cloud-eureka-client1项目的

application.yaml内容保持相同。

server:port: 9091

1.5 服务端与客户端启动类代码

1)cloud-eureka-server1 与cloud-eureka-server2 spring-boot项目的启动类代码:

package com.hsf.cloudeurekaserver1;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer
public class EurekaServer1Application {public static void  main(String[] args){SpringApplication.run(EurekaServer1Application.class,args);}
}
package com.hsf.cloudeurekaserver2;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer
@SpringBootApplication
public class EurekaServer2Application {public static void main(String[] args){SpringApplication.run(EurekaServer2Application.class);}}

注意:spring-cloud-eureka服务端项目均要加上@EnableEurekaServer注解

2)  cloud-eureka-client1与 cloud-eureka-client2 spring-boot项目的启动类代码:

package com.hsf.cloudeurekaclient1;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class CloudEurekaClient1Application {public static void main(String[] args) {SpringApplication.run(CloudEurekaClient1Application.class, args);}}
package com.hsf.cloudeurekaclient2;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class EurekaClient2Application {public static void main(String[] args){SpringApplication.run(EurekaClient2Application.class,args);}
}

在IDEA中依次启动cloud-eureka-server1, cloud-eureka-server1, cloud-eureka-client1, cloud-eureka-client2四个项目,项目启动成功后,在浏览器地址栏中输入http://peer1:8761后回车,出现如下画面表示高可用服务注册中心和服务客户端项目启动成功

图 1  Eureka注册中心服务注册与服务发现效果图

上图中可以看到peer1的备用注册中心地址:http://peer2:8762/eureka/ 以及一个服务实例EUREKACLIENT ,但是有两个不同的实例ID,分别是eurekaClient1和eurekaClient2;若出现peer1节点宕机,所有的服务都将注册到pee2节点上来。开发者可以手动关停cloud-eureka-server1应用,然后再浏览器地址栏中输入http://peer2:8762回车查看效果可以看到EUREKACLIENT服务注册到了peer2节点上来了,之前是没有的。注意:开发环境上用的是伪集群,用的是相同IP地址+不同端口号,而正式环境一定是不同机器IP地址+相同端口号的。

2. cloud-eureka-client项目提供RestFull Service Api服务

2.1 检验客户端负载均衡的http接口

cloud-eureka-client1项目的controller包中新建DemoController.java控制器类,代码如下

package com.hsf.cloudeurekaclient1.controller;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;@RestController
public class DemoController {private static final Logger logger = LoggerFactory.getLogger(DemoController.class);@Value("${eureka.instance.hostname}")private String hostname;@Value("${server.port}")private String port;@RequestMapping(path="/remoteIndex",method= RequestMethod.GET)public String Index(){return hostname+":"+port;}}

cloud-eureka-client2项目的DemoController同上,可直接从cloud-eureka-client1项目中复制过来

3. 新建服务消费端项目cloud-web子项目并引入eureka-client和openfengin的依赖

3.1 cloud-web.pom

<?xml version="1.0" encoding="UTF-8"?>
<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"><parent><artifactId>spring-boot-samples</artifactId><groupId>com.hsf</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-web</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId><version>2.0.2.RELEASE</version><exclusions><exclusion><groupId>com.sun.jersey</groupId><artifactId>jersey-client</artifactId></exclusion><exclusion><groupId>com.sun.jersey</groupId><artifactId>jersey-core</artifactId></exclusion><exclusion><groupId>com.sun.jersey.contribs</groupId><artifactId>jersey-apache-client4</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>2.0.2.RELEASE</version></dependency><dependency><groupId>com.oracle</groupId><artifactId>ojdbc6</artifactId><version>11.2.0.3</version></dependency><!-- apache commons加密解密工具类 --><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.10</version></dependency></dependencies><build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>${java.version}</source><target>${java.version}</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>com.hsf.samplesimpl.MySpringApplication</mainClass></configuration></plugin><!-- 跳过单元测试 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin></plugins></build></project>

3.2 cloud-web服务消费端项目yaml配置文件

1) application-dev.yaml

server:address: 127.0.0.1port: 8080rpcServices:serviceName: eurekaClientcontextPath: /eurekaClientindexUrl: /remoteIndexuserInfoUrl: /userInfospring:datasource:        #配置数据源,必配,否则项目启动报错type: com.zaxxer.hikari.HikariDataSourceurl: jdbc:oracle:thin:@localhost:1521:ORCLdriverClassName: oracle.jdbc.driver.OracleDriverusername: SYSTEMpassword: password   #oracle数据库SYSTEM用户登陆密码eureka:client:serviceUrl:defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/  #服务中心注册地址instance:hostname: localhostinstance-id: cloud-web  #实例注册到eureka服务器上的唯一实例IDprefer-ip-address: true   #显示IP地址lease-renewal-interval-in-seconds: 30 #过多长时间发送心跳给eureka服务器,表明它仍然活着,默认为30s

注意:服务消费方方也要注册到注册中心去,不然调用其他微服务时会报No Instances Available for eurekaClient 异常,导致调用微服务失败,http接口报500错误

2) application.yaml

server:servlet:context-path: /myAppsession:cookie:name: customCookiemax: 30mconnection-timeout: 30000ms
spring:profiles:active: devapplication:name: myApplicationjpa:show-sql: truehibernate:ddl-auto: updateproperties:hibernate:dialect: org.hibernate.dialect.Oracle10gDialectlogging:level:org:hibernate:SQL: debugtype:descriptor:sql: trace

4 服务提供方cloud-eureka-client1和cloud-eureka-client2项目开发Restful接口

分别提供一个证明负载均衡和从数据库查询数据的接口,mysql表用的之前建的userinfo表,持久层框架用的spring-data-jpa框架,代码如下

4.1 Controller层代码

package com.hsf.cloudeurekaclient1.controller;import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hsf.cloudeurekaclient2.model.UserInfo;
import com.hsf.cloudeurekaclient2.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class DemoController {private static final Logger logger = LoggerFactory.getLogger(DemoController.class);@Value("${eureka.instance.hostname}")private String hostname;@Value("${server.port}")private String port;@Autowiredprivate UserInfoService userInfoService;private static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();@RequestMapping(path="/remoteIndex",method= RequestMethod.GET)public String Index(){return hostname+":"+port;}@RequestMapping(path="/userInfo",method = RequestMethod.GET)public ResponseEntity<UserInfo> queryUserInfo(@RequestParam("userAccount") String userAccount){logger.info("/userInfo request begin:----------");UserInfo userInfo = userInfoService.findByUserAccount(userAccount);String jsonInfo = gson.toJson(userInfo);logger.info("userInfo:"+jsonInfo);ResponseEntity<UserInfo> entity = new ResponseEntity(userInfo, HttpStatus.OK);return entity;}}

4.2 Service层代码

1)  接口类

package com.hsf.cloudeurekaclient1.service;import com.hsf.cloudeurekaclient1.model.UserInfo;public interface UserInfoService {UserInfo findByUserAccount(String userAccount);}

2) 实现类

package com.hsf.cloudeurekaclient1.service.impl;import com.hsf.cloudeurekaclient1.dao.UserInfoRepository;
import com.hsf.cloudeurekaclient1.model.UserInfo;
import com.hsf.cloudeurekaclient1.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class UserInfoServiceImpl implements UserInfoService {@Autowiredprivate UserInfoRepository userDao;@Overridepublic UserInfo findByUserAccount(String userAccount) {return userDao.findByUserAccount(userAccount);}
}

4.3 Dao层代码

package com.hsf.cloudeurekaclient1.dao;import com.hsf.cloudeurekaclient1.model.UserInfo;
import org.springframework.data.repository.CrudRepository;public interface UserInfoRepository extends CrudRepository<UserInfo,Integer> {UserInfo findByUserAccount(String userAccount);}

cloud-eureka-client2项目的Restful接口开发代码同上,可知直接复制过来

代码结构如下图所示

4.4 实体类代码

package com.hsf.cloudeurekaclient1.model;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;@Entity
@Table(name="userinfo")
public class UserInfo implements Serializable {@Idprivate Integer Id;@Column(name="user_account")private String userAccount;private String password;@Column(name="nick_name")private String nickName;@Column(name="dept_no")private Integer deptNo;@Column(name="email_address")private String emailAddress;@Column(name="birth_day")private Date birthDay;//省略set,get方法
}

4.5 cloud-web项目启动类

package com.hsf.samplesimpl;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class MySpringApplication {private static final Logger logger = LoggerFactory.getLogger(MySpringApplication.class);public static void main(String[] args) {SpringApplication application = new SpringApplication(MySpringApplication.class);application.run(args);}@Bean@LoadBalanced    //客户端添加支持负载均衡注解public RestTemplate restTemplate(){return  new RestTemplate();}}

5 服务消费方接口开发

5.1 采用RestTemplate调用微服务

1) Controller层代码

IUserService接口

package com.hsf.samplesimpl.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;@RestController
public class UserController extends BaseController {@Autowiredprivate RestTemplate restTemplate;    //注入RestTemplate bean实例@Value("${rpcServices.serviceName}")   //取自配置文件的远程微服务名private String serviceName;@Value("${rpcServices.contextPath}")   //微服务上下文路径private String contextPath;@Value("${rpcServices.indexUrl}")     // 验证负载均衡URLprivate String indexUrl;//验证客户端调用微服务实现了负载均衡接口@RequestMapping(path="/localIndex",method = RequestMethod.GET)public String index(){logger.info("/localIndex request start");StringBuilder builder = new StringBuilder("http://");builder.append(serviceName).append(contextPath).append(indexUrl);String uri = builder.toString();logger.info("uri:"+uri);ResponseEntity<String> entity = restTemplate.getForEntity(uri,String.class);return entity.getBody();}// 验证从微服务提供方数据库获取数据接口@RequestMapping(path="/remoteUserInfo",method = RequestMethod.GET)public ResponseEntity<MySqlUserInfo> queryMysqlUserInfo(@RequestParam("userAccount") String userAccount) {logger.info("/remoteUserInfo request start");logger.info("userAccount:"+userAccount);StringBuilder builder = new StringBuilder("http://");builder.append(serviceName).append(contextPath).append(userInfoUrl).append("? userAccount={userAccount}");String uri = builder.toString();logger.info("uri:"+uri);Map<String,String> paramMap = new HashMap<>();paramMap.put("userAccount",userAccount);ResponseEntity<MySqlUserInfo> entity =  restTemplate.getForEntity(uri,MySqlUserInfo.class,paramMap);return entity;}}

MysqlUserInfo实体类

package com.hsf.samplesimpl.model;import java.io.Serializable;
import java.util.Date;public class MySqlUserInfo implements Serializable {private Integer Id;private String userAccount;private String password;private String nickName;private Integer deptNo;private String emailAddress;private Date birthDay;//此处省略set和get方法
}

2) 在IDEA中启动两个Eureka服务端项目和Eureka客户端项目后,再启动服务消费方cloud-web项目,均为Debug模式启动,Eureka服务端和客户端以及服务消费方项目启动成功后如下图所示:

3) 在来看一下服务注册中心的服务实例信息

我们发现cloud-web项目的实例也注册到了服务注册中心上来了

4) postman测试RestTemplate调用eurekaClient服务的/remoteIndex接口验证客户端负载均衡

第一次调用效果图

第二次调用效果图

连续调用循环出现9090和9091端口,验证了客户端负载均衡

5) postman测试RestTemplate调用eurekaClient服务的获取mysql数据库用户信息接口

5.2 采用Fengin调用微服务

1)  Fengin服务接口层代码

package com.hsf.samplesimpl.service;import com.hsf.samplesimpl.model.MySqlUserInfo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;@FeignClient(name="eurekaClient")    //name对应服务提供方的服务名,也就是yaml配置文件中spring.application.name对应的value值
public interface FeignService {//RequestMapping 注解中的value值对用url映射,method对应请求方法,根据官方文档提示这里不能用GetMapping注解@RequestMapping(value = "/eurekaClient/remoteIndex",method = RequestMethod.GET)String getUrl();@RequestMapping(value="/eurekaClient/userInfo",method = RequestMethod.GET)MySqlUserInfo getUserInfo(@RequestParam("userAccount") String userAccount);}

2) Controller层代码

UserController类中注入FenginService实例并新增两个Fengin调用的接口
@Resource
private FeignService feignService;@GetMapping(value="/remoteUrl")public String getRemoteUri(){return feignService.getUrl();}@GetMapping(value="/feignUserInfo")public MySqlUserInfo getRemoteUserInfo(String userAccount){return  feignService.getUserInfo(userAccount);}

3) postman测试Fengin调用eurekaClient微服务的验证负载均衡接口

4) postman测试Fengin调用eurekaClient微服务获取Mysql数据库用户信息接口

总结:

  • 本文采用spring cloud的spring-cloud-netflix-eureka-server模块搭建了两个服务注册中心,实现注册中心高可用,服务注册中心项目的启动类需要加上@EnableEurekaServer注解使当前服务成为一个服务注册中心;
  • 采用spring-cloud-netflix-eureka-client模块搭建了微服务提供方项目和微服务调用方项目,并且微服务提供方项目构建了两个实例以验证客户端负载均衡,微服务客户端项目的启动类上都要加上@EnableEurekaClient注解或者@DiscoveryClient注解,让当前项目启动后其服务注册到注册中心;
  • 通过将各自系统的服务注册到服务注册中心,不同系统之间可以实现系统代码无耦合地调用,获取其他注册到注册中心服务的接口,从而可以方便地通过服务名调用不通系统不通数据源的接口;
  • 需要注意的是无论是服务注册中心、服务提供方还是服务消费方都需要配置数据源项目启动时才不会报错,无论是服务提供方还是服务消费方都需要注册到Eureka注册中心上去才能实现微服务的正常调用

spring-cloud开发微服务笔记(二):高可用Eureka注册中心的搭建与RestTemplate和Fengin客户端调用微服务示例相关推荐

  1. 高可用Eureka注册中心配置说明(双机部署)

    目  录 1. 高可用EureKa注册中心示意图 2. Eureka实例相互注册配置 3. 微服务注册到Eureka配置 4. 启动步骤及配置成功检查 5. 说明事项 1. 高可用EureKa注册中心 ...

  2. Spring Cloud Eureka(三)实现一个高可用的注册中心

    Spring Cloud Eureka(三)实现一个高可用的注册中心 实现一个高可用的注册中心 在微服务结构这样的分布式环境中,我们需要充分考虑发生故障的情况,所以在生产环境中必须为服务的各个组件进行 ...

  3. SpringCloud系列二:Restful 基础架构(搭建项目环境、创建 Dept 微服务、客户端调用微服务)...

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:Restful 基础架构 2.具体内容 对于 Rest 基础架构实现处理是 SpringCloud 核心所在,其基本操 ...

  4. SpringCloud 搭建项目环境、创建 Dept 微服务、客户端调用微服务

    对于 Rest 基础架构实现处理是 SpringCloud 核心所在,其基本操作形式在 SpringBoot 之中已经有了明确 的讲解,那么本次为 了清晰可见,创建一套新的微服务架构:部门微服务(De ...

  5. EurekaServer高可用的注册中心集群搭建

    转载请注明出处:https://www.cnblogs.com/mahongchao/p/9773586.html 1.创建springboot工程,工程目录如下: 2.添加gradle依赖 depe ...

  6. 走进Spring Cloud之二 eureka注册中心(Greenwich版本)

    走进Spring Cloud之二 eureka注册中心(Greenwich版本) eureka 构建SpringCloud 工程 eureka 注册中心 eureka-server moudle po ...

  7. Spring Cloud 第一天课堂笔记

    1. 系统架构演变概述 目标:了解项目架构的演变历程 小结: #mermaid-svg-jDcYEZKjHiRrxSmv .label{font-family:'trebuchet ms', verd ...

  8. 03-搭建Eureka注册中心和服务端

    Eureka注册中心的搭建 首先以7001为例 首先导入依赖 <dependencies><!-- https://mvnrepository.com/artifact/org.sp ...

  9. 【微服务】Eureka注册中心

    文章目录 前置导入 Eureka的结构和作用 搭建eureka-server 创建eureka-server服务 引入eureka依赖 编写启动类 编写配置文件 启动服务 总结 服务注册 引入依赖 配 ...

最新文章

  1. INotifyPropertyChanged 接口 CallerMemberName属性
  2. strtok函数取WinMain的参数
  3. AQS.transferForSignal
  4. linux7开启ntp服务,【NTP】CentOS7.2配置NTP服务
  5. TSPITR方式数据库找回误操作丢失的数据
  6. 使用webpack打包后的vue项目如何运行(express)
  7. 辽宁省计算机辅助普通话水平测试应试指南,计算机辅助普通话水平测试 应 试 指 南...
  8. keybd_event、SendInput笔记
  9. 如何备份服务器日志到其他服务器_sql2008自动备份到ftp服务器,sql2008自动备份到ftp服务器代码公布...
  10. [转] 面向对象编程 - 继承和多态
  11. 通过DriverManager接口获取连接
  12. php echo substr('hello',1,-2);-2是什么意思 为什么结果是 el
  13. Win10账户锁定的解除方法
  14. web网页常见特效3——轮播图
  15. IMRAM: Iterative Matching with Recurrent Attention Memory for Cross-Modal Image-Text Retrieval
  16. RocketMQ的长轮询消费方式
  17. 什么是独立站,独立站的作用是什么?
  18. 前缀、真前缀、后缀、真后缀
  19. 2019年3月全国计算机二级考试试题,(完整版)2019年全国计算机二级考试试题题库(附答案)...
  20. 从头到脚说单测——谈有效的单元测试(下篇)

热门文章

  1. POJ 3618 - Exploration 不一样的排序解决办法
  2. CSS浮动的基本概念和运用
  3. CTFshow月饼杯(第二届) 中秋快乐 套套去哪儿出题思路+解法
  4. 面试流程及常见面试题
  5. Linux eth0网络设备无法激活解决办法
  6. springMVC源码分析--@SessionAttribute用法及原理解析SessionAttributesHandler和SessionAttributeStore
  7. 家常六菜一汤(蒜薹肉丝、凉拌黄瓜丝、红烧猪蹄、酸辣白菜、烤鸡翅、清蒸鲈鱼和鱼头豆腐汤)...
  8. 农历查询API接口说明,农历API
  9. (织梦cms)dedecms5.7注入和上传0day
  10. linux系统安装内存测试,一种Linux系统下基于IDK内存注错的测试方法及系统与流程...