美文网首页
SpringCloud 之Eureka入门

SpringCloud 之Eureka入门

作者: 8奈文摩尔8 | 来源:发表于2020-08-05 20:22 被阅读0次

创建服务端-注册中心
添加cloud 依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

启动类开启服务
@EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class EurekaClientProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientProviderApplication.class, args);
    }

修改application.yml配置文件

# 注释版本
server:
  port: 10086
spring:
  # 指定服务名称  
  application:
    name: eureka-server
eureka:
  client:
    # 注册中心地址
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
    # 是否需要注册到注册中心
    fetch-registry: false  
 server:
     # 开启自我保护机制(默认就是true,如果设置成false则在eureka注册中心有红色警告)
     enable-self-preservation: true
 instance:
    # 强制指定ip地址(不用配置,使用默认获取当前服务器地址)
    ip-address: 127.0.0.1
    # 默认获取当前服务器地址(可以不用配置,默认值就是true)
    prefer-ip-address: true

使用 http://localhost:10086/](http://localhost:10086/ 测试


创建客户端_provider 提供方
pom配置
~~~
 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <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>
~~~

启动类开启客户端
@EnableEurekaClient
~~~
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientProviderApplication.class, args);
    }
~~~
修改配置文件 application.yml
~~~
# 配置应用基本信息和DB
server:
  port: 9091
spring:
  application:
    name: eureka-client-provider
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    password: root
    url: jdbc:mysql://127.0.0.1:3306/springcloud?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
# 配置eureka server
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
~~~
启动项目 去注册中心检查是否正常注册

创建客户端-consumer
pom配置文件

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>

启动类开启客户端 并注入restTemplast

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientConsumerApplication {

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

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

}

创建 ConsumerController 执行调用

@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/getUser/{id}")
    public String getUser(@PathVariable Integer id){
        // 指定请求服务的url,然后通过RestTemplate发送请求
        // 不建议这种写法,url硬编码了
//        String url = "http://localhost:9091/user/findUserById/" + id;
        // 1、获取Eureka注册中心提供方实例列表
        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("eureka_client_provider");
        // 2、获取具体实例(服务)
        ServiceInstance serviceInstance = serviceInstances.get(0);
        // 3、发送请求
        String url = "http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/user/findUserById/" + id;
        String json = restTemplate.getForObject(url, String.class);
        return json;
    }
}

修改配置文件 application.yml

# 配置应用基本信息
server:
  port: 8080
spring:
  application:
    name: eureka-client-consumer
# 配置eureka server
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

访问 http://localhost:8080/consumer/getUser/1 测试

创建spring boot 项目

相关文章

网友评论

      本文标题:SpringCloud 之Eureka入门

      本文链接:https://www.haomeiwen.com/subject/dmworktx.html