欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springcloud学习---Eureka_01

程序员文章站 2022-07-13 08:35:02
...

1、搭建Eureka注册中心(之展示核心配置文件以及类)

1.1 application.yml配置文件介绍:

spring:
  application:
    ##实例名称
    name: lfq-eureka

server:
  port: 8761

eureka:
  client:
    ###代表不将自己注册到注册中心服务
    register-with-eureka: false
    ##代表是否从其他注册中心服务获取服务   单节点注册中心不需要
    fetch-registry: false
    ##设置交互地址,查询以及注册都需要这个地址,多个地址用,隔开
    service-url:
      defaultZone: http://localhost:8761/eureka/

1.2 启动类配置介绍:

@EnableEurekaServer//代表当前服务是一个注册中心
@SpringBootApplication
public class MicroserviceDisvoceryEurekaApplication {

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

1.3 pom文件(只展示核心)

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

	<properties>
        <java.version>1.8</java.version>
    </properties>

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

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

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

启动,访问localhost:8761 ,如下所示;
springcloud学习---Eureka_01

2.搭建服务提供者

2.1 application.yml文件介绍:

server:
  port: 8081
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: lfq-provide-8081

2.2 启动类配置:

@EnableDiscoveryClient//代表是一个服务提供者
@SpringBootApplication
public class MicroserviceSimpleProvideUserApplication {

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

}

2.3 编写服务提供接口:

@RestController
@RequestMapping("providedata")
public class ProvideDataController {

    @RequestMapping("getData")
    public String getData(){
        return "hello world!";
    }
}

2.4 pom文件(只展示核心):

	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
	<properties>
        <java.version>1.8</java.version>
    </properties>

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

        <!-- 服务提供者 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

    </dependencies>

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

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

3.编写服务消费者

3.1 application.yml文件介绍:

server:
  port: 8082
  context-path: /

spring:
  application:
    name: lfq-consume-8082
eureka:
  client:
    service-url:
      ##代表注册中心的地址
      defaultZone: http://localhost:8761/eureka/
    ##表示不向注册中心注册自己的服务
    register-with-eureka: false

3.2 启动类配置:

@LoadBalanced,详细介绍请看: https://www.cnblogs.com/lishangzhi/p/11861026.html

@EnableEurekaClient//代表自己是一个客户端
@SpringBootApplication
public class MicroserviceSimpleConsumeUserApplication {

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

    @Bean
    @LoadBalanced//这个注解会自动构造LoadBalancerClient接口的实现类并注册到ioc,实现注册中心负载均衡,详细介绍请看
    public RestTemplate getR(){
        return new RestTemplate();
    }
}

3.3 消费接口:

@RestController
@RequestMapping("consume")
public class ConsumeController {

    @Autowired
    public RestTemplate restTemplate;

    @RequestMapping("getProvideData")
    public String getProvideData(){
        return restTemplate.getForObject("http://LFQ-PROVIDE-8081/providedata/getData",String.class);
    }

}

3.4 pom文件(只展示核心):

	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
	<properties>
        <java.version>1.8</java.version>
    </properties>

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

        <!-- 服务提供者 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>

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

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

3.5 访问:

访问http://localhost:8082/consume/getProvideData:
springcloud学习---Eureka_01