spring-cloud学习(一)———eureka集群服务注册于与发现

Eureka

1.服务端-集群
1.集群主机1
    server:
      port: 7001
    
    eureka:
      instance:
        hostname: eureka7001.com  #eureka服务端的实例名称  host文件映射本机
      client:
        register-with-eureka: false     #false表示不向注册中心注册自己。
        fetch-registry: false         #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
        service-url:
          #单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
          #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
          defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
          

2.集群主机2
    server:
      port: 7002
    
    eureka:
      instance:
        hostname: eureka7002.com  #eureka服务端的实例名称  host文件映射本机
      client:
        register-with-eureka: false     #false表示不向注册中心注册自己。
        fetch-registry: false           #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
        service-url:
          #单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
          #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
    


3.集群主机3
    server:
      port: 7003
    
    eureka:
      instance:
        hostname: eureka7003.com #eureka服务端的实例名称  host文件映射本机
      client:
        register-with-eureka: false     #false表示不向注册中心注册自己。
        fetch-registry: false           #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
        service-url:
          #单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
          #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
    

4.pom
    <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>

5.主启动类

    @SpringBootApplication
    @EnableEurekaServer
2.服务提供(注册)
1.服务提供者1
    server:
      port: 8001
    
    mybatis:
      type-aliases-package: com.qin.eurekaprovider2.entities    # 所有Entity别名类所在包
      mapper-locations:
        - classpath:mybatis/mapper/**/*.xml                     # mapper映射文件
    
    spring:
      application:
        name: eureka-provider
      datasource:
        url: jdbc:mysql://localhost:3306/cloudDB01              # 当前数据库
        username: root
        password: 123456
        driver-class-name: com.mysql.jdbc.Driver
    
    eureka:
      client: #客户端注册进eureka服务列表内
        service-url:
          #defaultZone: http://localhost:7001/eureka
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
      instance:
        instance-id: eureka-provider8001
        prefer-ip-address: true     #访问路径可以显示IP地址
 
2.服务提供者2
    server:
      port: 8002
    
    mybatis:
      type-aliases-package: com.qin.eurekaprovider2.entities    # 所有Entity别名类所在包
      mapper-locations:
        - classpath:mybatis/mapper/**/*.xml                       # mapper映射文件
    
    spring:
      application:
        name: eureka-provider
      datasource:
        url: jdbc:mysql://localhost:3306/cloudDB02             # 当前数据库
        username: root
        password: 123456
        driver-class-name: com.mysql.jdbc.Driver
      mvc:
        view:
          prefix: /templates/
          suffix: .html
    
    eureka:
      client: #客户端注册进eureka服务列表内
        service-url:
          #defaultZone: http://localhost:7001/eureka
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
      instance:
        instance-id: eureka-provider8002
        prefer-ip-address: true     #访问路径可以显示IP地址
    
    
3.服务提供者3
    server:
      port: 8003
    
    mybatis:
      type-aliases-package: com.qin.eurekaprovider3.entities    # 所有Entity别名类所在包
      mapper-locations:
        - classpath:mybatis/mapper/**/*.xml                        # mapper映射文件
    
    spring:
      application:
        name: eureka-provider
      datasource:
        url: jdbc:mysql://localhost:3306/cloudDB03               # 当前数据库
        username: root
        password: 123456
        driver-class-name: com.mysql.jdbc.Driver
    
    eureka:
      client: #客户端注册进eureka服务列表内
        service-url:
          #defaultZone: http://localhost:7001/eureka
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
      instance:
        instance-id: eureka-provider8003
        prefer-ip-address: true     #访问路径可以显示IP地址

4.pom
     <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
5.主启动类
    @SpringBootApplication
    @EnableEurekaClient //eureka客户端
    @EnableDiscoveryClient  //发现服务  包括eureka 
3.消费者
3.1.yaml
    server:
      port: 6767
    
    
    eureka:
      client:
        register-with-eureka: false
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/   

3.2.pom
     <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

3.3.主启动类
    package com.qin.eurekaconsumer;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    @SpringBootApplication
    @EnableEurekaClient
    public class EurekaConsumerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaConsumerApplication.class, args);
        }
    
        @Bean
        @LoadBalanced  //负载均衡
        public RestTemplate getRestTemplate()  
        {
            return new RestTemplate();
        }
    }
  

3.4.controller
    package com.atguigu.springcloud.controller;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    import com.atguigu.springcloud.entities.Dept;
    
    @RestController
    public class DeptController_Consumer
    {
    
    	//private static final String REST_URL_PREFIX = "http://localhost:8001";
        //通过服务名来获取服务消费
    	private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-DEPT";
    
    	/**
    	 * 使用 使用restTemplate访问restful接口非常的简单粗暴无脑。 (url, requestMap,
    	 * ResponseBean.class)这三个参数分别代表 REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。
    	 */
    	@Autowired
    	private RestTemplate restTemplate;
    
    	@RequestMapping(value = "/consumer/dept/add")
    	public boolean add(Dept dept)
    	{
    		return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
    	}
    
    	@RequestMapping(value = "/consumer/dept/get/{id}")
    	public Dept get(@PathVariable("id") Long id)
    	{
    		return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
    	}
    
    	@SuppressWarnings("unchecked")
    	@RequestMapping(value = "/consumer/dept/list")
    	public List<Dept> list()
    	{
    		return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
    	}
    
    	// 测试@EnableDiscoveryClient,消费端可以调用服务发现
    	@RequestMapping(value = "/consumer/dept/discovery")
    	public Object discovery()
    	{
    		return restTemplate.getForObject(REST_URL_PREFIX + "/dept/discovery", Object.class);
    	}
    
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值