Spring Cloud 连接数据库

  1. 下载模板包
    https://start.spring.io/
    选择 Spring Web, Spring Data JPA, and MySQL Driver.
  2. 测试本地数据库是否能连通
  3. 解压文件
  4. 修改配置文件 src/main/resources/application.properties
    spring.jpa.hibernate.ddl-auto=update
    spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
    spring.datasource.username=springuser
    spring.datasource.password=ThePassword
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    #spring.jpa.show-sql: true
  5. 增加 @Entity Model

    src/main/java/com/example/accessingdatamysql/User.java

package com.example.accessingdatamysql;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity // This tells Hibernate to make a table out of this class
public class User {
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Integer id;

  private String name;

  private String email;

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }
}

6 增加UserRepository.java

package com.example.accessingdatamysql;

import org.springframework.data.repository.CrudRepository;

import com.example.accessingdatamysql.User;

// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete

public interface UserRepository extends CrudRepository<User, Integer> {

}

7 增加src/main/java/com/example/accessingdatamysql/MainController.java

package com.example.accessingdatamysql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller // This means that this class is a Controller
@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)
public class MainController {
  @Autowired // This means to get the bean called userRepository
         // Which is auto-generated by Spring, we will use it to handle the data
  private UserRepository userRepository;

  @PostMapping(path="/add") // Map ONLY POST Requests
  public @ResponseBody String addNewUser (@RequestParam String name
      , @RequestParam String email) {
    // @ResponseBody means the returned String is the response, not a view name
    // @RequestParam means it is a parameter from the GET or POST request

    User n = new User();
    n.setName(name);
    n.setEmail(email);
    userRepository.save(n);
    return "Saved";
  }

  @GetMapping(path="/all")
  public @ResponseBody Iterable<User> getAllUsers() {
    // This returns a JSON or XML with the users
    return userRepository.findAll();
  }
}

8 运行

mvn spring-boot:run

9 测试,使用postman或者curl

curl --location 'http://localhost:8080/demo/all'
curl --location 'http://localhost:8080/demo/add' \
--form 'name="First"'

curl --location 'http://localhost:8080/demo/update' \
--form 'name="2"' \
--form 'id="3"'

curl --location 'http://localhost:8080/demo/delete' \
--form 'id="3"'

好,下面我要把他改造成一个微服务,并推送到k8中

minikube start
😄  Microsoft Windows 10 Home China 10.0.19045.2364 Build 19045.2364 上的 minikube v1.30.1
✨  根据现有的配置文件使用 docker 驱动程序
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
🔄  Restarting existing docker container for "minikube" ...
🐳  正在 Docker 23.0.2 中准备 Kubernetes v1.26.3…
🔗  Configuring bridge CNI (Container Networking Interface) ...
🔎  Verifying Kubernetes components...
    ▪ Using image docker.io/kubernetesui/dashboard:v2.7.0
    ▪ Using image docker.io/kubernetesui/metrics-scraper:v1.0.8
    ▪ Using image registry.k8s.io/metrics-server/metrics-server:v0.6.3
    ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
💡  Some dashboard features require the metrics-server addon. To enable all features please run:

        minikube addons enable metrics-server


🌟  Enabled addons: storage-provisioner, metrics-server, default-storageclass, dashboard
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

启动KubeSphere

kubectl apply -f https://github.com/kubesphere/ks-installer/releases/download/v3.3.2/kubesphere-installer.yaml

kubectl apply -f https://github.com/kubesphere/ks-installer/releases/download/v3.3.2/cluster-configuration.yaml

kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l 'app in (ks-install, ks-installer)' -o jsonpath='{.items[0].metadata.name}') -f

部署这两个服务到k8并启用jenkins devops为目标
方案1 手动打包
1 打包镜像
DockerFile
2 推送镜像到docker hub
3 拉取镜像并部署
方案2 使用jenkins打包部署,使用私有仓库管理镜像
使用方案2

minikube service ks-console -n kubesphere-system

PS C:\Users\DELL> minikube service ks-console -n kubesphere-system
|-------------------|------------|-------------|---------------------------|
|     NAMESPACE     |    NAME    | TARGET PORT |            URL            |
|-------------------|------------|-------------|---------------------------|
| kubesphere-system | ks-console | nginx/80    | http://192.168.49.2:30880 |
|-------------------|------------|-------------|---------------------------|
🏃  Starting tunnel for service ks-console.
|-------------------|------------|-------------|------------------------|
|     NAMESPACE     |    NAME    | TARGET PORT |          URL           |
|-------------------|------------|-------------|------------------------|
| kubesphere-system | ks-console |             | http://127.0.0.1:57389 |
|-------------------|------------|-------------|------------------------|

在kubesphere启用devops 以及 服务网格
参考:
www.kubesphere.io/zh/docs/v3.3/plu...

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!