源码:
https://github.com/LM917178900/spring-boot-hello
https://github.com/LM917178900/eureka-server
https://github.com/LM917178900/ribbon-consumer
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eureka-serverartifactId>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Brixton.SR5version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
client即是服务端也是消费端,将client注册到两台注册中心。
spring.application.name=hello-service
# 注册到两台注册中心
eureka.client.service-url.defaultZone=http://peer1:1111/eureka/,http://peer2:1112/eureka
package com.didispace.springboothello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* 能够被发现为客户端
*/
@EnableDiscoveryClient
@SpringBootApplication
public class SpringBootHelloApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootHelloApplication.class, args);
}
}
package com.didispace.springboothello.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.logging.Logger;
/**
* @Author: leimin
* @Description: new class
* @Date: 2020/6/8 9:08
* @Version: 1.0
*/
@RestController
public class HelloController {
/**
* xx
*/
private final Logger logger = Logger.getLogger(String.valueOf(getClass()));
/**
* xx
*/
@Autowired
private DiscoveryClient client;
/**
* xx
* @return xx
*/
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String index(){
ServiceInstance instance = client.getLocalServiceInstance();
logger.info("/hello,host:" + instance.getHost()+", service_id:" +instance.getServiceId());
return "hello World !";
}
}
不同shell窗口,同时启动两个实例
java -jar ./spring-boot-hello-0.0.1.SANPSHOT.jar --server.port=8081
java -jar ./spring-boot-hello-0.0.1.SANPSHOT.jar --server.port=8082
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eureka-serverartifactId>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Brixton.SR5version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
配置两个application,相互注册检索,实现注册同步和高可用,分别是:
application-peer1.properties
# 自己是注册中心,当成客户端
eureka.client.register-with-eureka=true
# 需要去检索服务
eureka.client.fetch-registry=true
spring.application.name=eureka-server
server.port=1111
eureka.instance.hostname=peer1
eureka.client.service-url.defaultZone=http://peer2:1112/eureka/
application-peer2.properties
# 自己是注册中心,被当成客户端
eureka.client.register-with-eureka=true
# 需要去检索服务
eureka.client.fetch-registry=true
spring.application.name=eureka-server
server.port=1112
eureka.instance.hostname=peer2
eureka.client.service-url.defaultZone=http://peer1:1111/eureka/
package com.huahua.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* 能够被发现为服务端
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
在不同shell窗口,同时启动两个注册中心。
java -jar ./demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar ./demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-ribbonartifactId>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Brixton.SR5version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
spring.application.name=ribbon-consumer
server.port=9000
# 配置一个就行了,负载均衡
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
package com.example.lei;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonConsumerApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
}
}
package com.example.lei.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @Author: leimin
* @Description: new class
* @Date: 2020/6/8 16:04
* @Version: 1.0
*/
@RestController
public class ConsumerController {
/**
* xx
*/
@Autowired
RestTemplate restTemplate;
/**
* yyy
* @return rr
*/
@RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
private String helloConsumer(){
return restTemplate.getForEntity("http://HELLO-SERVICE/hello",String.class).getBody();
}
}
然后就开始使用,通过consumer调用spring-boot-hello中的接口。