SpringCloud微服务小白也能搭(Hoxton.SR8)(一)Eureka|服务的注册与发现

简单上手,直接照搬,就可搭建微服务(Hoxton.SR8) 2020.8.28发布,SpringCloud搭建的文章正在整理,干货不要错过哦
https://spring.io/blog/2020/08/28/spring-cloud-hoxton-sr8-has-been-released

1.创建父pom项目

注意:

  1. Spring Assistant是一个插件,需要自己安装,安装教程参考
  2. 第三幅图,以前是不用强制选择的组件的,应该是ideaj的版本问题,没关系,我们先选一个,创建完后删除就好了
  3. 我们使用的springboot版本是2.2.10
  4. 最后选择你的项目名以及项目位置

   

1.1 当前阶段父 pom.xml 内容

<?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>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.10.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.zqh.www</groupId>
    <artifactId>cloud-hoxton-sr8</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cloud-hoxton-sr8</name>
    <description>Hoxton.SR8微服务</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
    </properties>

    <dependencies>
    </dependencies>

    <!-- https://spring.io/blog/2020/08/28/spring-cloud-hoxton-sr8-has-been-released -->
    <dependencyManagement>
        <dependencies>
            <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>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.创建 Eureka 注册中心

注意:

  1. 右击项目创建module,选择Maven,点击next,然后填写你的项目名称

   

2.1 当前阶段 eureka-server 项目 pom.xml 内容

<?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>cloud-hoxton-sr8</artifactId>
        <groupId>com.zqh.www</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-server</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
         <!-- 给Eureka注册中心添加认证 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>

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

2.2 配置 EurekaServerApplication 以及 application.yml

注意:

  1. ideaj没有帮我生成包路径,Application文件,以及application.yml,所以这里是我自己创建
package com.zqh.www;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.core.env.Environment;

/**
 * 开启eureka服务
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

    private final static Logger logger = LoggerFactory.getLogger(EurekaServerApplication.class);

    public static void main(String[] args) {
        Environment env = SpringApplication.run(EurekaServerApplication.class, args).getEnvironment();
        logger.info(
                "\n----------------------------------------------------------\n\t"
                        + "Application '{}' is running! Access URLs:\n\t"
                        + "Local: \t\thttp://localhost:{}{}"
                        + "\n----------------------------------------------------------",
                env.getProperty("spring.application.name"), env.getProperty("server.port"),
                env.getProperty("server.servlet.context-path") != null ? env.getProperty("server.servlet.context-path") : "");
    }
}
server:
  port: 8081
spring:
  application:
    name: eureka-server
  security:
    user:
      # 配置spring security登录用户名和密码
      name: root
      password: root
eureka:
  client:
    # 让自己不需要注册在上面禁止客户端注册,表明自己是一个eureka server
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  instance:
    hostname: localhost
package com.zqh.www.config;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * 默认情况下添加SpringSecurity依赖的应用每个请求都需要添加CSRF token才能访问,Eureka客户端注册时并不会添加,所以需要配置/eureka/**路径不需要CSRF token。
 */
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}

  

2.3 浏览器访问 eureka 服务页面

注意:点击访问 http://localhost:8081  账户:root  密码:root

3.创建客户端并注册

注意:

  1. 右击项目创建module,选择Maven,点击next,然后填写你的项目名称
  2. 按照2.2的步骤创建包路径,EurekaClientApplication 和 application.yml

   

3.1 当前阶段 eureka-client 项目 pom.xml 内容

<?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>cloud-hoxton-sr8</artifactId>
        <groupId>com.zqh.www</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-client</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3.2 EurekaClientApplication 和 application.yml 

package com.zqh.www;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.core.env.Environment;

/**
 * 开启eureka客户端
 */
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {

    private final static Logger logger = LoggerFactory.getLogger(EurekaClientApplication.class);

    public static void main(String[] args) {
        Environment env = SpringApplication.run(EurekaClientApplication.class, args).getEnvironment();
        logger.info(
                "\n----------------------------------------------------------\n\t"
                        + "Application '{}' is running! Access URLs:\n\t"
                        + "Local: \t\thttp://localhost:{}{}"
                        + "\n----------------------------------------------------------",
                env.getProperty("spring.application.name"), env.getProperty("server.port"),
                env.getProperty("server.servlet.context-path") != null ? env.getProperty("server.servlet.context-path") : "");
    }
}
server:
  port: 8083
