集群之 -- spring cloud eureka 集群代码

24 篇文章 0 订阅
5 篇文章 0 订阅
起两个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

再分别启动myservice和myservice1

这时候可以看到:





这个时候两个cloud注册中心就起来了,而且都有服务,当其中一个注册中心断了,也一样可以看到另外一个列表中还存在,

service也一样,只要有一个或多个同时运行,不会因为其中一个挂掉而断开链接!


最后启动myclient
然后访问
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 了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值