玩转SpringCloud(F版本) 一.服务的注册与发现(Eureka)

一.服务的注册与发现(Eureka

spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等

 

1. 主项目:版本控制

 

Springboot集中声明

 

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.4.RELEASE</version>
  <relativePath/>
</parent>

 

Springcloud版本

 

<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>

 

主项目管理的依赖Jar

 

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

 

Springboot组件:

 

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

 

2.创建2model工程

一个model工程作为服务注册中心,即Eureka Server,另一个作为Eureka Client。

1创建服务注册中心Eureka Server 

项目架构:

 

 

引入主项目:

 

<parent>
   <groupId>com.fsdm</groupId>
   <artifactId>SpringCloud_test1</artifactId>
   <version>1.0-SNAPSHOT</version>
</parent>

 

引入spring-cloud-starter-netflix-eureka-server的依赖:

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

Application启动类:

@EnableEurekaServer
@SpringBootApplication
public class Demo1Application {

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

 

注解解析:

@SpringBootApplication

1. 复合注解主要包括@ComponentScan,和@SpringBootConfiguration@EnableAutoConfiguration

2. @SpringBootConfiguration标注当前类是配置类

3. @EnableAutoConfiguration启动自动的配置根据你添加的jar包来配置你项目的默认配置

4. @EnableAutoConfiguration扫描当前包及其子包下被@Component@Controller@Service@Repository注解标记的类并纳入到spring容器中进行管理

 

@EnableEurekaServer

1. 表明这是一个EurekaServer服务注册中心

2. 配合yml文件使用:

Eureka:
  Client:
    registerWithEureka: false
    fetchRegistry: false

 

 

yml配置:

 

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
  #  表明自己是一个eureka server.
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  application:
    name: eurka-server

 

启动程序,访问http://localhost:8761/

client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。

 

No application available 表示没有服务被发现

因为我们还没有注册服务当然没有发现服务啦,

 

 

2)创建一个服务提供者 (eureka client)

项目架构:

 

引入主项目:

 

<parent>
   <groupId>com.fsdm</groupId>
   <artifactId>SpringCloud_test1</artifactId>
   <version>1.0-SNAPSHOT</version>
</parent>

 

需要的jar以及组件:

 

<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>

 

Application启动类:

 

@SpringBootApplication
@EnableEurekaClient
@RestController
public class Demo2Application {

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

   @Value("${server.port}")
   String port;
   @RequestMapping("/hi")
   public String home(@RequestParam String name) {
      return "hi "+name+",i am from port:" +port;
   }


}

 

注解解析:

@EnableEurekaClient

1. 表明这是一个EurekaClient服务提供者

 

2. 基于spring-cloud-netflix,只能为eureka作用。

 

 

@RestController

1. 复合注解主要包括@Controller@ResponseBody

2. 标注controller层,可供url访问

3. 无法返回jsp页面,或者html,配置的视图解InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。

 

yml配置:

 

server:
  port: 8762

spring:
  application:
#工程名称
    name: service-hi

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

 

需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。

 

 

启动这个项目,刷新http://localhost:8761/

发现一个服务已经注册在服务中了,服务名为SERVICE-HI ,端口为8762

你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI

关于红色字体的警告解释:

https://www.cnblogs.com/breath-taking/articles/7940364.html

 

访问http://localhost:8762/hi?name=fsdm

 

 

                                        未完,待续。。。

posted @ 2018-09-09 15:30  房上的猫  阅读(7760)  评论(2编辑  收藏  举报