程序员

Spring Cloud服务注册与发现

2019-12-29  本文已影响0人  勿念及时雨

Spring Cloud服务注册与发现,需要使用到Eureka组件。
1.创建maven主工程
创建maven主工程springcloud-demo,在pom文件中引入相关依赖,Spring Boot版本为2.0.3.RELEASE,Spring Cloud版本为Finchley.RELEASE,此pom文件作为整个项目的父pom文件,具有依赖版本控制的作用,其他module工程继承此pom。pom文件代码如下:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.wunian.springcloud</groupId>
  <artifactId>springcloud-demo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>springcloud-demo Maven Webapp</name>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
    <!--设定一个空值将始终从仓库中获取,不从本地路径获取-->
    <relativePath/>
  </parent>

  <modules>
    <module>eureka-server</module>
    <module>service-hi</module>
  </modules>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring-cloud.version>Finchley.RELEASE</spring-cloud.version><!--SpringCloud版本-->
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>

    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <dependencyManagement>
    <dependencies>
      <!--SpringCloud的父依赖-->
      <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>
    <pluginManagement>
      <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

2.创建两个module工程
一个module工程作为Eureka Server,另一个作为Eureka Client,在Intellij IDEA中分别创建SpringBoot工程分别为eureka-serverservice-hi
Eureka Server中需要引入spring-cloud-starter-netflix-eureka-server依赖,pom文件代码如下:

<?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>

    <groupId>com.wunian.server</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.wunian.springcloud</groupId>
        <artifactId>springcloud-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <!--Spring Cloud eureka-server依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
</project>

Eureka Client中需要引入spring-cloud-starter-netflix-eureka-client依赖,pom文件代码如下:

<?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>com.wunian.springcloud</groupId>
        <artifactId>springcloud-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.wunian.client</groupId>
    <artifactId>service-hi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>service-hi</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--spring web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--SpringCloud eureka-client依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

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

3.启动服务注册中心
在Spring Boot工程eureka-server的启动类上添加@EnableEurekaServer注解即可,代码如下:

@SpringBootApplication
@EnableEurekaServer//启用服务注册中心
public class EurekaServerApplication {

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

4.配置Eureka Server
在默认情况下Eureka Server也是一个Eureka Client ,因此必须指定一个 Server。eureka-server的配置文件appication.yml如下:

server:
  port: 8081
eureka:
  instance:
    hostname: localhost
  client:
    #通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/  #eurekaclient中注册的地址需配置成此
spring:
  application:
    name: eureka-server #服务与服务之间相互调用一般都是根据这个name

配置完成后,启动工程打开浏览器,访问:http://localhost:8081,可以看到eureka server的可视化界面。
5.启动Eureka Client
service-hi的启动类上,通过@EnableEurekaClient注解表明自己是一个Eureka Client,代码如下:

@SpringBootApplication
@EnableEurekaClient //表明自己是一个eurekaclient
@RestController  //@Controller和@ResponseBody注解的组合
public class ServiceHiApplication {

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

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

    //http://localhost:8082/hi?name=jack
    @RequestMapping("/hi")
    public String home(@RequestParam(value = "name",defaultValue = "wunian")String name){
        return "Hello "+name+",I am from port:"+port;
    }
}

6.配置Eureka Client
仅凭@EnableEurekaClient注解还不够,还需在配置文件中配置自己的服务注册中心的地址,application.yml文件如下:

server:
  port: 8082
spring:
  application:
    name: service-hi  #服务与服务之间相互调用一般都是根据这个name
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8081/eureka/   #注册地址与eureka-server中配置的要一致

启动工程,在浏览器中访问:http://localhost:8081,可以发现已经有一个名叫SERVICE-HI,端口为8082的服务已经注册进服务中了,此时在浏览器中访问:http://localhost:8082/hi?name=wunian,可以在浏览器中看到如下信息:

Hello wunian,I am from port:8082

上一篇 下一篇

猜你喜欢

热点阅读