栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Eureka服务注册与发现和集群环境搭建

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Eureka服务注册与发现和集群环境搭建

1、搭建第一个Eureka注册中心,项目名为eureka-server-7001

导入pom文件





    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.4.RELEASE
         
    
    erueka-server7001
    erueka-server7001
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
            2.2.1.RELEASE
        
    

2、编写配置文件application.yaml

server:
  port: 7001
spring:
  application:
    name: demo-register
eureka:
  instance:
    hostname: erueka7001.com
  client:
    register-with-eureka: false
    fetch-registry: false
    instance-info-replication-interval-seconds: 30
    serviceUrl:
      defaultZone: http://erueka7002.com:7002/eureka/,http://erueka7003.com:7003/eureka/

3、在springboot的启动类上加注解@EnableEurekaServer启动服务

package org.example;

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



@SpringBootApplication
@EnableEurekaServer
public class App7001
{
    public static void main( String[] args )
    {

        SpringApplication.run(App7001.class,args);
    }
}

4、搭建第二个Eureka注册中心,项目名为eureka-server-7002





    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.4.RELEASE
         
    
    erueka-server-7002
    erueka-server-7002
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
            2.2.1.RELEASE
        
    

5、编写配置文件application.yaml

server:
  port: 7002
spring:
  application:
    name: demo-register
eureka:
  instance:
    hostname: erueka7002.com
  client:
    register-with-eureka: false
    fetch-registry: false
    instance-info-replication-interval-seconds: 30
    serviceUrl:
      defaultZone: http://erueka7001.com:7001/eureka/,http://erueka7003.com:7003/eureka/

6、在springboot的启动类上加注解@EnableEurekaServer启动服务

package org.example;

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


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

    }
}

7、搭建第三个Eureka注册中心,项目名为eureka-server-7003

导入pom文件





    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.4.RELEASE
         
    
    erueka-server-7003
    erueka-server-7003

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
            2.2.1.RELEASE
        
    


8、编写application.yaml配置文件

server:
  port: 7003
spring:
  application:
    name: demo-register
eureka:
  instance:
    hostname: erueka7003.com
  client:
    register-with-eureka: false
    fetch-registry: false
    instance-info-replication-interval-seconds: 30
    serviceUrl:
      defaultZone: http://erueka7001.com:7001/eureka/,http://erueka7002.com:7002/eureka/

9、在springboot的启动类上加注解@EnableEurekaServer启动服务

package org.example;

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


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

    }
}

10、注册服务到三个注册中心,第一个服务的提供者项目名为 eureka-provider-7004

导入pom文件




    
        spring-boot-starter-parent
        org.springframework.boot
        2.2.4.RELEASE
         
    
    4.0.0

    erueka-provider-7004

    erueka-provider-7004
    
        UTF-8
        1.7
        1.7
    

    
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.4
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            mysql
            mysql-connector-java
            8.0.26
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
            2.2.1.RELEASE
        
    


11、编写controller,dao,pojo三个包的类访问数据库

<1>controller包中的类

package org.example.controller;

import org.example.dao.UserMapper;
import org.example.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class ProviderController {
    @Autowired
    UserMapper userMapper;
    @GetMapping("/hello")
    @ResponseBody
    public String getUser() {
        List userList = userMapper.getUserList();
        return "从服务提供者获取到了信息"+userList;
    }
}

<2>dao包中的类

package org.example.dao;

import org.example.pojo.User;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface UserMapper {
//    获取user表的信息
    List getUserList();
}

<3>pojo实体类

package org.example.pojo;

public class User {
    private int id;
    private String name;
    private int age;
    private String db;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", age=" + age +
                ", db='" + db + ''' +
                '}';
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getDb() {
        return db;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setDb(String db) {
        this.db = db;
    }
}

<4>springboot的启动类

package org.example;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
@MapperScan("org.example.dao")
public class Provider7004
{
    public static void main( String[] args )
    {
        SpringApplication.run(Provider7004.class,args);

    }
}

12、编写resources下的application.yaml配置文件

eureka:
  client:
    service-url:
      defaultZone: http://erueka7001.com:7001/eureka/,http://erueka7002.com:7002/eureka/,http://erueka7003.com:7003/eureka/
    register-with-eureka: true
    fetch-registry: true
    instance-info-replication-interval-seconds: 30
    registry-fetch-interval-seconds: 10
  instance:
    instance-id: erueka-provider-7004

server:
  port: 7004

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db01?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=true
    username: root
    password: 123
  application:
    name: service-provider-7004
mybatis:
  type-aliases-package: org.example.pojo
  mapper-locations: classpath:mybatis/mapper/*.xml

13、编写resources下的mapper文件,在resources下新建一个目录为mybatis/mapper,文件名为UserMapper.xml





    
        SELECT * FROM user
    

14、编写Eureka-consumer-7007来测试服务

 第一步,创建BeanConfiguration的类,创建RestTemplate

1 package com.ssc.consumer;
 2 
 3 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.web.bind.annotation.GetMapping;
 7 import org.springframework.web.client.RestOperations;
 8 import org.springframework.web.client.RestTemplate;
 9 
10 @Configuration
11 public class BeanConfiguration {
12    @Bean
13    @LoadBalanced
14     public RestTemplate getRestTemplate(){
15         return new RestTemplate();
16     }
17 
18 
19 }

第二步写调用接口

 1 package org.example.consumer;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.web.bind.annotation.GetMapping;
 5 import org.springframework.web.bind.annotation.RestController;
 6 import org.springframework.web.client.RestTemplate;
 7 
 8 @RestController
 9 public class ConsumerController {
10     @Autowired
11     private RestTemplate restTemplate;
     //直接调用
12     @GetMapping("/consumer/callHello")
13     public String callHello(){
14         return restTemplate.getForObject("http://localhost:7004/hello",String.class);
15     }
16 //通过Eureka调用
17     @GetMapping("/consumer/callHello2")
18     public String callHello2(){
19         return restTemplate.getForObject("http://erueka7004.com/hello",String.class);
20     }
21 
22 }

第三步、添加application.yml文件

1 spring:
2   application:
3     name: eureka-user-consumer
4 server:
5   port: 7007
6 eureka:
7   client:
8     service-url:
9       defaultZone: http://localhost:7004/eureka

第四步、启动程序,访问 http://localhost:7007/consumer/callHello2查看效果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/424911.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号