ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringBoot+springCloud+eureka整合(父子聚合工程)

2022-01-06 19:02:48  阅读:236  来源: 互联网

标签:SpringBoot springCloud springframework eureka client org import cloud


1.父pom文件导入依赖:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
    </parent>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2021.0.0</version>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>

注:根据父pom的springBoot版本需要导入兼容的Cloud包

2.编写rureka的server

子工程导入依赖

        <!--eureka服务端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

编写application.xml的文件

server:
#  服务端口
  port: 7777
spring:
  application:
#    服务name
    name: eureka-server
eureka:
  client:
#    禁止自己当做服务注册
    register-with-eureka: false
#    屏蔽注册信息
    fetch-registry: false
    service-url:
#      注册到eureka的ip地址
      defaultZone: http://${spring.application.name}:${server.port}/eureka
  instance:
#    为false时,那么注册到Eureka中的Ip地址就是本机的Ip地址
#    如果设置了true并且也设置了eureka.instance.ip-address那么就将此ip地址注册到Eureka中
    prefer-ip-address: true
#    将此ip地址注册到Eureka中
    ip-address: 127.0.0.1
#    127.0.0.1设置为服务名
    hostname: ${spring.application.name}

编写application启动类

package com.guigu.cloud.eureka;

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

@SpringBootApplication
/*
* EnableEurekaServer
* 注解的作用是:只有eureka的服务端注解类生效
* */
@EnableEurekaServer
public class SpringCloudEurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudEurekaApplication.class,args);
    }
}

启动application启动类之后

浏览器访问:http://127.0.0.7:7777

出现如下效果,则成功搭建eureka注册中心环境

3.服务提供者

子工程导入依赖

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

编写application.yml文件

server:
#  服务端口
  port: 24317
spring:
#  数据库数据源
  datasource:
#    驱动
    driver-class-name: com.mysql.cj.jdbc.Driver
#    数据库地址
    url: jdbc:mysql://127.0.0.1:3306/springcloud?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
#    用户
    username: root
#    密码
    password: 123456
  application:
#    服务名
    name: user-provider
eureka:
  client:
    service-url:
#      注册到eureka的ip地址
      defaultZone: http://localhost:7777/eureka
  instance:
#    表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance。
    lease-expiration-duration-in-seconds: 90
#    表示eureka client发送心跳给server端的频率。
    lease-renewal-interval-in-seconds: 30

编写服务提供者apllication启动类

package com.guigu.cloud;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@MapperScan(basePackages = "com.guigu.cloud.mapper")
/*
* EnableEurekaClient:只有eureka的客户端注解类生效
* EnableDiscoveryClient:所有的注册中心注解类都可以用
* */
//@EnableEurekaClient
@EnableDiscoveryClient
public class UserProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserProviderApplication.class,args);
    }
}

启动服务提供者apllication启动类之后

访问eureka可视化界面

Instances currently registered with Eureka一栏出现user-provider程序,提供商服务则注册成功

编写消费者的Controller

package com.guigu.cloud.controller;

import com.guigu.cloud.pojo.TbUser;
import com.guigu.cloud.service.UserService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    UserService userService;

    @RequestMapping("find/{id}")
    public TbUser findById(@PathVariable Integer id){
        System.out.println("provide:id:"+id);
        TbUser userId = userService.getUserId(id);
        return userId;
    }
}

4.服务消费者

子工程导入依赖

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

编写application.yml文件

server:
#  服务端口
  port: 24318
spring:
  application:
#    服务名
    name: user-consumer
eureka:
  client:
    service-url:
#      注册到eureka的ip地址
      defaultZone: http://localhost:7777/eureka

编写服务消费者apllication启动类

package com.guigu.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

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

    /*注入eureka的发送请求对象*/
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

启动服务消费者apllication启动类之后

访问eureka可视化界面

Instances currently registered with Eureka一栏出现user-consumer程序,提供商服务则注册成功

编写消费者的Controller

package com.guigu.cloud.controller;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("consumer")
public class UserController {
    @Resource
    /*发送请求对象*/
    private RestTemplate restTemplate;

    @Resource
    /*获取ruewka注册中心信息对象*/
    private DiscoveryClient discoveryClient;

    @RequestMapping("user/{id}")
    public Object findById(@PathVariable Integer id){
        //通过信息对象获取eureka服务中名字叫user
        //-provider的信息
        List<ServiceInstance> list = discoveryClient.getInstances("user-provider");
        //由于是一个集合,取第一个信息的对象
        ServiceInstance serviceInstance=list.get(0);
        //获取host和post加上提供者注册的端口服务
        String url="http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/user/find/"+id;
        //http://127.0.0.1/24317/user/find/1
        //发送请求
        return restTemplate.getForObject(url,String.class);
    }
}

源码链接,仅供学习参考

springCloud_eureka.zip - 蓝奏云文件大小:21.3 K|https://llh317.lanzout.com/iYEtRygfxcf

标签:SpringBoot,springCloud,springframework,eureka,client,org,import,cloud
来源: https://blog.csdn.net/L0317/article/details/122345877

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有