springcloud学习笔记(二)服务的注册与发现(Eureka)

一、什么是Eureka:

Spring Cloud Netflix的Eureka ,eureka是一个服务注册和发现模块。

二、本次学习使用的开发环境:

idea(2017.2)

jdk1.8

maven3.1.1

springboot1.4.3

三、开始编码 Hello Word

首先创建一个maven 父级项目:

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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xc.springcloud</groupId>
    <artifactId>SpringCloudTest</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>EurekaService</module>
        <module>EurekaService_service</module>
        <module>EurekaService_service2</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


</project>

然后创建一个子项目(Eureka服务端)

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>SpringCloudTest</artifactId>
        <groupId>com.xc.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>EurekaService</artifactId>


    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

    </dependencies>

</project>
启动类:

package com.xc.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;


/**
 * @author xuchuang
 * @updateTime 2017年12月20日 下午3:53:53
 */
@SpringBootApplication
@EnableEurekaServer
public class App {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(App.class, args);
    }



}
@EnableEurekaServer为启用Eureka
然后是配置文件:

application.yml

server:
  port: 8761     #指定服务端口
eureka:
  client:
    registerWithEureka: false  #是否将eureka自身作为应用注册到eureka注册中心
    fetchRegistry: false #为true时,可以启动,但报异常:Cannot execute request on any known server
若想在注册是的时候加入登录帐号密码,完整配置文件如下:

server:
  port: 8761     #指定服务端口
eureka:
  client:
    registerWithEureka: false  #是否将eureka自身作为应用注册到eureka注册中心
    fetchRegistry: false #为true时,可以启动,但报异常:Cannot execute request on any known server
    serviceUrl:
      defaultZone: http://xc:123@localhost:8761/eureka/


security:
    user:
      name: xc
      password: 123
    basic:
      enabled: true

现在我们访问:localhost:8761


输入帐号密码:


可以看到EUREKA的服务端界面,说明ok

接下来创建一个子项目(Eureka客户端)

完整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>SpringCloudTest</artifactId>
        <groupId>com.xc.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>EurekaService_service</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

</project>

启动类:

package com.xc.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xuchuang
 * @updateTime 2017年12月20日 下午3:53:53
 */
@SpringBootApplication
@EnableDiscoveryClient
public class App{


    public static void main(String[] args) throws Exception {
        SpringApplication.run(App.class, args);
    }

}
注意@EnableDiscoveryClient注解
写几个服务:

package com.xc.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xuchuang
 * @updateTime 2017年12月20日 下午3:53:53
 */
@SpringBootApplication
@EnableDiscoveryClient
public class App{


    public static void main(String[] args) throws Exception {
        SpringApplication.run(App.class, args);
    }

}

@RestController
class ServiceInstanceRestController {

	@Autowired
	private DiscoveryClient discoveryClient;

	@RequestMapping("/service-instances/{applicationName}")
	public List<ServiceInstance> serviceInstancesByApplicationName(
			@PathVariable String applicationName) {
		return this.discoveryClient.getInstances(applicationName);
	}

	@RequestMapping("/")
	public String sayhello() {
		return "hello";
	}

	@RequestMapping("/hello")
	public String sayhe() {
		return "hello";
	}
}
配置文件(application.properties 为了学习  yml 和.properties都使用了一下):

user.username=xc

server.port:7070
spring.application.name:cloud-test
eureka.client.serviceUrl.defaultZone:http://xc:123@localhost:8761/eureka/ #指向要注册的eureka
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${server.port}  #可省略

ok 把客户端和服务段跑起来,就可以看到如上图的效果。至此  hello word 完成!

 我们再建一个客户端  port为7071 application.name 为cloud-test2 启动(过程省略),再查看服务端:


可以看到现在eureka发现了两个客户端。











  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值