【SpringCloud】 Eureka

Eureka

父工程

核心依赖

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>xy</groupId>
    <artifactId>xy-cloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>xy-cloud</name>
    <description>xy-cloud</description>
    <properties>
        <java.version>8</java.version>
        <spring.cloud.version>Hoxton.SR12</spring.cloud.version>
    </properties>
    <packaging>pom</packaging>
    <modules>
        <module>xy-eureka</module>
        <module>xy-consumer</module>
    </modules>

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

    <dependencyManagement>
        <!--引入springcloud依赖的-->
        <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>

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

</project>

服务端

核心依赖

<!--eureka server 依赖坐标-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

配置文件

# 服务端口
server:
  port: 9090

spring:
  # 应用名字,eureka 会根据它作为服务id
  application:
    name: EurekaServer

#  eureka
eureka:
  instance:
    hostname: localhost
  client:
    service-url:   #eureka server 的地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
    register-with-eureka: false # 不向eureka server 注册自己
    fetch-registry: false # 不向eureka server 获取服务列表

启动类

加上@EnableEurekaServer 注解

package xy.cloud.eureka;

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

@EnableEurekaServer
@SpringBootApplication
public class XyEurekaApplication {

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

启动成功

客户端

核心依赖

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

配置文件

server:
  port: 8081

spring:
  application:
    name: xy-consumer

eureka:
  client:
    service-url:
      #eureka server 的地址
      defaultZone: http://localhost:9090/eureka
    # 向eureka server 获取服务列表
    fetch-registry: true
    #  向eureka server 注册
    register-with-eureka: true

启动类

启动类添加 @EnableEurekaClient 表示当前服务为客户端

@EnableEurekaClient
@SpringBootApplication
public class XyConsumerApplication {

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

}

注册成功

RestTemplate

服务提供者

新建一个服务提供者,xy-provider ,工程跟xy-consumer 方式一样。

接口

供消费者调用

@RestController
public class ProviderController {

    @Value("${server.port}")
    private Integer port;

    @GetMapping("/helloProvider")
    public String helloProvider(){
        return "helo i am provider ,my port is : " +port;
    }
}

服务消费者

RestTemplate配置

@Configuration
public class RestTemplateConfiguration {
    
    @Bean
    public RestTemplate getRestTemplate(){
        return  new RestTemplate();
    }
}

接口

@RestController
public class ConsumerController {

    /**
     * spring提供的一个用于访问rest接口的模板对象
     */
    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/getConsumer")
    public String helloConsumer(){
        String res = restTemplate.getForObject( "http://xy-provider/helloProvider", String.class);
        return res;
    }
}

测试结果

posted @ 2023-05-04 15:11  Hello、Lin  阅读(12)  评论(0编辑  收藏  举报