起两个cloud集群
mycloud,mycloud1
pom.xml都加以下配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.1.1.RELEASE</version>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在host中分配配置:
127.0.0.1 e1
127.0.0.1 e2
mycloud的配置
server.port=1111
eureka.instance.hostname=e1
spring.application.name=mycloud
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://e1:1111/eureka/,http://e2:4444/eureka/
feign.hystrix.enabled=true
mycloud1的配置
server.port=4444
eureka.instance.hostname=e1
spring.application.name=mycloud
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://e1:1111/eureka/,http://e2:4444/eureka/
feign.hystrix.enabled=true
两个cloud的启动文件分别加@EnableEurekaServer
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@EnableEurekaServer
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
起两个service
myservice和myservice1
pom.xml都以下配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
两个service都加以下启动配置@EnableDiscoveryClient:
@Configuration
@ComponentScan("com.gy")
@EnableAutoConfiguration
@EnableFeignClients("com.gy.api")
/*@EnableScheduling*/
@EnableDiscoveryClient
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
分别新建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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/sys/")
public class Test1Controller {
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/test" ,method = RequestMethod.GET)
public String add(@RequestParam("t") String t) {
ServiceInstance instance = client.getLocalServiceInstance();
t = "result:"+t;
System.out.println("myuri:"+instance.getUri()+" myhost:" + instance.getHost() + ", myserviceid:" + instance.getServiceId() + ", t:" + t);
return t;
}
}
application分别以下配置
spring.application.name=compute-service
server.port=2222
eureka.client.serviceUrl.defaultZone=http://e1:1111/eureka/,http://e2:4444/eureka/
spring.application.name=compute-service
server.port=5555
eureka.client.serviceUrl.defaultZone=http://e1:1111/eureka/,http://e2:4444/eureka/
起一个client
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
//http://start.spring.io/
@Configuration
@ComponentScan("com.gy")
@EnableAutoConfiguration
@EnableFeignClients("com.gy.api")
/*@EnableScheduling*/
/*@EnableDiscoveryClient*/
/*@EnableHystrix*/
@EnableDiscoveryClient//重要,这个要加下
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
新建一个api
package com.gy.api;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient("compute-service")//这里直接用名称,这样才能起到集群的效果,千万不能是地址,切记!
public interface Ser1Api {
@RequestMapping(value = "/sys/test" ,method = RequestMethod.GET)
public String add(@RequestParam("t") String t);
}
新建一个controller
package com.gy.controller;
import com.gy.api.Ser1Api;
import com.gy.service.TestService;
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;
@RestController
@RequestMapping(value = "")
public class TestController {
@Autowired
private Ser1Api ser1api;
@Autowired
private TestService testService;
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/t1", method = RequestMethod.POST)
public String t1() {
String r = restTemplate.getForEntity("http://COMPUTE-SERVICE/sys/test?t=chs", String.class).getBody();//第一次调用
String t = ser1api.add("chs");//第二次调用
return t;
}
}
application配置
spring.application.name=feign-consumer
server.port=7777
eureka.client.serviceUrl.defaultZone=http://e1:1111/eureka/,http://e2:4444/eureka/
eureka.client.register-with-eureka=false
feign.hystrix.enabled=true
全部代码上齐了,
现在先分别启动mycloud和mycloud1
然后访问
http://localhost:3333/t1(运行两次)
可以发现:打印:result:chs
这个时候到myservice或myservice1各访问两次,分别会看到
myuri:http://70HMWKC61BOKA6H:2222 myhost:70HMWKC61BOKA6H, myserviceid:compute-service, t:result:chs
mycloud,mycloud1
pom.xml都加以下配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.1.1.RELEASE</version>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在host中分配配置:
127.0.0.1 e1
127.0.0.1 e2
mycloud的配置
server.port=1111
eureka.instance.hostname=e1
spring.application.name=mycloud
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://e1:1111/eureka/,http://e2:4444/eureka/
feign.hystrix.enabled=true
mycloud1的配置
server.port=4444
eureka.instance.hostname=e1
spring.application.name=mycloud
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://e1:1111/eureka/,http://e2:4444/eureka/
feign.hystrix.enabled=true
两个cloud的启动文件分别加@EnableEurekaServer
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@EnableEurekaServer
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
起两个service
myservice和myservice1
pom.xml都以下配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
两个service都加以下启动配置@EnableDiscoveryClient:
@Configuration
@ComponentScan("com.gy")
@EnableAutoConfiguration
@EnableFeignClients("com.gy.api")
/*@EnableScheduling*/
@EnableDiscoveryClient
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
分别新建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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/sys/")
public class Test1Controller {
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/test" ,method = RequestMethod.GET)
public String add(@RequestParam("t") String t) {
ServiceInstance instance = client.getLocalServiceInstance();
t = "result:"+t;
System.out.println("myuri:"+instance.getUri()+" myhost:" + instance.getHost() + ", myserviceid:" + instance.getServiceId() + ", t:" + t);
return t;
}
}
application分别以下配置
spring.application.name=compute-service
server.port=2222
eureka.client.serviceUrl.defaultZone=http://e1:1111/eureka/,http://e2:4444/eureka/
spring.application.name=compute-service
server.port=5555
eureka.client.serviceUrl.defaultZone=http://e1:1111/eureka/,http://e2:4444/eureka/
起一个client
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
//http://start.spring.io/
@Configuration
@ComponentScan("com.gy")
@EnableAutoConfiguration
@EnableFeignClients("com.gy.api")
/*@EnableScheduling*/
/*@EnableDiscoveryClient*/
/*@EnableHystrix*/
@EnableDiscoveryClient//重要,这个要加下
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
新建一个api
package com.gy.api;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient("compute-service")//这里直接用名称,这样才能起到集群的效果,千万不能是地址,切记!
public interface Ser1Api {
@RequestMapping(value = "/sys/test" ,method = RequestMethod.GET)
public String add(@RequestParam("t") String t);
}
新建一个controller
package com.gy.controller;
import com.gy.api.Ser1Api;
import com.gy.service.TestService;
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;
@RestController
@RequestMapping(value = "")
public class TestController {
@Autowired
private Ser1Api ser1api;
@Autowired
private TestService testService;
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/t1", method = RequestMethod.POST)
public String t1() {
String r = restTemplate.getForEntity("http://COMPUTE-SERVICE/sys/test?t=chs", String.class).getBody();//第一次调用
String t = ser1api.add("chs");//第二次调用
return t;
}
}
application配置
spring.application.name=feign-consumer
server.port=7777
eureka.client.serviceUrl.defaultZone=http://e1:1111/eureka/,http://e2:4444/eureka/
eureka.client.register-with-eureka=false
feign.hystrix.enabled=true
全部代码上齐了,
现在先分别启动mycloud和mycloud1
再分别启动myservice和myservice1
这时候可以看到:
这个时候两个cloud注册中心就起来了,而且都有服务,当其中一个注册中心断了,也一样可以看到另外一个列表中还存在,
service也一样,只要有一个或多个同时运行,不会因为其中一个挂掉而断开链接!
然后访问
http://localhost:3333/t1(运行两次)
可以发现:打印:result:chs
这个时候到myservice或myservice1各访问两次,分别会看到
myuri:http://70HMWKC61BOKA6H:2222 myhost:70HMWKC61BOKA6H, myserviceid:compute-service, t:result:chs
myuri:http://70HMWKC61BOKA6H:2222 myhost:70HMWKC61BOKA6H, myserviceid:compute-service, t:result:chs
说明轮询访问服务service 了