spring:
  application:
    name: eureka-client
eureka:
  client:
    service-url:
      #注册地址
      defaultZone: http://root:root@localhost:8081/eureka/
  #显示服务器IP加端口
  instance:
    hostname: localhost
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

4.刷新浏览器eureka服务页面,服务注册完成

5. 总结

  • 完成eureka 服务的启动,并添加security授权验证
  • 完成 client 注册 eureka

6.gitee地址

源码参考

### 使用 Supervisor 和 Gunicorn 部署 Python Web 应用 #### 安装必要的软件包 为了确保环境准备就绪,需先安装 `gunicorn` 及其管理工具 `supervisor`。对于基于 Debian 的 Linux 发行版,可以利用 `apt-get` 来完成此操作: ```bash sudo apt-get update && sudo apt-get install gunicorn supervisor -y ``` 这步骤会自动处理依赖关系并安装最新版本的应用程序及其服务监控器[^4]。 #### 创建应用程序目录结构 建议为项目建立专门的工作空间,以便更好地管理和维护各个组件之间的关联性。假设工作区位于 `/var/www/myproject/` 下,则应包含如下子文件夹: - **static/** 存储静态资源(CSS, JavaScript 文件) - **media/** 用户上传的内容保存在此处 - **logs/** 日志记录位置 - **venv/** 虚拟环境中存放Python解释器及相关库 此外还需放置源码以及配置文件于根目录下。 #### 编写 WSGI 入口脚本 创建名为 `wsgi.py` 或者其他名称的入口模块来定义 Flask/Django 等框架实例作为可调用对象传递给 Gunicorn: ```python from myapp import create_app # 假设myapp是你的应用名 application = create_app() ``` 该文件应当置于项目的顶层目录内方便后续引用。 #### 设置 Gunicorn 启动参数 通过编写 `.ini` 格式的配置文档指定运行选项,比如监听端口号、进程数量等重要设置项。这里给出个简单的例子——`gunicorn.conf.ini` : ```ini [program:gunicorn] command=/path/to/gunicorn --workers 3 --bind unix:/tmp/app.sock config.wsgi:application directory=/var/www/myproject/ user=nobody group=nogroup autostart=true autorestart=true stderr_logfile=/var/log/gunicorn/error.log stdout_logfile=/var/log/gunicorn/access.log environment DJANGO_SETTINGS_MODULE="config.settings.production" ``` 注意上述命令中的路径需要根据实际情况调整;同时考虑到安全性因素推荐使用 Unix socket 方式连接 Nginx 服务器而不是公开 IP 地址绑定[^2]。 #### 将任务加入到 Supervisord 中 编辑全局配置文件 `/etc/supervisord.conf` ,添加指向自定义 INI 文件的新条目从而让 supervisord 认识到新注册服务单元: ```ini [include] files = /etc/supervisor/conf.d/*.conf ``` 接着把之前编写的 Gunicorn 配置复制粘贴至 `/etc/supervisor/conf.d/gunicorn.conf` 并重启守护进程使之生效: ```bash sudo systemctl restart supervisor ``` 此时应该可以通过查看状态得知 Gunicorn 是否成功启动并且处于稳定运行之中了。 #### NGINX反向代理配置 最后步就是修改Nginx站点可用配置以实现HTTP请求转发功能。打开对应虚拟主机模板后追加类似下面所示片段: ```nginx server { listen 80; server_name example.com www.example.com; location / { proxy_pass http://unix:/tmp/app.sock; # 对应Gunicorn使用的socket地址 include proxy_params; } location /static/ { alias /var/www/myproject/static/; } } ``` 记得执行 `sudo nginx -t` 测试语法无误后再加载更新过的设定表单。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值