您的位置:首页 > 编程语言 > Java开发

SpringCloud组件之(Eureka)的注册服务与发现服务的实践

2018-03-28 09:33 836 查看
吐槽:不得不说,学完了Springboot,,发现没学到啥玩应,,将白了就是正常的单体应用多了一个容器,默认给你集成好了tomcat的容器,利用maven的打包方式,管你是几个model的工程,还是单独的model的工程,(ps:吐槽下IDEA和Eclipse的设计思想,能不能吸收点对方好处,IDEA要是吸收了Eclipse的多模块建立工程,独立工作环境的模式,会不会更好一点,eclipse要是吸收了IDEA的代码提示功能,和多个项目开发的功能是不是更完美,仅仅个人吐槽,欢迎喷我),打成jar包,在服务器里面,一句“java
-jar xxxx" ,就能运行起来这个项目了,啧啧,,是不是刚刚学习的同学都觉得美滋滋,不用xml配置,不用繁琐的打包方式,不用。。。。。反正就是简单是不是?????但是!!!但是!!难道忘记了当初我们用dubbo时候,用zookeper时候服务进行注册,分发,负载均衡的时候了么??那时候是单体应用么???不是吧,那就来了,,我们用了Springboot难道还要弄个dubbo或者zookeper这种东西去集成???反正作为一个懒人,懒到极致的人,我是觉得,不想,非常非常不想,那么就扔掉那一套不是亲生儿子的框架吧,扎身于Springcloud的知识中吧,,,

首先,我想先来个总结,我这个人,可能比较好高骛远,,(ps:就是就是),所以导致学习知识时候,我希望掌握所有的东西,都大概了解了一下的,然后在逐个击破,例如这个Springcloud是个啥,,刚刚开始我还以为是个框架,后来发现,,这特么不是框架,是插件,一堆的插件,而且是基于Springboot的,学吧,然后发现了eureka这个东西,简单一句话:eureka就是负责,服务的注册和使用,举个例子就是:个人<client>--中介<servercenter>--卖房子的个人<server>), 所以,,根据我们以前的经验,,我们要有三个东西,,,其实也是两个东西,,一个是服务中心,另一个是客户端,(ps:成对儿出现的)所以呢,,,废话不多说直接开撸

第一步:建立三个普通的项目

使用maven建立simple的三个mavenproject,

第一个:起名叫做,,,,first-114-,,,对,,你没看错,,为了加强理解,,114就想当于中介了

第二个:起名叫做,,,,first-person,,,对,,就是个人的意思,,为了加强理解,相当于一个服务了

第三个:起名叫做,,,,first-police,,,对,,就是警察局的意思,,为了加强理解,相当于另一个服务了了



然后,说一下个人怎么规划的,,警察在114中心注册了自己的一个联系方式,,然后个人通过找到这个联系方式,与114联系,然后找到警察的这个联系方式

其实就是,其中的一个服务A在服务控制中心注册了,,另一个服务B想要用到服务A里面的内容,通过服务控制中心去找服务A注册的名字,然后并使用

第二步:开始搭建114(服务注册中心这个项目)

1、pom文件下引入Springcloud的依赖:

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--eureka服务器的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>


2、建立服务注册中心的启动类

package yjc.crazy.cloud;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

//Springboot的启动的那个注解,不明白的,先看Springboot的知识去
@SpringBootApplication
//启动eurekaServer的注解
@EnableEurekaServer
public class ServerApp {

public static void main(String[] args) {
new SpringApplicationBuilder(ServerApp.class)
.web(true)
.run(args);
}
}


3、在resources目录下面编写配置文件application.yml文件

server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false


肯定有同学问我这两个配置了fale的东西是啥,,,我就给你在它的文档里面找到了
百度翻译解释了下:(ps:自行理解)

指示此实例是否应该将其信息注册到尤里卡服务器以供其他人发现。

在某些情况下,您不希望您的实例被发现,而您只是想发现其他实例。



/*****************************************到这里第一个服务注册中心的demo就实现了**************************************************************/

第三步:建立服务提供者的一端(警察)
4000

1、在pom文件中引入需要的依赖

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>

2、编写一个启动类

package yjc.crazy.cloud;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class PoliceServer {

public static void main(String[] args) {
new SpringApplicationBuilder(PoliceServer.class).web(true).run(args);
}

}

3、在编写一个实体类

package yjc.crazy.cloud;

public class Police {

private String name;
private Integer id;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}

}

4、在编写一个Controller层
package yjc.crazy.cloud;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PosiceController {

@RequestMapping("call/{id}")
private Police call(@PathVariable Integer id) {
Police police = new Police();
police.setId(id);
police.setName("first eureak");
return police;
}

}

5、在写好配置文件
server:
port: 8081

spring:
application:
name: first-police
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
其实这样就编写好了,并且我们通过正常的请求是可以进行访问的



可以看到这个服务是可以进行使用的了,,

第三步:编写另外一个调用者的服务,就是个人的服务

1、在pom引入依赖

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<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>
</dependencies>

2、编写启动类(其实这个启动类跟警察那个启动类是一个样子的,都是用了SpringbootApplication和EnableEurekaclient的这个注解)
package yjc.crazy.cloud;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class PersonServer {
public static void main(String[] args) {
new SpringApplicationBuilder(PersonServer.class).web(true).run(args);
}
}


3、写一个Controller层来支持访问
package yjc.crazy.cloud;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Configuration
public class TestController {

@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}

@GetMapping("/router")
public String router() {
RestTemplate tpl = getRestTemplate();
String json = tpl.getForObject("http://first-police/call/1", String.class);
return json;
}

}

看到没,这里面的写法就有点有意思了,,,是不是,,用的是RestTemplate这个类,,大家不懂得可以百度看看,涨涨知识,这里面有一个地方就是那个看起来像url的地方,用的竟然不是正儿八经的url,,,竟然是我们刚刚写的那个警察的服务的名字,,看到了么,,,然后后面跟的就是平常写的那个Controller的拦截的字段就可以了,

4、编写配置文件
server:
port: 8082
spring:
application:
name: first-person
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
这里面person这个服务也是需要惊醒注册的才可以

总结:这里面我们其实写了三个Springboot的项目,每一个项目呢,都有一个启动类,只是服务器中心用的注解是EnableEurekaServer这个注解,客户端用的是EnableEurekaClient这个注解,其次就是引入的依赖的不同,不同的地方只有最后一个依赖,,,大家可以看pom文件的东西,,这里放一个对比图



///*********************************************************************************************************************************************************************



可以看到区别,只有这个jar包的依赖是不一样的哦,,,,,,,然后重点,,,!!!!重点!!!!!我们的启动顺序是:

先启动114这个服务,,,在启动警察这个服务,,最后启动个人的这个服务,,,,当我们启动都不报错的时候,我们进入eureka的后台其实是可以看到这两个服务的名字的



好了写到这里基本也就可以了,,然后我会把我的代码上传到github上面,供大家访问下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