Spring Cloud demo(一)── eureka服务搭建

eureka服务搭建

  1. 新建一个maven项目 eureka-server
  2. 引入相关依赖
    2.1 pom文件
    注意依赖的版本问题

<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.5.RELEASEversion> 
        <relativePath/> 
    parent>

    <groupId>com.boogroupId>
    <artifactId>eureka-serverartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>eureka-servername>
    <description>eureka server for Spring clouddescription>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        
        <spring-cloud.version>Finchley.SR1spring-cloud.version>
    properties>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
        dependency>
   dependencies>

    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                configuration>
            plugin>
        plugins>
    build>

project>

  1. application.yml配置文件

spring:
  application:
    name: eureka-server
server:
  port: 8761
  servlet:
      # 应用的访问路径
      context-path: /
eureka:
  instance:
    hostname: localhost
  client:
    #是否将自己注册到eureka server,构建集群环境时,需将自己注册到集群中,默认为true
    registerWithEureka: false
    #是否从eureka server获取注册信息,构建集群环境时,需同步其他eureka server节点,默认为true
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/
  1. 启动类
    Eureka服务端加上@EnableEurekaServer注解
package com.boo.eureka;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

  1. 启动
    启动成功,在浏览器访问 http://localhost:8761/
    Spring Cloud demo(一)── eureka服务搭建_第1张图片
问题解决
  1. 版本冲突问题
  • SpringBoot 与 SpringCloud 版本对应如下(部分)
spring-boot-starter-parent spring-cloud-dependencies
版本号 发布日期 版本号 发布日期
1.5.2.RELEASE 2017 年 3 月 稳定版 Dalston.RC1 2017 年未知月
1.5.9.RELEASE 2017 年 11 月 稳定版 Edgware.RELEASE 稳定版
1.5.16.RELEASE
1.5.20.RELEASE Edgware.SR5
2.0.2.RELEASE 2018 年 5 月 Finchley.BUILD-SNAPSHOT 2018 年
2.0.6.RELEASE Finchley.SR2
2.1.4.RELEASE GreenWith.SR1
2.1.7.RELEASE Aug, 2019 Greenwich.RELEASE Jan 23,2019

eureka客户端(提供者)

  1. 新建一个maven项目 eureka-provider
  2. 引入相关依赖
    2.1 pom文件
    注意依赖的版本问题

<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.0modelVersion>
	<parent>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-parentartifactId>
		<version>2.0.5.RELEASEversion> 
		<relativePath/> 
	parent>

	<groupId>com.boogroupId>
	<artifactId>eureka-providerartifactId>
	<version>0.0.1-SNAPSHOTversion>
	<name>eureka-providername>
	<description>eureka客户端(提供者) for Spring Clouddescription>

	<properties>
		<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
		<java.version>1.8java.version>
		
		<spring-cloud.version>Finchley.SR1spring-cloud.version>
	properties>

	<dependencies>

		
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>

		
		<dependency>
			<groupId>org.springframework.cloudgroupId>
			<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
		dependency>

	dependencies>

	
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloudgroupId>
				<artifactId>spring-cloud-dependenciesartifactId>
				<version>${spring-cloud.version}version>
				<type>pomtype>
				<scope>importscope>
			dependency>
		dependencies>
	dependencyManagement>

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

project>
  1. application.yml配置文件
    写明自己的服务名称和需要注册到哪个Eureka服务器上
server:
  port: 8081
  servlet:
    context-path: /

spring:
  application:
    name: eureka-provider

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  1. 启动类
    Eureka服务端加上@EnableEurekaClient注解
package com.boo.eurekaprovider;

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

@SpringBootApplication
@EnableEurekaClient // Eureka 客户端
public class EurekaProviderApplication {

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

}
  1. 创建一个Rest风格的控制器CarController ,用于向调用者提供自己的服务
@RestController
@RequestMapping("/car")
public class CarController {

    @RequestMapping("/detail/{carId}")
    public BizCar detail(@PathVariable("carId") Long carId){
        BizCar bizCar = new BizCar();
        bizCar.setCarId(carId);
        bizCar.setCarName("HV H6");

        return bizCar;
    }
}
  1. 启动
    6.1 在浏览器访问 http://localhost:8081/car/detail/1
    Spring Cloud demo(一)── eureka服务搭建_第2张图片
    6.2 此时Eureka注册中心页面展示为

eureka客户端(调用者)

提供者已经创建完毕,为了验证我们是否可以调用来自提供者的服务,我们接着创建一个简单的maven项目eureka-consumer,在pom.xml中加入依赖,由于都是作为Eureka客户端,所以eureka-consumer的依赖和eureka-provider的依赖相同,只不过调用者作为服务的请求一方,并不关心具体是怎么拿到服务的,而服务提供者有可能不止一个–可能是集群。

  1. 新建一个maven项目 eureka-consumer
  2. 引入相关依赖
    2.1 pom文件
    注意依赖的版本问题

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.5.RELEASEversion> 
        <relativePath/> 
    parent>

    <groupId>com.boogroupId>
    <artifactId>eureka-consumerartifactId>
    <version>1.0-SNAPSHOTversion>
    <description>eureka客户端(提供者) for Spring Clouddescription>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        
        <spring-cloud.version>Finchley.SR1spring-cloud.version>
    properties>

    <dependencies>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-configartifactId>
        dependency>

    dependencies>

    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>
  1. application.yml配置文件
    写明自己的服务名称和需要注册到哪个Eureka服务器上
server:
  port: 8082
  servlet:
    context-path: /

spring:
  application:
    name: eureka-consumer

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  1. 启动类
    Eureka服务端加上@EnableEurekaClient注解
@SpringBootApplication
@EnableEurekaClient // Eureka 客户端
public class EurekaConsumerApplication {
	public static void main(String[] args) {
		SpringApplication.run(EurekaConsumerApplication.class, args);
	}
}
  1. 创建一个Rest风格的控制器ConsumerController ,让eureka-consumer引用Spring RestTemplate作为一个负载均衡的客户端去调用服务提供者提供的服务;使用创建RestTemplate的Bean组件(@Bean)并使用@LoadBalanced限定符,创建一个负载均衡的RestTemplate。
    RestTemplate是Spring框架里面提供的,它可以自动配置去使用Ribbon。
    这里我们可以直接使用ribbon进行负载均衡,相关依赖已经包含在spring-cloud-starter-netflix-eureka-xxx包中,无需重复引入:
    Spring Cloud demo(一)── eureka服务搭建_第3张图片
  • ConsumerController
package com.boo.web.controller;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

/**
 * description: ConsumerController 
* date: 2021/8/26 15:29
* author: Boo
* version: 1.0
*/
@Controller @Configuration public class ConsumerController { @Bean @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); } @GetMapping(value = "/router") @ResponseBody public String router(){ RestTemplate temp = getRestTemplate(); return temp.getForObject("http://eureka-provider/car/detail/1", String.class); } }
  1. 启动
    6.1 在浏览器访问 http://localhost:8082/router
    Spring Cloud demo(一)── eureka服务搭建_第4张图片

6.2 此时Eureka注册中心页面展示为
Spring Cloud demo(一)── eureka服务搭建_第5张图片

你可能感兴趣的:(Spring,Cloud,spring,boot,java,spring,spring,cloud)