说在前头:本人为大二在读学生,书写文章的目的是为了对自己掌握的知识和技术进行一定的记录,同时乐于与大家一起分享,因本人资历尚浅,能力有限,文章难免存在一些错漏之处,还请阅读此文章的大牛们见谅与斧正。若在阅读时有任何的问题,也可通过评论提出,本人将根据自身能力对问题进行一定的解答。
前言
1.创建eureka-server
①创建一个名为eureka-server的工程,作为配置中心
②依赖配置文件内容:
<?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">
<parent>
<artifactId>Bosen</artifactId>
<groupId>com.bosen.www</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-server</artifactId>
<dependencies>
<!-- eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
</project>
复制
③启动类内容:
package com.bosen.eureka.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* <p>eureka服务端启动类</p>
* @author Bosen 2021/5/20 13:47
*/
@SpringBootApplication// springboot启动配置
@EnableEurekaServer// 开启eureka服务发现功能
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
复制
④配置文件内容:
spring:
application:
name: eureka-server # 应用名
server:
port: 8761 # 端口号
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false # 是否注册自身
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 注册地址
复制
⑤启动项目:启动成功后控制台日志输入如下
2.在gitee创建配置仓库
SpringCloudConfig的配置文件一般都放在github中统一托管,但国内访问github相对较慢,因此我们使用国内的gitee作为配置托管中心进行测试。
我们需要在gitee中创建一个保存项目配置文件的操作,当我们启动项目时,项目通过该仓库获取配置信息。
在gitee中注册完成后,创建一个仓库的流程如下:
在gitee创建好仓库后,我们接下来需要创建配置中心服务,用于管理各个服务的配置信息。
②依赖配置文件内容:
<?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">
<parent>
<artifactId>Bosen</artifactId>
<groupId>com.bosen.www</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-server</artifactId>
<dependencies>
<!-- config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
</project>
复制
③创建启动类:
package com.bosen.config.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* <p>配置中心启动类</p>
* @author Bosen 2021/5/21 13:10
*/
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
复制
④启动类内容说明:
EnableEurekaClient:声明为eureka客户端,需要向eureka注册中心注册,并依赖注册中心完成各个服务的配置信息装配。
EnableConfigServer:开启配置中心服务。
⑤设置配置文件application.yml:
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/bosen-once/bosen # 存放配置文件的仓库
default-label: master # 分支
search-paths: config # 对应的文件夹
#username: # 如果仓库是公共的不需要输入用户名和密码,否则必须要输入
#password: # 密码(可以使用本机公钥)
server:
port: 8888
eureka:
client:
serviceUrl:
defautlZone: http://localhost:8761/eureka/
复制
⑥启动服务:启动成功后如下
⑦访问eureka-server查看config-server是否已经成功注册:访问http://localhost:8761/ 如下
4.创建用于测试的eureka-client客户端
①依赖配置:
<?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">
<parent>
<artifactId>Bosen</artifactId>
<groupId>com.bosen.www</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-client</artifactId>
<dependencies>
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6</version>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<!-- config-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
</dependencies>
</project>
复制
②启动类:
package com.bosen.eureka.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
复制
③编写一个控制器:
package com.bosen.eureka.client.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@RequestMapping("/")
public String index() {
return "This is IndexController index();";
}
}
复制
④配置文件(注意:配置文件命名应当使用bootstrap,不建议使用application。原因:bootstrap的优先级大于application,需要从远端获取配置文件时,应该使用优先级大的bootstrap,以免发生不必要的错误)
spring:
application:
name: eureka-client # 当前服务应用名
cloud:
config:
label: master # 仓库分支
discovery:
enabled: true # 开启 Config 服务发现与注册
client:
simple:
local:
service-id: config-server # 配置中心应用名
复制
⑤在gitee中编写该服务的配置文件:
server:
port: 8001 # 端口号
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ # 注册地址
复制
5.测试
6.总结