Docker部署Spring cloud微服务详细讲解 (二)

一、Spring Cloud中的Eureka

      使用Spring Cloud创建微服务项目,其中非常关键的模块就是服务的发现与注册模块(Eureka),该模块对于整个微服务架构可以说起着决定生死的作用,微服务间的通讯间接或直接都有Eureka的参与。本节我们就要讲讲微服务中使用Eureka来进行服务发现和注册时可能会出现的网络问题。

二、准备阶段

      我们准备了一个Eureka项目(服务注册发现中心),Test项目(模拟业务),zuul(网关路由):

下面先分别来看一下配置文件和启动类

Eureka:

配置文件:
spring:
  application:
    name: eureka7001.com

server:
  port: 7001

eureka:
  client:
     register-with-eureka: false     #false:不作为一个客户端注册到注册中心
     fetch-registry: false     #意思是不作为微服务注册到eureka中,默认为true
                               #为true时,可以启动,但报异常:Cannot execute request on any known server
     service-url:
       defaultZone: http://localhost:7001/eureka  #默认注册到8761端口,不写的话会报错

启动类:

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

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

Test:

配置文件:
spring:
  application:
    name: test               #微服务调用时的名字

server:
  port: 8001

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/   #Eureka的地址
  instance:
    instance-id: test8001.com   #注册到eureka中的名字

启动类:

package com.example.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class TestApplication {

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

业务类:

package com.example.test.rest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestRest {
    @RequestMapping("/test/{info}")
    public String test(@PathVariable(value="info") String info) throws Exception {
        return info;
    }
}

zuul:

配置文件:
spring:
  application:
    name: zuul               #微服务调用时的名字

server:
  port: 9001

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/   #Eureka的地址
  instance:
    instance-id: zuul9001.com   #注册到eureka中的名字

zuul:
  ignored-services: "*"  #表示忽略所有为服务的名字,通过下面匹配的路径去访问
  prefix: /api        #为zuul设置一个公共的前缀
  routes: 
    test:
      path: /test/**    #表示拦截/api/test/的所有请求
      service-id: test  #对应test微服务中的spring.application.name 

启动类:

package com.example.zuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableDiscoveryClient
public class ZuulApplication {

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

接着,将三个简单的微服务启动,并访问我们的业务类。

我们可以看到,Eureka已经发现并注册了两个微服务。接着访问http://localhost:9001/api/test/test/helloworld!。


我们可以看到Zuul已经为我们创建了路由。

三、上传服务器,准备测试

将三个微服务打成jar包,并上传到服务器。

此时已经将三个jar包都上传成功了。接着编写他们的Dockerfile

Eureka:

FROM frolvlad/alpine-oraclejdk8
VOLUME /tmp
ADD eureka.jar app.jar   
EXPOSE 7001
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Test:

FROM frolvlad/alpine-oraclejdk8
VOLUME /tmp
ADD Test.jar app.jar
EXPOSE 8001
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Zuul:

FROM frolvlad/alpine-oraclejdk8
VOLUME /tmp
ADD zuul.jar app.jar
EXPOSE 9001
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

分别将三个jar打成镜像

Eureka:


Test:


zuul:


接着运行三个镜像。


现在,三个镜像都已经运行起来并且已经映射了端口号。


三台服务器均正常启动,但是我们可以看到,当鼠标放在test8001.com上时,左下角出现的地址信息却是eaa.....什么的,我们可以在上图中发现这是该容器的ID。那么能够正常访问吗?相信读者也有答案了。


当当当当!!!当然是不能访问的了。

接着,我们修改三个微服务项目的application.yml配置文件,追加 eureka.instance.prefer-ip-address: true(表示微服务显示IP)

接着继续上传,创建镜像并运行,得到下图:



这样子容器会像虚拟机一样自动被分配了一个虚拟ID,并映射了主机的端口号,这样便可以从外部访问到容器并且能够实现容器间的通讯了。


谢谢各位看官能一直看到这里,小弟万分感谢。

若有不正确的地方,欢迎大牛指正!!!!!









猜你喜欢

转载自blog.csdn.net/qq_30770095/article/details/79652387