微服务组件部署

时间:2022-06-20 04:13:36
使用IDEA构建Eureka服务
1、创建服务注册中心
a、创建SpringBoot项目时勾中CloudDovery中的EurekaServer
b、在Pom文件中,添加eureka依赖
<dependencies>
<!--eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
c、在main函数类中,添加@EnableEurekaServer注解。
@EnableEurekaServer
@SpringBootApplication
public class EurekaserverApplication {

public static void main(String[] args) {
SpringApplication.run(EurekaserverApplication.class, args);
}
}
d、修改配置文件( appication.yml或者 appication.properties)
server.port=8761
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
spring.application.name=cloud-eureka-server

如上,配置了eureka的服务端口,通过 eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server。
e、通过IDEA直接启动,访问 http://localhost:8761 ,能够看到Eureka的管理页面

——————————————————————————————————————————————————
创建一个服务提供者(EurekaClient)
a、创建方式同上
b、不同点,需要在主函数中修改或添加注解@EnableEurekaClient,申明自己是一个EurekaClient
c、修改配置文件 application.yml,指定服务注册中心的地址,和自己服务的端口号
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8762
spring:
application:
name: service-hi
d、运行,访问 http://localhost:8762/hi?name=forezp能看到页面显示“hi forezp,i am from port:8762”,访问 http://localhost:8761能看到现在已经注册过的服务。________________________________________________________________________________________________________________________
构建Docker镜像
a、如果需要将EurekaServer设置成多节点,需要在EurekaClient中添加多个注册中心地址,多个地址用“,”隔开。
b、编写DockerFile
FROM 10.128.222.245:5000/frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD EurekaServer.jar app.jar
#RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
c、构建镜像
sudo docker build -t="eurekaserver" .
d、运行
docker run --name EurekaServer -p 8761:8761 eurekaserver

docker server create --name EurekaServer --publish 8761:8761 ----replicas 2 10.128.222.245:5000/eurekaserver


history
docker service create --name eurekaserver --publish 8761 --replicas 3 10.128.222.245:5000/eurekaserver:latest
docker service create --name service-hi_8762 --publish 8762:8762 --replicas 2 10.128.222.245:5000/service-hi_8762:latest
docker service create --name service-hi_8763 --publish 8763:8763 --replicas 2 10.128.222.245:5000/service-hi-8763:latest
docker service create --name service-ribbon --publish 8764:8764 --replicas 2 10.128.222.245:5000/service-ribbon:latest
docker service create --name service-ribbon --publish 8769:8769 --replicas 2 10.128.222.245:5000/service-ribbon:latest
docker service create --name config-server --publish 8888:8888 --replicas 2 10.128.222.245:5000/config-server:latest
docker service create --name config-client --publish 8881:8881 --replicas 2 10.128.222.245:5000/config-client:latest