java技术--SpringCloud:Eureka客户端/服务端代码实现(05)

1.首先要创建一个父工程

(1)父工程的packaging为pom类型
(2)父工程中只包含pom配置文件即可,不需要其余东西

2.创建服务注册中心(Eureka服务端)

(1)创建1个model工程:作为服务注册中心,即Eureka Server
(2)下面以创建server为例子,详细说明创建过程如下:本人使用MyEclipse2016
    <1>创建好父工程后,在父工程右键--选择Maven--创建model
    <2>创建完后的工程的pom.xml文件如下:
    <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>
    <artifactId>EurekaServer</artifactId>
    <packaging>jar</packaging>
    <!--这里引用创建的父工程-->
    <parent>
      <groupId>com.bisien</groupId>
      <artifactId>SpringCloud-Parent</artifactId>
      <version>0.0.1-SNAPSHOT</version>
  </parent>
  <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
</project>
(3)启动一个服务注册中心
   <1>只需要一个注解@EnableEurekaServer
   <2>这个注解需要在springboot工程的启动application类上加
   @SpringBootApplication
   @EnableEurekaServer
   public class EurekaServerApplication {
	 public static void main(String[] args) {
		SpringApplication.run(EurekaServerApplication.class, args);}}
(4)创建配置文件application.properties
   <1>默认情况下erureka server也是一个eureka client ,必须要指定一个server
   server.port=8260   #服务端端口号
   eureka.instance.hostname=localhost  #服务端所在IP
   #表示是否将自己注册在EurekaServer上,默认为true
   #由于当前应用就是EurekaServer,所以置为false
   eureka.client.register-with-eureka=false
   #表示表示是否从EurekaServer获取注册信息,默认为true
   #单节点不需要同步其他的EurekaServer节点的数据
   eureka.client.fetch-registry=false 
   #设置Eureka的地址 
   eureka.client.service- url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
  spring.application.name:EurkaServer	
(5)启动工程,打开浏览器访问: http://localhost:8260
  	<1>eureka server 是有界面的
  	<2>第一次启动在Instances currently registered with Eureka下显示:
  	   2.1.No instances available:没有服务被发现 
  	   2.2.因为还没有注册服务不可能有服务被发现

3.创建客户端 (Eureka客户端)

(1)当client向server注册时,它会提供一些元数据
    <1>主机和端口,URL,主页等
(2)Eureka server 从每个client实例接收心跳消息
    <1>如果心跳超时,则通常将该实例从注册server中删除
(3)下面以创建client为例子,详细说明创建过程如下:本人使用MyEclipse2016 
    <1>创建好父工程后,在父工程右键--选择Maven--创建model
    <2>创建完后的工程的pom.xml文件如下:    
    <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>
   <artifactId>EurekaClient</artifactId>
  <packaging>jar</packaging>
  <parent>
    <groupId>com.bisien</groupId>
    <artifactId>SpringCloud-Parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  </project> 
(4)启动一个客户端
    <1>通过注解@EnableEurekaClient 表明自己是一个eurekaclient
    @SpringBootApplication
    @EnableEurekaClient
    @RestController
    public class EurekaClientApplication {
	  public static void main(String[] args) {
		SpringApplication.run(EurekaClientApplication.class, args);}
	  @Value("${server.port}")
	  String port;
	  @RequestMapping("/hi")
	  public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
		return "hi " + name + " ,i am from port:" + port;}} 
(5)创建配置文件application.properties
   <1>需要在配置文件中注明自己的服务注册中心的地址
   <2>需要指明spring.application.name,这个很重要
      2.1.在以后的服务与服务之间相互调用一般都是根据这个name 
  server.port=8261
  #http://localhost:8260/eureka/为服务端的访问地址
  eureka.client.service-url.defaultZone=http://localhost:8260/eureka/
  spring.application.name:EurkaClient	
(6)启动工程,打开http://localhost:8260 ,即eureka server 的网址
    <1>会发现一个服务已经注册在服务中了
    <2>服务名为EURKACLIENT ,端口为8261 	

4.至此SpringCloud:Eureka服务端/客户端搭建已完成
5.注意事项

(1)配置文件尽量使用application.properties格式,本人使用.yml格式总出错
(2)必须服务端/客户端都启动后,访问服务端网址才会注册成功
发布了191 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq591009234/article/details/105561132
今日推荐