Spring Cloud Learning(一): 服务注册

2021-07-11 15:05

阅读:710

标签:uil   depend   oid   div   中心   必须   blank   情况   reg   

  官网https://projects.spring.io/spring-cloud/,spring cloud官网各组件版本为:

Component Edgware.SR4 Finchley.SR1 Finchley.BUILD-SNAPSHOT
spring-cloud-aws 1.2.3.RELEASE 2.0.0.RELEASE 2.0.1.BUILD-SNAPSHOT
spring-cloud-bus 1.3.3.RELEASE 2.0.0.RELEASE 2.0.1.BUILD-SNAPSHOT
spring-cloud-cli 1.4.1.RELEASE 2.0.0.RELEASE 2.0.1.BUILD-SNAPSHOT
spring-cloud-commons 1.3.4.RELEASE 2.0.1.RELEASE 2.0.2.BUILD-SNAPSHOT
spring-cloud-contract 1.2.5.RELEASE 2.0.1.RELEASE 2.0.2.BUILD-SNAPSHOT
spring-cloud-config 1.4.4.RELEASE 2.0.1.RELEASE 2.0.2.BUILD-SNAPSHOT
spring-cloud-netflix 1.4.5.RELEASE 2.0.1.RELEASE 2.0.2.BUILD-SNAPSHOT
spring-cloud-security 1.2.3.RELEASE 2.0.0.RELEASE 2.0.1.BUILD-SNAPSHOT
spring-cloud-cloudfoundry 1.1.2.RELEASE 2.0.0.RELEASE 2.0.1.BUILD-SNAPSHOT
spring-cloud-consul 1.3.4.RELEASE 2.0.1.RELEASE 2.0.2.BUILD-SNAPSHOT
spring-cloud-sleuth 1.3.4.RELEASE 2.0.1.RELEASE 2.0.2.BUILD-SNAPSHOT
spring-cloud-stream Ditmars.SR4 Elmhurst.SR1 Elmhurst.BUILD-SNAPSHOT
spring-cloud-zookeeper 1.2.2.RELEASE 2.0.0.RELEASE 2.0.1.BUILD-SNAPSHOT
spring-boot 1.5.14.RELEASE 2.0.4.RELEASE 2.0.4.BUILD-SNAPSHOT
spring-cloud-task 1.2.3.RELEASE 2.0.0.RELEASE 2.0.1.BUILD-SNAPSHOT
spring-cloud-vault 1.1.1.RELEASE 2.0.1.RELEASE 2.0.2.BUILD-SNAPSHOT
spring-cloud-gateway 1.0.2.RELEASE 2.0.1.RELEASE 2.0.2.BUILD-SNAPSHOT
spring-cloud-openfeign   2.0.1.RELEASE 2.0.2.BUILD-SNAPSHOT
spring-cloud-function 1.0.0.RELEASE 1.0.0.RELEASE 1.0.1.BUILD-SNAPSHOT

  其中,在使用spring cloud时,需注意spring boot版本,Finchley构建并使用Spring Boot 2.0.x,预计不会与Spring Boot 1.5.x一起使用。

  注意:Dalston发布将于2018年12月达到使用寿命.Eginware将遵循Spring Boot 1.5.x的生命周期结束周期。

  Dalston和Edgware发布版基于Spring Boot 1.5.x构建,预计不会与Spring Boot 2.0.x一起使用。

一、注册与发现

  Spring Cloud使用eureka,  所有需要访问配置文件的应用都作为一个erureka client注册上去。eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送请求,在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。

  maven依赖如下:


         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0com.chm
    springcloud
    0.0.1-SNAPSHOTjarspringcloudSpring cloud eureka server project moduleorg.springframework.boot
        spring-boot-starter-parent
        2.0.4.RELEASEUTF-8UTF-81.8Finchley.SR1org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        org.springframework.cloud
            spring-cloud-starter-config
        org.springframework.boot
            spring-boot-starter-test
            testorg.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}pomimportorg.springframework.boot
                spring-boot-maven-plugin
            spring-milestonesSpring Milestoneshttps://repo.spring.io/milestonefalse

   启动类为:

@EnableEurekaServer
@SpringBootApplication
public class SpringcloudEurekaServerApplication {

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

   配置文件为:

server:
      port: 9999
eureka:
       instance:
           hostname: 127.0.0.1
       client:
            registerWithEureka: false
            fetchRegistry: false
            serviceUrl:
                     defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

  结果报异常为:java.lang.IllegalStateException: Failed to load property source from location ‘classpath:/application.yml‘,需严格注意yml文件格式。

  启动eureka server,然后访问http://localhost:9999,可看到Eureka启动界面。

客户端

  maven依赖如下:

 1  2  3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     4.0.0 5 
 6     com.chm 7     springcloud
 8     0.0.1-SNAPSHOT 9     jar10 
11     springcloud12     Spring cloud eureka client project module13 
14     15         org.springframework.boot16         spring-boot-starter-parent
17         2.0.4.RELEASE18         19     20 
21     22         UTF-823         UTF-824         1.825         Finchley.SR126     27 
28     29         
30         31             org.springframework.cloud32             
33             spring-cloud-starter-netflix-eureka-client
34         35         36             org.springframework.boot37             spring-boot-starter-web
38         39 
40         41             org.springframework.boot42             spring-boot-starter-test
43             test44         45     46 
47     48         49             50                 org.springframework.cloud51                 spring-cloud-dependencies
52                 ${spring-cloud.version}53                 pom54                 import55             56         57     58 
59     60         61             62                 org.springframework.boot63                 spring-boot-maven-plugin
64             65         66     67 
68     
70     71         72             spring-milestones73             Spring Milestones74             https://repo.spring.io/milestone75             76                 false77             78         79     80 
81 

  启动类为:

 1 @SpringBootApplication
 2 @EnableEurekaClient
 3 @RestController
 4 public class SpringcloudEurekaClientApplication {
 5 
 6     public static void main(String[] args) {
 7         SpringApplication.run(SpringcloudEurekaClientApplication.class, args);
 8     }
 9 
10     @Value("${server.port}")
11     String port;
12     @RequestMapping("/")
13     public String home() {
14         return "hello world from port " + port;
15     }
16 
17 }

  配置文件为:

1 eureka:
2   client:
3     serviceUrl:
4       defaultZone: http://localhost:9999/eureka/
5 server:
6   port: 8888
7 spring:
8   application:
9     name: service-eurekaclient

  访问即可发现服务已经注册。

Spring Cloud Learning(一): 服务注册

标签:uil   depend   oid   div   中心   必须   blank   情况   reg   

原文地址:https://www.cnblogs.com/award/p/9550854.html


评论


亲,登录后才可以留言!